问题描述
- VS2013 C++串口编程,接收刷卡器数据,调试显示字符串中的字符无效
-
C++代码:
hcom = CreateFile(_T("COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL, NULL);
if (hcom == INVALID_HANDLE_VALUE)
{
return;
}
SetupComm(hcom, 1024, 1024);
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout = 1000;
TimeOuts.ReadTotalTimeoutMultiplier = 5;
TimeOuts.ReadTotalTimeoutConstant = 50;
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier = 500;
TimeOuts.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(hcom, &TimeOuts); //设置超时
DCB dcb;
GetCommState(hcom, &dcb);
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = TWOSTOPBITS;
SetCommState(hcom, &dcb);
PurgeComm(hcom, PURGE_TXCLEAR | PURGE_RXCLEAR);
char str[100];
DWORD wCount=0;//读取的字节数
BOOL bReadStat=false;
bReadStat = ReadFile(hcom, str, 100, &wCount, NULL);
if (!bReadStat)
{
MessageBox(_T("读串口失败!"));
return;
}
调试图片:
解决方案
你试试我用的
static HANDLE hCom = NULL;
static OVERLAPPED m_osRead;
static OVERLAPPED m_osWrite;
static DCB dcb;
#define BAUD_RATE 9600
int com_open( int comindex )
{
char comname[8];
memset(comname,0, sizeof(comname));
sprintf(comname,"COM%d", comindex);
hCom = CreateFileA(comname, GENERIC_READ|GENERIC_WRITE, //允许读写
0, //值必须为0,表示不将串口与其他应用程序共享
NULL, // no security attrs
OPEN_EXISTING, // 指定如何打开文件,在打开设备(串口是一种设备)时,此参数必须指定为OPEN_EXISTING
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, //指定文件属性及相关标志,但是对于串行口,唯一有意义的设置是FILE_FLAG_OVERLAPPED或0
NULL);
if( hCom == (HANDLE)-1 )
{
return 0;
}
GetCommState(hCom, &dcb ); //读串口原来的参数设置
dcb.BaudRate = BAUD_RATE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT ;
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
SetCommState( hCom, &dcb ); //串口参数配置
SetCommMask(hCom, EV_RXCHAR|EV_TXEMPTY );//设置事件驱动的类型
SetupComm( hCom, 1024,1024) ; //设置输入、输出缓冲区的大小
PurgeComm( hCom, PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR|PURGE_RXCLEAR);//清干净输入、输出缓冲区
COMMTIMEOUTS TimeOuts;
//设置读超时
TimeOuts.ReadIntervalTimeout=1;
TimeOuts.ReadTotalTimeoutMultiplier=1;
TimeOuts.ReadTotalTimeoutConstant=1;
//设置写超时
TimeOuts.WriteTotalTimeoutMultiplier=1;
TimeOuts.WriteTotalTimeoutConstant=1;
SetCommTimeouts(hCom,&TimeOuts); //设置超时
memset(&m_osRead,0,sizeof(OVERLAPPED));
m_osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
memset(&m_osWrite,0,sizeof(OVERLAPPED));
m_osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
//check
bool ret = CheckConnect(300);
if( ret <= 0 )
{
CloseHandle(hCom);
hCom = NULL;
return 0;
}
return 1;
}
int ReceiveData( unsigned char *buff_Receive )
{
COMSTAT ComStat;
DWORD dwErrorFlags;
DWORD dwBytesRead=1024;//读取的字节数
BOOL bReadStat=FALSE;
ClearCommError(hCom,&dwErrorFlags,&ComStat);
if(!ComStat.cbInQue)
return 0;
dwBytesRead=min(dwBytesRead,(DWORD)ComStat.cbInQue);
bReadStat=ReadFile(hCom,buff_Receive,dwBytesRead,&dwBytesRead,&m_osRead);
if(!bReadStat)
{
if(GetLastError()==ERROR_IO_PENDING)
//GetLastError()函数返回ERROR_IO_PENDING,表明串口正在进行读操作
{
WaitForSingleObject(m_osRead.hEvent,10);
//使用WaitForSingleObject函数等待,直到读操作完成或延时已达到2秒钟
//当串口读操作进行完毕后,m_osRead的hEvent事件会变为有信号
}
}
// PurgeComm(hCom, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
return dwBytesRead;
}
解决方案二:
com_open中的CheckConnect可以直接去掉
时间: 2024-10-21 13:48:38