以下的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>