全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3582
推到 Plurk!
推到 Facebook!

關於RS232 Rx 延遲的問題

缺席
w9190040
一般會員


發表:6
回覆:3
積分:1
註冊:2012-04-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2013-09-25 09:57:25 IP:211.72.xxx.xxx 訂閱
 小弟現在使用 PL2303 UART to USB 的IC 傳送是沒問題的 但接收會延遲80m~100ms  
不知道哪一位高手可以幫我看看我那裡有問題

COM 設定如下
DCB dcbCommPort;
AnsiString UART_Config;


hComm = 0;

hComm = CreateFile(ComPort->Text.c_str(), // 設定連接埠
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if(hComm == INVALID_HANDLE_VALUE)
{
Application->MessageBox((ComPort->Text " 正在被使用中,或者尚未驅動!").c_str(),"錯誤!!",MB_ICONERROR);
return;
// Application->Terminate();
}

GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 100;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);

UART_Config="baud=38400 parity=N data=8 stop=1";
dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
BuildCommDCB(UART_Config.c_str(),&dcbCommPort); // 設定鮑率為 9600
SetCommState(hComm, &dcbCommPort);
UART_Config=ComPort->Text " 己經成功的連線了!";
Application->MessageBox(UART_Config.c_str(),"成功訊息!!",MB_ICONINFORMATION);

接收如下

void OutHexData(AnsiString tmpStr)
{

char OutBuf[100]={0};
CMD = tmpStr.SubString(1,1);
strcpy(OutBuf,tmpStr.c_str());
Usart_List->Tx_List->Lines->Add(tmpStr);
frmDebug->lstSend->Lines->Add(tmpStr);
PurgeComm(hComm, PURGE_RXABORT | PURGE_RXCLEAR);

if (CMD == "L")
{
UART_Tx(OutBuf,tmpStr.Length());
//SDIAppForm->Timer1->Enabled=true;
for(;;)
{
ReadFile(hComm, InBuff, 50, &dwBytesRead, NULL);
if(dwBytesRead)
{
InBuff[dwBytesRead] = 0; // NULL TERMINATE THE STRING
Hax2ASCII(dwBytesRead);
//RxUartStatus="Receive Over";
Usart_List->Rx_List->Text=InBuff;
frmDebug->lstReceive->Text=InBuff;
SDIAppForm->Timer1->Enabled=false;
PurgeComm(hComm, PURGE_RXABORT | PURGE_RXCLEAR);
return;
}
}
}
else if (CMD == "P")
{
UART_Tx(OutBuf,tmpStr.Length());
for(;;)
{
ReadFile(hComm, InBuff, 50, &dwBytesRead, NULL);
if(dwBytesRead)
{
InBuff[dwBytesRead] = 0; // NULL TERMINATE THE STRING
Hax2ASCII(dwBytesRead);
//RxUartStatus="Receive Over";
Usart_List->Rx_List->Text=InBuff;
frmDebug->lstReceive->Text=InBuff;
SDIAppForm->Timer1->Enabled=false;
PurgeComm(hComm, PURGE_RXABORT | PURGE_RXCLEAR);
return;
}

TX如下

void UART_Tx(char *Tx_buff,int Tx_count)
{
int i = 0;
for(i=0;i<=Tx_count;i )
{
TransmitCommChar(hComm,Tx_buff[i]);
}

問題已經結決了 因未收取的資料並沒有到50 所以他會等帶到time out 在接收。


編輯記錄
w9190040 重新編輯於 2013-09-25 09:59:53, 註解 無‧
w9190040 重新編輯於 2013-10-08 17:24:52, 註解 無‧
jcjroc
高階會員


發表:21
回覆:279
積分:115
註冊:2002-09-18

發送簡訊給我
#2 引用回覆 回覆 發表時間:2013-09-29 22:01:06 IP:60.248.xxx.xxx 訂閱
用Timer???
那正常

系統時間:2024-04-27 2:24:35
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!