This is my first python project away from learning through textbook exercises. I have no other experience coding so sorry that the formatting is likely horrendous.
I'm using DMU11 form Silicon Sensing and want a python program to be able to read the data from it, plot it and save it to a file. I'm currently stuck at the first stage and can't figure out how to get the data to stream using readline.
Also, any help/ comments on the other parts (and/ or better formatting) would also be greatly appreciated!!
Here is my code so far:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pyreadline as readline
import serial
import time
import sys
import csv
import struct #used to read big-endian format from a file
ser = serial.Serial(port = "COM5",
baudrate=460800,
bytesize=8,
timeout=2,
stopbits=serial.STOPBITS_TWO
)
print(ser)
#Gives this: Serial<id=0x3eedd90,open=True>
# (port='COM7', baudrate=460800, bytesize=8, parity='N',
# stopbits=2, timeout=2, xonxoff=False, rtscts=False,
# dsrdtr=False)
#count = 0
pin_high = "1C13\n"
ser.write(pin_high.encode())
#ser = ser.readline()
#h=2bytes l=4bytes
''' IMU MESSAGE OUTPUT ORDER
Record Number
Time Stamp
Header [2bytes hexID: 0x55AA integer]
Message Count [2bytes 0-65535 integer]
Axis X Rate (°/s) [4bytes hexID: 0x00 SGL]
Axis X Accelerometer Y (g) [4bytes hexID: 0x02 SGL]
Axis X Accelerometer X (g) [4bytes hexID: 0x03 SGL]
Axis X Temperature (°C) [4bytes hexID: 0x06 SGL]
Axis Y Rate (°/s) [4bytes hexID: 0x20 SGL]
Axis Y Accelerometer Y (g) [4bytes hexID: 0x22 SGL]
Axis Y Accelerometer X (g) [4bytes hexID: 0x23 SGL]
Axis Y Temperature (°C) [4bytes hexID: 0x26 SGL]
Axis Z Rate (°/s) [4bytes hexID: 0x40 SGL]
Axis Z Accelerometer Y (g) [4bytes hexID: 0x42 SGL]
Axis Z Accelerometer X (g) [4bytes hexID: 0x43 SGL]
Axis Z Temperature (°C) [4bytes hexID: 0x46 SGL]
IMU Temperature (°C) [4bytes hexID: 0x60 SGL]
Aux Voltage (V) [4bytes hexID: 0x61 SGL]
System Startup BIT Flags [2bytes hexID: 0xC0 Binary]
System Operation BIT Flags [2bytes hexID: 0xC1 Binary]
Item Error Indication Flags 1 - 16 [2bytes hexID: 0xC2 Binary]
Checksum [2bytes 0-65535 integer]
'''
#header, count,
#x_rate, x_acc_y, x_acc_x, x_temp,
#y_rate, y_acc_y, y_acc_x, y_temp,
#z_rate, z_acc_y, z_acc_x, z_temp,
#IMU_temp, AUX_V, StartUp_BIT, Opp_BIT, Error_BIT, checksum = struct.unpack(">hhllllllllllllllhhhh", s)
while True:
try:
ser_bytes = ser.readline()
decoded_bytes = float(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
print(decoded_bytes)
except:
print("Keyboard Interrupt")
break
'''
# Read and record the data
data =[] # empty list to store the data
for i in range(50):
b = ser.readline() # read a byte string
string_n = b.decode() # decode byte string into Unicode
string = string_n.rstrip() # remove \n and \r
flt = float(string) # convert string to float
print(flt)
data.append(flt) # add to the end of data list
time.sleep(0.1) # wait (sleep) 0.1 seconds
ser.close()
for line in data:
print(line)
'''
#break
if ser.isOpen():
ser.close()
ser.open()
ser.isOpen()
ser.read()
#Gives the output: b'\t'
print(ser.read(64))
dataPacket = ser.readline() #reply
dataPacket=str(dataPacket,'utf-16')
print(dataPacket)
# Gives the output: b'\x1f=B\x16\xa0\x8cB-\x87\x8e@*\xd5\xde\xbf\x1fg\xbfDz,mB\x18bsB\x18)\xac@SO\xad<\xa0\x8f\x82A\x04c\xa4\xbf\x8e\xd6\xcbC\n\xdc\xd2\x00\x00\x00\x00\x00\x00\x00\x00\x10\x1bU\xaa\xd3M'
#out = ''
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(40)
if out != '':
print(">>" + out)
ser.close()
1 Answer 1
You can't use readline, because it works with data separated with newline data sequences.
The sensor sends packets of binary data, not lines of text characters.