串行口终端乱码的解决.

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hejinjing_tom_com/article/details/79863440

pc, 为usb转485 , 设备名 /dev/ttyUSB1

嵌入式: 设备名 /dev/ttymxc3

二者的通信协议为9600, 8n1, 无流控.

用简单的echo, cat 不能得到正确的结果. 可能是stty未能完全匹配通信协议, 试过stty sane -F /dev/ttyUSB1 不起作用.

用串行口终端cutecom可以正常显示, 可惜配置并没有被保留到系统.

用cat 却不能正常显示(乱码), cat -A 甚至能显出乱在哪里.
例如: #echo "aaaaa" > /dev/ttymxc3
      #cat -A < /dev/ttyUSB1
       显示了aM-aM- 等等,
我知道用stty 可以对终端进行设置, 但没有记录当时是如何了.
后来我用minicom 把它配置好了, 在minicom 中可以正常收发,这个配置还被保留到了系统,
此时用cat 也能正常收发了, 我打印了一下此时的ttyUSB1 配置, 以备以后出现问题时参考!
 
正确的ttyUSB1 配置, 用cat 命令终于可以正常显示了.
hjj@Inspiron:~$ stty -a -F /dev/ttyUSB1
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 5;
-parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
-iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt
-echoctl -echoke

如果再有串行口或者485收发乱码, 可以用stty 或 minicom 配置一下! ,推荐用minicom, stty 是很繁琐的.
留此快照备查备考!

猜你喜欢

转载自blog.csdn.net/hejinjing_tom_com/article/details/79863440