Home > other >  Python reads the serial data cannot read how to solve
Python reads the serial data cannot read how to solve

Time:09-23

Recently doing python serial module to receive serial data and writing, encounter a problem. The python when receive and read serial data, sometimes the data will not be a one-off after reading, I was doing this resolution will occur when the data is not complete due to the phenomenon of packet loss, complete data have fixed frame head, also has the check code, but because of the length of the data is not fixed, so when parsing or problems, a serial port to python is byte, I with b 'fixed frame head after cutting data plus b' fixed frame head, did not receive the data, that may add b 'fixed frame head, but parse the data is wrong, cannot resolve will be discarded, I how should operate, ask the great spirit guide

CodePudding user response:

For each data plus head and tail and validation, after receiving the data set bag, the group package complete post-processing logic,
Again a little bit complicated to design error retransmission

CodePudding user response:

I'm taking over other people's code, serial port protocol is their, only fixed and check code in the beginning, no end, and the data length is not fixed, I can't judge data exactly have finished reading,
Because involved in previous versions of the product, so the agreement also cannot modify the
This problem has been solved now, but still want to thank brothers advice

CodePudding user response:

Also recently in writing code, serial communication, serial interface look too simple, compared with the socket, much less protocol processing packaging, error handling, accident handling all get oneself,
Actually a robust complete serial communication interface from lu code, compared to the socket, much more difficult, it is no wonder that the CDT statute is the most common, and spent a long time to spread 101,

Now, it seems, even 101 specifications, has been industrialized so long, or because the frame structure is too complex, vendors can compatible part is limited, can't easily all implementation can be compatible,

Summarize the idea, really strong, robust serial communication interface, or is it better to put the data ATM, or TCP/IP grouping processing, don't figure is simple, the application layer directly to the load to the physical, but his first realization of a data link layer, to load the application layer, can make work easier,

CodePudding user response:

refer to the second floor huangxx0815 response:
I am take over other people's code, serial port protocol is their, only the fixed and the check code in the beginning, no end, and the data length is not fixed, I can't judge data exactly have finished reading,
Because involved in previous versions of the product, so the agreement also cannot modify the
This problem has been solved now, but still want to thank brothers advice

Could you tell me how to solve? I also met the same problem!

CodePudding user response:

To judge the data first, starting from the protocol header, the protocol is correct and then see the data length is consistent, if the data did not accept the data length is right, then the incomplete data stored by the first on the list, the second encounter data protocol in head, direct and not receiving the data together for the first time, continue to parse, it is ok, test this method is feasible in person
  • Related