问题
I have written a Linux app to read & write binary data to a remote device over a USB port that is emulating a serial port.
When I read data from the device, I have a USB sniffer that shows a binary data stream like this (0x01, 0x0A......0x13), but when my program reads the bytes, the 0x13 is not in the byte stream - this is the XOFF char, but I am not using XON/XOFF flow control (I think).
Tried both open read and write, as well as fopen fread and fwrite in binary mode, same result. Any ideas?
回答1:
Thanks for any responses, like the website. Turns out stty showed:
# stty -F /dev/ttyUSB0
speed 115200 baud;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 0; time = 10;
-brkint -imaxbel
-opost
-isig -icanon -echo -echoe
Even though it looked like flow control was off, The solution was to use cfmakeraw settings to see ALL characters and ignore nothing.
cfmakeraw() sets the terminal to something like the "raw" mode of the old Version 7 terminal driver: input is available character by character, echoing is disabled, and all special processing of terminal input and output characters is disabled. The terminal attributes are set as follows:
termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
termios_p->c_oflag &= ~OPOST;
termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
termios_p->c_cflag &= ~(CSIZE | PARENB);
termios_p->c_cflag |= CS8;
Can see all my data now :)
回答2:
Maybe it's better to avoid control characters being sent through serial port at all, and instead slightly modify app on Linux and remote device to encode/decode them into/from two bytes. For example:
0x00 0x00 -> 0x00
0x00 0x01 -> 0x13 (escape XOFF)
0x00 0x02 -> 0x11 (escape XON)
Considering the probability of appearing of these 3 bytes in a binary stream this shouldn't decrease the overall throughput I think.
And by the way, XON/XOFF is a software flow control and basic function of serial/terminal drivers. Actually this function can be useful in your case too - to avoid buffers overflow and missing some valuable bytes you can pause (XOFF) or resume (XON) transmission.
来源:https://stackoverflow.com/questions/8070632/missing-flow-control-data-0x13-from-reading-device-data-stream