Question

I am trying to read a Datastream from RS232 with python 3.3.2 and pyserial. I start the stream with:

ser = serial.Serial('/dev/ttyUSB0', baudrate=19200, timeout=3, stopbits=serial.STOPBITS_TWO)
ser.write(bytes([0x05, 0x69, 0x02, 0x0A, 0x86]))

After this i get every second a 107 byte long dataset. First byte should be 107 (number of bytes) and second one should be 105 (Code).

What is the best way to seperate one 107 byte long dataset starting with 107 and 105? If I use print(ser.read(107)) a few times I get:

b'\x00\x00\x00\x00\x00P\xbf\x99\x10\xe0}\x86\xaaV\xd4\xeeg\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x87}i\xdf\n\x00\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc1\xc3k\x00\x00\x00\xc0t\xb4\xbd\xf0\x1c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Y\x00\x00\x00\x00\x00\x00\x80\x01\x00\x80\xc7\x10\xc0\xcd\xe6G\x0b\x99\xd4\xcb.'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc5\xeck\x00\x00\x00\xc0\xf4\xb3\xbd\xf0\x1c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa0V\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\xc8\x10@\xcd\xe67\x8b\x99\xd4\x0b/\x00\x00@\x00\xf6\xb7\xbf\xfd\xed\xaf:\x00\x00\x00\xe8\xf3J\xaf\x04\x00\x00\x00\x00\x00p\xbf\x99\x18\xe0}\x86\xaaV\xd4\xeeg\x12\x00\x00\x00\x00\x00\x00\x00'

The k (ascii for 107) is somewhere in the middle.

Was it helpful?

Solution

It might be helpful to flush your input before you put the request. I.e.,

ser.read(ser.inWaiting())

to read out all bytes which are waiting. Then, hoping that no further bytes are being sent, you can send your command:

ser.write(bytes([0x05, 0x69, 0x02, 0x0A, 0x86]))

This is supposed to make sure that all bytes which are coming next are an answer to this command.

Then read data until you get your 107:

found = False
buffer = '' # what is left from the previous run...
while not found:
    rd = ser.read(50)
    buffer += rd
    sp = buffer.split(chr(107), 1)
    if len(sp) == 2:
        pkt = chr(107) + sp[1] # candidate for a valid packet
        if pkt[1] == chr(105): # 
            while len(pkt) < 107: # TODO add a timeout condition here...
                rd = ser.read(107 - len(pkt))
                pkt += rd
            found = True
        else:
            buffer = pkt[1:] # process this further...
    else: # no 107 found; empty the buffer.
        buffer = ''
# Now we have a pkt of 107 bytes and can do whatever we want with it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top