Ich bin neu im Forum, deshalb ein paar kleine Infos zu Beginn:
Ich studiere Telematik im 2. Semester und habe dementsprechende Grundlagenkenntnisse in Java.
Mit Python habe ich bisher nur leichte Berührungspunkte gehhabt.
Nun zu meinem Problem:
Im Rahmen eines Projektes soll im ersten Schritt von einem Sensormodul, das einen internen Serial->USB Adapter besitzt, mittels eines RaspberryPI der eingehende Datenstrom vom USB-Port in eine Datei auf einem USB Stick geschrieben werden.
Das Skript dazu funktioniert bereits und sieht wie folgt aus:
Code: Alles auswählen
#!/usr/bin/env python
# get data from serial port, save them to a file
from __future__ import print_function
import serial, io, time
#time.sleep(10)
addr = '/dev/ttyUSB0' # serial port to read data from
baud = 115200 # baud rate for serial port
fname = '/mnt/usb/sensor-log.txt' # path to file to save data in
fmode = 'a' # file mode = append
list = []
with serial.Serial(addr, baud) as pt, open(fname,fmode) as outf:
# print ('Writing data to file \''+fname+'\'...')
# print ('Press Ctrl+C to exit')
while (1):
x = pt.read()
outf.write(x) # write data to file
outf.flush() # make sure it actually gets written out
list.append(x)
print (list)
Das Problem besteht darin, dass die Daten des Sensors Hexadezimal dargestellt werden, ich sie aber Binär, also Bit für Bit, brauche.
Hier ein Beispiel was auf der Konsole ausgegebn wird:
['H', 'B', '3', '#', '`', '\x0e', '}', 'a', '\xaa', 'z', '\x04', 'w', '\n', 'v', '\x00', 'x', '\xa2', '\x81', 'b', '|', '\xdf', '\x8d', 'N', 'C', '\xbf', '\x0f', '\x81', '\x00', '\x06', '\x03', '\x9a', '\x00', '\x10', '?', '\xff', '=', '\xac', '\xa8', '\x99', '\xc5', '\xab', '\xc9', '\xdc', '\xc5', '\x9e', '\x1e', '\xca', '\x00', '\xb5', '\x00', '\x88', '\xb4', '\xe2', 'k', '\x00', '\xbc', '\xf9', '\x00', '\x00', '\x00', '\x00', 'M', '\xf2', '\x03', '\xff', '\x02', '\x90', '\x00', '\xa4', '?', '/', '\xa3', '\xc4', '\xbe', '\xf7', '\t', 'O', '\xbd', '\xbf', '\xfb', '\xa1', '?', '\t', 'O', 'a', '\xbf', 'y', '\xa6', '\xcf', '>', '\xcc', '\x8a', '\xee', '?', '\x8e', '}', '\x87', '>', '\xd0', 'h', '.', '?', 'S', '\x92', '\x8b', '\xbe', '\xc7', '%', '@', '\xbf', '%', 'A', '\xdb', '\xbd', "'", '\xd9', '\x88', '\xbf', 'C', ';', '\xb2', '\xbf', '%', 'n', '\x89', '?', '\x0f', '\xbf', '\x18', '?', '\x04', 'N', '\xb8', '\xbf', '\xdd', '\x0b', 'F', '?', '\xa3', '\x0f', 'S', '\xbe', '\xe2', '[', 'W', '@', 'e', ')', 'H', '@', '\xf2', '\xf7', '\xaa', '\xc0', '\xc9', '_', '\x16', '\xbd', 'e', '\x03', '\x00', '\xbe', '9', '\xdc', '\x80', '?', '5', '\xc0', '\xa0', 'B', '\x15', 'J', ' ', 'B', '3', 'q', '\x80', '\x0e', 'n', 'h', 'w', '}', '\xea', 'w', '\xe5', 't', '\xf9', 'x', '9', '\x81', '\xea', '}', ')', '\x8b', '\xbe', 'C', '\xbe', '\x0f', '\x82', '\x00', '\x06', '\x03', '\xae', '\x00', '\x10', '?', '\xff', '=', '\xac', '\xa8', '\x99', '\xc5', '\xab', '\xc9', '\xdc', '\xc5', '\x9d', 'T', '\xb8', '\x00', '\x05', '\x00', '8', '\x04', '$', '\x02', '\x00', '\xbc', '\xc0', '\x00', '\x00', '\x00', '\x00', '\xcb', '\xd5', '\x03', '\xff', '\x02', '\x90', '\x00', '\xa4', '?', '0', '\x9b', 'Z', '\xbe', '\xfb', '\xef', '3', '\xbd', '\xb7', '\n', '=', '?', '\x05', '\xfe', '\xdc', '\xbf', '\x7f', '\x1d', '\x8f', '>', '\xce', '\x1e', '\xd3', '?', '\x89', '\xdb', 'i', '>', '\xdf', 'F', '\xd5', '?', 'O', 'e', 'Q', '\xbe', '\xc8', '\x99', '\x1c', '\xbf', '"', '\\', '\xcd', '\xbd', '\x03', '\xd0', '\xef', '\xbf', 'E', '\xc0', '\xf8', '\xbf', '#', 'o', '\x9d', '?', '\x15', '\xd9', '\xb2', '>', '\xff', '\xe3', ' ', '>', '\xd6', '\xfa', '\x14', '?', '\xdd', '\xc8', ',', '\xbf', '\x81', 'K', '\xd2', '@', 'c', '\x05', '\x8c', '@', '\xd9', '\xf2', 'D', '\xc0', '\xbc', '(', '\xf0', '\xbd', '\x0b', '\xdc', '\x00', '\xbe', '\x14', '\xb2', '\x80', '?', '\x1c', '\xf8', '\xa0', 'B', '\x15', '?', '\xd0', 'B', '4', '\r', '\xc0', '\x0e', '\x92', '`', '\xbc', 't', '5', 'w', '\xf0', 'v', '<', 'x', '\xe0', '\x82', 'J', '}', '\xce', '\x8a', '\x16', 'C', '\xbc', '\x0f', '\x84', '\x00', '\x06', '\x03', '\xc2', '\x00', '\x10', '?', '\xff', '=', '\xac', '\xa8', '\x99', '\xc5', '\xab', '\xc9', '\xdc', '\xc5', '\x9e', 'V', '\x8e', '\x80', '^', '\x00', 'c', '_', 'X', '\x1f', '\x00', '\xbc', '\x1a', '\x00', '\x00', '\x00', '\x00', '\xe0', 'H', '\x03', '\xff', '\x02', '\x90', '\x00', '\xa4', '?', '2', 'u', '\xb5', '\xbe', '\xff', 'K', '\x10', '\xbd', '\x8e', 'b', '\xa8', '?', '\x02', '\xae', '|', '\xbf', '\x7f', '\xba', '\xfc', '>', '\xd9', '~', '\x83', '?', '\x83', '\xd2', '\xf3', '>', '\xf0', '6', '3', '?', 'G', '\xf2', '\x90', '\xbe', '\xd3', '\x03', '4', '\xbf', '$', 's', '\x10', '\xbc', '\x96', '\xd5', '(', '\xbf', 'D', '#', '\\', '\xbf', '\x1b', '"', '\xa3', '?', '\x1f', '\xcb', '\xc8', '>', '\xfc', 'v', 'F', '?', 'H', '2', ',', '@', '"', '\x8d', '\xba', '=', '[', '\xfe']
Ich möchte gerne das vor der Ausgabe die Hex-Werte in Binärwerte gewandelt werden.
Im zweiten Schritt des Projektes soll aus dem Bitstrom das Protokoll des Sensors gefiltert und die Informationen ausgewertet werden.
Ich habe bereits das Forum durchsucht und auch Suchmaschinen bemüht, jedoch leider ohne eine funktionierende Lösung zu finden.
In der Hoffnung das es eine Möglichkeit gibt mein Problem zu lösen schon einmal vielen Dank an alle!