Reading a serial port in python with unknown data length -


hello trying read data pic32 microcontroller configured serial port.

the pic32 sends "binary" data variable in length (14 26 bytes long). want read in data , separate bits convert them decimal equivalent.

import serial import csv  #open configuartion file open('config.txt') configfile:    #save config parameters array called parameters    parameters = configfile.read().split()  #function initialize serial port def init_serial():     global ser     ser = serial.serial()     ser.baudrate = 9600     ser.port = 'com7'     ser.timeout = 10     ser.open()     if ser.isopen():         print ('open: ' + ser.portstr)  #call serial initilization function init_serial()  #writes lines config file serial port counter = 0 while counter<4:     ser.write(chr(int(parameters[counter])).encode('utf-8') + chr(int(parameters[counter+1])).encode('utf-8'))     counter = counter + 2  #opens csv file append  resultsfile = open('results.csv', 'wt')  #writes titles of 4 columns csv file resultsfile.write("{} {} {} {}\n".format('channelai', 'channelaq', 'channelbi', 'channelbq'))    count=0 while count < 10:          #read serial port     incoming = ser.read(26)      #decodes incoming bytes string     incoming = incoming.decode('cp1252')       #will select element 4, 5 & 6 incoming data     channelaistr = incoming[4:6]     #converts slected elements integer     channelai=int(channelaistr, 16)      channelaqstr = incoming[7:10]     #channelaq=int(channelaqstr, 16)      channelbistr = incoming[10:13]     #channelbi=int(channelbistr, 16)      channelbqstr = incoming[13:16]     #channelbq=int(channelbqstr, 16)      #writes csv file     resultsfile.write("{} {} {} {}\n".format(str(channelai), str(channelaq), str(channelbi), str(channelbq)))                   count = count + 1  #close file save memory resultsfile.close() 

i having trouble reading , converting bits serial port. on how appreciated.

i know reading serial port correctly , getting data looks "\x00\x7f\x7f" example. want convert 3 byte long string integer.

i not sure how determine if data 14 or 26 bytes long or in-between.

in cases, might want use wrapper class wraps io.

on every request of data, can have wrapper either read number of bytes or bytes available, until have enough decode. wrapper decodes them , returns them tuple.

this first guess how can proceed; not see having trouble in code.


but syntactig sugar , more advanced state of program.

for start, let's locate more obvious things: seem ripping data apart in wrong way. code

#will select element 4, 5 & 6 incoming data channelaistr = incoming[4:6] 

does not fit each other: indexing [4:6] means 4 inclusive 6 exclusive. if want 4, 5 , 6, have write [4:7].

the next step convert integer. if have \x00\x7f\x7f, can mean lot of things:

  • if 24 bit integer, can little or big endian. in first case, 0x7f7f00, in second case 0x007f7f.

    these cases can dealt with

    >>> a='\x00\x7f\x7f' >>> import struct >>> struct.unpack("<i", a+"\x00")[0] 8355584 >>> 0x7f7f00 8355584 >>> struct.unpack(">i", "\x00"+a)[0] 32639 >>> 0x7f7f 32639 
  • if different kind of data format (maybe floating point?), have more verbose these data mean.

so, if right integer solution, of

channelai = struct.unpack(">i", "\x00" + channelaistr)[0] channelai = struct.unpack("<i", channelaistr + "\x00")[0] 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -