求助:USB转串口通信数据无法正常接收
我用windows API 写了一个串口通信的程序,由于要用到两个串口,因此一条要用到USB转串口,在用电脑现有串口测试程序时收发数据正常,但用USB转串口发送数据会出错,哪为高手能帮我分析下,问题出在哪里,不甚感激!!!!!!
代码如下:
//定义的串口通信类SerialPort.h
class CSerialPort
{
public:
typedef enum tagParity
{
NON,
ODD,
EVEN,
MARKY,
SPACE
}PLARITY;
CSerialPort(void);
~CSerialPort(void);
bool Init_SerialPort(LPTSTR szPort=L"COM4", UINT nBaudRate = 9600 , PLARITY cParity = EVEN,
UINT ndatabits = 8, UINT nStopbits = 1);
DWORD SerialWrite(PBYTE wrBuffer, int nSize);
DWORD SerialRead(PBYTE rdBuffer, int nSiz);
void CloseSerialPort(void);
bool IsOpened(void);
protected:
HANDLE m_CommPort;
bool m_Opened;
};
//类成员函数Serial.cpp
#include "SteAfx.h"
#include "serialport.h"
#include "stdio.h"
CSerialPort::CSerialPort(void)
: m_CommPort(0), m_Opened(false)
{
}
CSerialPort::~CSerialPort(void)
{
}
bool CSerialPort::Init_SerialPort(LPTSTR szPort,
UINT nBaudRate,
PLARITY cParity,
UINT ndatabits,
UINT nStopbits)
{
BOOL bPortReady = FALSE;
DWORD dwError = 0, dwRC = 0;
DCB dcb;
COMMTIMEOUTS CommTimeouts;
/* Modified *******************************/
//CString errStr; // remove
/*******************************************************************/
m_CommPort = CreateFile(
szPort,
GENERIC_READ|GENERIC_WRITE,
0, // exclusive
NULL, // sec attr
OPEN_EXISTING,
0, // no attributes
NULL); // no template
if (m_CommPort == INVALID_HANDLE_VALUE)
{
LPVOID lpMsgBuf;
lpMsgBuf = NULL;
dwError = GetLastError();
dwRC = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError, // from GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
if (dwRC && lpMsgBuf)
{
/* Modi *******************************/
// errStr.Format(L"COM open failed: Port = %s Error = %d - %s", szPort, dwError, lpMsgBuf); //remove
//AfxMessageBox(errStr);
/*******************************************************************/
}
else
{
/* Modified By*******************************/
// errStr.Format(L"COM open failed: Port = %s Error = %d ", szPort, dwError); // remove
// AfxMessageBox(errStr);
/*******************************************************************/
} // end if
if (dwRC && lpMsgBuf)
{
LocalFree( lpMsgBuf );
} // end if
return false;
}
else
{
bPortReady = SetupComm(m_CommPort, 256, 256); // set buffer sizes
if (!bPortReady)
{
dwError = GetLastError();
/* Modified *******************************/
// errStr.Format(L"SetupComm failed: Port = %s Error = %d", szPort, dwError); // remove
// AfxMessageBox(errStr);
/*******************************************************************/
}
bPortReady = GetCommState(m_CommPort, &dcb);
if (!bPortReady)
{
dwError = GetLastError();
/* *******************************/
// errStr.Format(L"GetCommState fai
/********************led: Port = %s Error = %d", szPort, dwError); // remove
// AfxMessageBox(errStr);***********************************************/
} // end if
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = nBaudRate;
dcb.ByteSize = ndatabits;
dcb.Parity = cParity;//NOPARITY;
dcb.StopBits = nStopbits;
dcb.fAbortOnError = TRUE;
bPortReady = SetCommState(m_CommPort, &dcb);
if (!bPortReady)
{
dwError = GetLastError();
/* *******************************/
// errStr.Format(L"SetCommState failed: Port = %s Error = %d", szPort, dwError); // remove
// AfxMessageBox(errStr);
/*******************************************************************/
}
bPortReady = GetCommTimeouts (m_CommPort, &CommTimeouts);
if (!bPortReady)
{
dwError = GetLastError();
/* Modified *******************************/
// errStr.Format(L"GetCommTimeouts failed: Port = %s Error = %d", szPort, dwError); // remove
// AfxMessageBox(errStr);
/*******************************************************************/
} // end if
CommTimeouts.ReadIntervalTimeout = 300;
CommTimeouts.ReadTotalTimeoutConstant = 1000;
CommTimeouts.ReadTotalTimeoutMultiplier = 300;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
bPortReady = SetCommTimeouts (m_CommPort, &CommTimeouts);
PurgeComm(m_CommPort,PURGE_TXCLEAR | PURGE_RXCLEAR);
if (!bPortReady)
{
dwError = GetLastError();
/* Modified By *******************************/
// errStr.Format(L"SetCommTimeouts failed: Port = %s Error = %d", szPort, dwError); // remove
// AfxMessageBox(errStr);
/*******************************************************************/
} // end if
printf("the serial initial success!");
}
m_Opened = true;
return true;
}
DWORD CSerialPort::SerialWrite(PBYTE wrBuffer, int nSize)
{
DWORD dwNumWritten;
WriteFile( m_CommPort,
wrBuffer,
nSize,
&dwNumWritten,
NULL);
return dwNumWritten;
}
DWORD CSerialPort::SerialRead(PBYTE rdBuffer, int nSiz)
{
BOOL bReadRC;
DWORD iBytesRead;
DWORD dwError;
/* Modified By *******************************/
// CString errStr; // remove
/*******************************************************************/
memset(rdBuffer, 0, sizeof(rdBuffer));
bReadRC = ReadFile(m_CommPort, rdBuffer, nSiz, &iBytesRead, NULL);
if (!bReadRC && iBytesRead <= 0)
{
dwError = GetLastError();
/* Modified By *******************************/
// errStr.Format(L"Read length failed: RC=%d Bytes read=%d, Error= %d",bReadRC, iBytesRead, dwError); // remove
// AfxMessageBox(errStr);
/*******************************************************************/
} // end if
return iBytesRead;
}
void CSerialPort::CloseSerialPort(void)
{
if (m_CommPort != INVALID_HANDLE_VALUE)
CloseHandle(m_CommPort);
m_CommPort = 0;
m_Opened = false;
}
bool CSerialPort::IsOpened(void)
{
return m_Opened;
}
//测试主程序
#include "SteAfx.h"
#include "SerialPort.h"
#include "stdio.h"
int main()
{
char wrBuffer[16];
CSerialPort PcPro;
if(PcPro.IsOpened())
PcPro.CloseSerialPort();
if(!PcPro.Init_SerialPort())
printf("you are fail!\n");
sprintf_s(wrBuffer,"%s","* 0 IR 008\r");
PcPro.SerialWrite((PBYTE)wrBuffer,11); //发送数据
PcPro.CloseSerialPort();
return 1;
}