0

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,,

  • My Question Is: Based on this format, how to get the size (in byte) of the above result? The reason for this question is, I can get the full/complete result as long as I know how large it is.

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.

7
  • Probably something like: data=ser.read() then size=len(data) Commented Dec 2, 2015 at 18:19
  • It does not look like the answer will always have the same size. There has to be a different way to read the response. Maybe you shoud set up a reasonable timeout (to detect the end) and then call ser.readlines()? Commented Dec 2, 2015 at 18:23
  • Can you program the robot? e.g. make it output something like "END" at the end of its output. Commented Dec 2, 2015 at 19:37
  • Also, as zvone mentioned, your output will be of different lengths possibly. However, the number of lines should be the same. Why not keep reading until you have 16 newlines? Commented Dec 2, 2015 at 19:39
  • you are right, the output will be different with dif size and lines. So I am afraid I cannot keep tracking 16 newlines bc they are dif... =( Commented Dec 2, 2015 at 19:53

1 Answer 1

1

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.

  • The response message size is apparently not fixed. Maybe there is a way to ask the device "what would be the length of the answer to getversion"?
  • Some protocols would prefix each message with the length information. Here there is none. Perhaps you can put the device in a different mode where it deos something like that by sending it some special command?
  • Your message does not look like it has as the end marked, but perhaps it is just not visible, e.g. might there be a null character ('\0') at the end? If there is one, you could read character-by-character until it appears.
  • Failing to find any other solution, you can try setting a reasonable timeout on reads (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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.