線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:4628
推到 Plurk!
推到 Facebook!

RS232測試程式開啟COM10以上的PORT會有ERROR的情形?

尚未結案
Gg36
一般會員


發表:3
回覆:1
積分:0
註冊:2005-07-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-06-08 11:09:37 IP:61.31.xxx.xxx 訂閱
以下的code是使用loopback來測試rs232, 但是每當我開到com10以上都會發生failure的情形, 明明有接上loopback, 理論上進入CheckModemLine()以及BCheckTransfer()應該正常work, 但我debug時程式都會進去不該進去判斷式去執行裡面的內容...以導致出現failure.    麻煩各位大大幫幫忙吧, 感恩!!    <textarea class="cpp" rows="10" cols="60" name="code"> #include #pragma hdrstop #include #include "Unit1.h" #define UART_ERROR_NONE 0x00000000 #define UART_ERROR_ARGS 0x00000001 #define UART_ERROR_OPEN 0x00000002 #define UART_ERROR_TX 0x00000004 #define UART_ERROR_RX 0x00000008 #define UART_ERROR_DTR 0x00000010 #define UART_ERROR_DSR 0x00000020 #define UART_ERROR_RTS 0x00000040 #define UART_ERROR_CTS 0x00000080 #define UART_ERROR_DCD 0x00000100 #define UART_ERROR_RI 0x00000200 #define UART_TYPE_UNDEFINE 0 #define UART_TYPE_3_LINE 3 #define UART_TYPE_5_LINE 5 #define UART_TYPE_8_LINE 8 #define UART_TYPE_9_LINE 9 DWORD CheckModemLine(HANDLE hSerialPort, DWORD dwUartType); BOOL CheckTransfer(HANDLE hSerialPort); FILE *inf; //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Canvas->Font->Size=16; inf=fopen("com_set.txt", "r"); Refresh(); int count=1; while(!feof(inf)) { HANDLE hSerialPort; DWORD dwUartStatus = UART_ERROR_NONE, dwUartType = UART_TYPE_UNDEFINE; TCHAR serialPort[MAX_PATH]; COMMTIMEOUTS TimeOut; TimeOut.ReadIntervalTimeout=0; TimeOut.ReadTotalTimeoutMultiplier=0; TimeOut.ReadTotalTimeoutConstant=300; //總讀取time out時間=0.3秒 TimeOut.WriteTotalTimeoutMultiplier=0; TimeOut.WriteTotalTimeoutConstant=300; //總寫入time out時間=0.3秒 fscanf(inf, "%s %d", serialPort, &dwUartType); hSerialPort = CreateFile (serialPort, // Pointer to the name of the port GENERIC_READ | GENERIC_WRITE, // Access (read-write) mode 0, // Share mode NULL, // Pointer to the security attribute OPEN_EXISTING,// How to open the serial port 0, // Port attributes NULL); // Handle to port with attribute // to copy if(hSerialPort == INVALID_HANDLE_VALUE) dwUartStatus=UART_ERROR_OPEN; SetCommTimeouts(hSerialPort, &TimeOut); // Check uart switch(dwUartType) { case UART_TYPE_9_LINE: case UART_TYPE_8_LINE: case UART_TYPE_5_LINE: // Check modem line dwUartStatus |= CheckModemLine(hSerialPort, dwUartType); case UART_TYPE_3_LINE: // Check TX/RX dwUartStatus |= CheckTransfer(hSerialPort); break; efault: dwUartStatus = UART_ERROR_ARGS; break; } if(hSerialPort != INVALID_HANDLE_VALUE) CloseHandle(hSerialPort); Canvas->TextOut(50, 30*count, serialPort); if(dwUartStatus==0x00000000) Canvas->TextOut(150, 30*count, "Pass"); else Canvas->TextOut(150, 30*count, "Failure"); count ; } fclose(inf); } //--------------------------------------------------------------------------- DWORD CheckModemLine(HANDLE hSerialPort, DWORD dwUartType) { DWORD dwUartStatus = UART_ERROR_NONE, dwModemStatus; // Clear DTR if(dwUartType == UART_TYPE_9_LINE || dwUartType == UART_TYPE_8_LINE) { if(!EscapeCommFunction (hSerialPort, CLRDTR)) dwUartStatus |= UART_ERROR_DTR; } // Clear RTS if(!EscapeCommFunction (hSerialPort, CLRRTS)) dwUartStatus |= UART_ERROR_RTS; // get modem line status GetCommModemStatus(hSerialPort, &dwModemStatus); // check modem line status, should be off now if(dwUartType == UART_TYPE_9_LINE || dwUartType == UART_TYPE_8_LINE) { if(dwModemStatus & MS_DSR_ON) // check DSR dwUartStatus |= UART_ERROR_DSR; } if(dwModemStatus & MS_CTS_ON) // check CTS dwUartStatus |= UART_ERROR_CTS; if(dwUartType == UART_TYPE_9_LINE || dwUartType == UART_TYPE_8_LINE) { if(dwModemStatus & MS_RLSD_ON) // check DCD dwUartStatus |= UART_ERROR_DCD; if(dwUartType == UART_TYPE_9_LINE && (dwModemStatus & MS_RING_ON)) // check RI dwUartStatus |= UART_ERROR_RI; } // Set DTR if(dwUartType == UART_TYPE_9_LINE || dwUartType == UART_TYPE_8_LINE) { if(!EscapeCommFunction (hSerialPort, SETDTR)) dwUartStatus |= UART_ERROR_DTR; } // Set RTS if(!EscapeCommFunction (hSerialPort, SETRTS)) dwUartStatus |= UART_ERROR_RTS; // get modem line status GetCommModemStatus(hSerialPort, &dwModemStatus); // check modem line status, should be on now if(dwUartType == UART_TYPE_9_LINE || dwUartType == UART_TYPE_8_LINE) { if(!(dwModemStatus & MS_DSR_ON)) // check DSR dwUartStatus |= UART_ERROR_DSR; } if(!(dwModemStatus & MS_CTS_ON)) // check CTS dwUartStatus |= UART_ERROR_CTS; if(dwUartType == UART_TYPE_9_LINE || dwUartType == UART_TYPE_8_LINE) { if(!(dwModemStatus & MS_RLSD_ON)) // check DCD dwUartStatus |= UART_ERROR_DCD; if(dwUartType == UART_TYPE_9_LINE && !(dwModemStatus & MS_RING_ON)) // check RI dwUartStatus |= UART_ERROR_RI; } return dwUartStatus; } BOOL CheckTransfer(HANDLE hSerialPort) { CHAR *pTestPattern = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXY", pReadPattern[MAX_PATH]; DWORD dwUartStatus = UART_ERROR_NONE, dwWriteLen, dwReadLen; // Send test pattern if(!WriteFile (hSerialPort, pTestPattern, strlen(pTestPattern), &dwWriteLen, NULL) || (dwWriteLen != strlen(pTestPattern))) { dwUartStatus |= UART_ERROR_TX; goto error_ret; } // Read the data from the serial port. dwReadLen = 0; if(!ReadFile (hSerialPort, &pReadPattern, strlen(pTestPattern), &dwReadLen, 0) || (dwReadLen != strlen(pTestPattern)) || memcmp(pReadPattern, pTestPattern, strlen(pTestPattern))) dwUartStatus |= UART_ERROR_RX; error_ret: return dwUartStatus; }</textarea>
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-06-08 11:26:01 IP:122.124.xxx.xxx 未訂閱
http://delphi.ktop.com.tw/board.php?cid=168&fid=917&tid=74379
harpist
資深會員


發表:3
回覆:251
積分:430
註冊:2002-10-03

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-06-08 11:28:12 IP:59.104.xxx.xxx 未訂閱
COM10 以上請用這樣的格式 <textarea class="cpp" rows="10" cols="60" name="code">serialPort = "\\\\.\\COM10"; </textarea>
------
~§~迷時師渡,悟了自渡~§~
系統時間:2024-12-04 17:09:04
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!