Reputation: 1503
firstly, I would like to thanks to whomever would help me.
- Environment
I am using Python v2.7 in Windows 8 OS. I am using COM4 to talk to robot by sending some commands in Python code.
I send a command getversion
to robot and suppose to get a bunch of data which is in the following format (I omit some, it is too long):
Component,Major,Minor,Build,Aux
APPassword,956FC721
BaseID,1.2,1.0,18000,2000,
BatteryType,4,LIION_4CELL_SMART,
Beehive URL, beehive.cloud.com
BlowerType,1,BLOWER_ORIG,
Bootloader Version,27828,,
BrushMotorType,1,BRUSH_MOTOR_ORIG,
BrushSpeed,1400,,
BrushSpeedEco,800,,
ChassisRev,1,,
Cloud Selector, 2
DropSensorType,1,DROP_SENSOR_ORIG,
LCD Panel,137,240,124,
LDS CPU,F2802x/c001,,
LDS Serial,KSH13315AA-0000153,,
To be specific, my code is:
ser.write('getver \n') # send 'getversion' cmd to robot
ser.read(1305)
The response size of getver
is 1305 byte, yes, I count it manually, that is why I would like to ask Python to tell me how large it is automatically.
Upvotes: 0
Views: 449
Reputation: 19352
In order to be able to communicate with a device, you have to know what the protocol is for that communication. Whoever designed the protocol had to define a way for you to know how many bytes to read. If you have a specification, it probably covers that question.
So, there is either a way to determine number of bytes beforehand or to detect the end of transmission, e.g. by the existance of a special end character.
Without some sort of specification, we can only guess what the protocol is.
'\0'
) at the end? If there is one, you could read character-by-character until it appears.ser = serial.Serial(..., timeout=2, ...)
). Then try to read everything. When there is nothing more to read, the read
function will freeze indefinitely, unless there is a timeout. If you set a reasonably long timeout and no date is received in that time, you can assume the transmission is over.Upvotes: 1