一、【情形1】
【Ubuntu14.04-->Win8】
[環(huán)境描述]
PC1:Win8,運(yùn)行串口調(diào)試工具:USR-TCP232-Test.exe;串口COM1的參數(shù)設(shè)置為“9600,8,1,N"。
PC2:Ubuntu14.04;串口/dev/ttyUSB0的參數(shù)設(shè)置為“9600,8,1,N"。
[代碼描述]
// 串口傳輸線程 處理函數(shù)void*CTestEth::ThreadFuncHandleSerialPortTransfer(IN void* arg){ if ((NULL == arg)) { return NULL; } CTestEth* pc_test_eth = (CTestEth*)arg; // 打開(kāi)串口 if (pc_test_eth->SerialPortOpen(pc_test_eth)) { cout << "pc_test_eth->SerialPortOpen() Error!" << endl; return NULL; } while (1) { // 串口傳輸函數(shù),循環(huán)發(fā)送字符串:"hello, ubuntu14.04/n"。 if (pc_test_eth->EthToSerialPortTransfer(pc_gap_eth)) //usleep(100000); // 延遲100000微秒,即100毫秒 //sleep(1); // 延遲1秒 } // 關(guān)閉串口 pc_test_eth->SerialPortClose(pc_gap_eth); return NULL;}
[問(wèn)題描述]
在PC2上,啟動(dòng)串口傳輸線程,循環(huán)發(fā)送字符串“hello, ubuntu14.04/n”。
在PC1上的串口調(diào)試工具中進(jìn)行接收,發(fā)現(xiàn)數(shù)據(jù)接收不全,有時(shí)接收到“hello,”,有時(shí)接收到“hello, ubun”,有時(shí)接收到“h”。
[原因分析]
出現(xiàn)類似問(wèn)題的原因是
(1)發(fā)送端或者接收端,串口配置代碼有誤。
(2)頻繁打開(kāi),關(guān)閉串口。
(3)發(fā)送端程序沒(méi)有設(shè)置延時(shí),導(dǎo)致對(duì)端接收程序來(lái)不及接收,或者接收不全。
(4)還有可能是發(fā)送端或者接收端的串口接觸不良,出現(xiàn)松動(dòng)。
(5)可能還有其他原因,稍后補(bǔ)充。
[對(duì)應(yīng)的解決方法]
(1)核對(duì)發(fā)送端以及接收端的串口參數(shù)設(shè)置。另外,仔細(xì)核對(duì)兩端的串口代碼。
(2)不要頻繁打開(kāi),關(guān)閉串口。
(3)在發(fā)送端程序中添加延時(shí)代碼。
(4)重新安裝發(fā)送端或者接收端的串口線。
[延時(shí)函數(shù)]
ubuntu中的延時(shí)函數(shù)為:sleep() // 秒級(jí)usleep() // 微秒級(jí),1秒=10^6微秒nanosleep() // 納秒級(jí),1秒=10^9納秒
[其他參考信息]
(1)msdn中的SerialPort.Open 方法
http://msdn.microsoft.com/zh-cn/library/system.io.ports.serialport.open(v=vs.110).aspx
備注
每個(gè)SerialPort對(duì)象只能有一個(gè)打開(kāi)的連接。
對(duì)于所有應(yīng)用程序,最佳做法是在調(diào)用Close方法之后和嘗試調(diào)用Open方法之前等待一會(huì)兒,因?yàn)槎丝诳赡芪醇磿r(shí)關(guān)閉。
(2)linux man page
sleep(3) - Linux man page
http://linux.die.net/man/3/sleep
usleep(3) - Linux man page
http://linux.die.net/man/3/usleep
nanosleep(2) - Linux man page
http://linux.die.net/man/2/nanosleep
二、【情形2】
【ubuntu14.04-->ubuntu14.04】
[環(huán)境描述]
PC1:ubuntu14.04;串口/dev/ttyS0的參數(shù)設(shè)置為“9600,8,1,N"。
PC2:Ubuntu14.04;串口/dev/ttyUSB0的參數(shù)設(shè)置為“9600,8,1,N"。
[情況描述]
在兩端均為ubuntu14.04的情況下,
在PC2中啟動(dòng)串口傳輸線程,
在PC1的終端中輸入命令
# cat /dev/ttyS0
那么即使PC2中的數(shù)據(jù)發(fā)送速率很快,每次發(fā)送的數(shù)據(jù)包很大(幾百字節(jié)),PC1中的終端也可正常接收數(shù)據(jù)。
三、【原文網(wǎng)址】[原文發(fā)布于2014-09-10 18:05]
http://www.CUOXin.com/tom-and-jerry/p/3964781.html
【完結(jié)】
新聞熱點(diǎn)
疑難解答
圖片精選