问题
From what I have been able to discover, XON and XOFF are nothing but special ASCII characters used for handshaking between two serial devices. But it is not clear to me what actually happens when I use this type of flow control for communication. Is there just an XON character at the start of my message, and an XOFF character at the end? I.e., when I write something using PySerial like this:
import serial
ser = serial.Serial('COMx', 9600, xonxoff = True)
ser.write('Hi!\r\n')
Does the device actually receive this:
\x11Hi!\r\n\x13
(\x11
and \x13
being the XON and XOFF characters in Python, respectively.) Or is there something else going on? (Sadly, with the demise of PortMon I don't have a simple way to test this myself.)
回答1:
XON/XOFF are used for flow control. If you are processing the information and can't receive any more because your input buffers are full, then you would send XOFF to tell the other party to stop sending. When your input buffers have some room, you would send an XON to tell the other party to start sending again. The applications I've seen (and I did a lot of this many years ago) only use them when needed, not as a standard part of a message.
One of the issues with XON/XOFF is that on a noisy line, these characters might not be received correctly, so the receiving party might send XOFF when the buffer is 80% full, and maybe again when 90% full, and then start sending them more frequently if the sending party doesn't stop.
Similarly, the receiving party might send XON when ready to receive, but if nothing more is received then it might send it periodically to make sure that the first one wasn't missed.
To implement successfully, neither character can appear in any part of the messages. Wikipedia explains some of this better than I can.
In PySerial, you shouldn't have to actually send them explicitly. It will handle it for you or you can use the API to send it.
来源:https://stackoverflow.com/questions/56568733/what-does-xon-xoff-flow-control-actually-do