How to get the size (in bytes) of output in Python

后端 未结 1 1968
野的像风
野的像风 2021-01-26 05:57

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 tal

相关标签:
1条回答
  • 2021-01-26 06:23

    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.
    0 讨论(0)
提交回复
热议问题