如何在C程序中通過串口終端讀取二進制數(shù)據(jù)?我通過串口與嵌入式設(shè)備通信。默認(rèn)情況下,嵌入式Linux使用此端口作為終端。但我想通過端口傳輸二進制數(shù)據(jù)(服務(wù)數(shù)據(jù)包)。我的/ etc / inittab文件有一個“getty”調(diào)用: console :: respawn:/ sbin / getty 115200 ttyS0我還有/ etc / passwd文件,其中“admin”用戶在登錄后啟動我的“cli”應(yīng)用程序: admin:8Mt / Jtxcyg8AY:1000:0:admin:/ tmp:/ tmp / cli運行程序之前我的默認(rèn)ttyS0設(shè)置是:~ # stty -aspeed 115200 baud;stty: standard input
line = 0;intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ^J;eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff-iuclc -ixany -imaxbel -iutf8opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0isig icanon -iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt-echoctl echoke~ #所以,在我的cli程序中,我執(zhí)行以下操作:main (){
...
system("stty erase ^H);
system("stty -F /dev/ttyS0 -icrnl -ixon -ixoff -opost -isig -icanon -echo"); // enter in non-canonical (raw) mode
// What function do I need to use here to retrieve binary data (also symbols that > 0x7F) from /dev/ttyS0?
system("stty -F /dev/ttyS0 icrnl ixon ixoff opost isig icanon echo"); // go back to canonical mode
...
exit(0);}我嘗試使用read()函數(shù)(使用unsigned char緩沖區(qū))來獲取二進制數(shù)據(jù),但無法接收正確的數(shù)據(jù)。我還初步打開/ dev / ttyS0來獲取file_descriptor并使用read()func。我的程序發(fā)送3個字節(jié):0xAA,0x02,0xFE。但在syslog中,我總是看到設(shè)備收到錯誤的符號:0x98,0xE6,0x18。有什么事?如何獲得正確的二進制數(shù)據(jù)?
如何在C程序中通過串口終端讀取二進制數(shù)據(jù)?
互換的青春
2019-08-28 13:47:43