自己写了一个串口接收下位机程序,就光接收,发现数据出错,希望高手看看指导下啊。
while (Dlg->m_hSerial != INVALID_HANDLE_VALUE)
{
//等待串口的事件发生
WaitCommEvent (Dlg->m_hSerial, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
Sleep(100);
ClearCommError(Dlg->m_hSerial,&dwErrorFlags,&ComStat);
//cbInQue返回输入缓冲区中的字符数
dwLength=ComStat.cbInQue; if(dwLength>0)
{
//从串口读取数
do
{
fReadState=ReadFile(Dlg->m_hSerial,&Byte,1,&dwBytes,NULL);
if(!fReadState)
{
MessageBox(NULL,TEXT("无法读取数据"),TEXT("错误"),MB_OK);
}
if(dwBytes==1)
{ ReceiveBuf[iCounter++] = Byte;
if(iCounter == 10)
{
iCounter=0;
}
strInChar=ReceiveBuf; }
}while (dwBytes == 1);
}
}
GetCommModemStatus (Dlg->m_hSerial, &dwCommModemStatus);
}
下面是初始化串口
PortDCB.DCBlength = sizeof (DCB);
//得到端口的默认设置信息
GetCommState (m_hSerial, &PortDCB);
//改变DCB结构设置
PortDCB.BaudRate = CBR_115200; //波特率
PortDCB.fParity = FALSE; //无奇偶校验
PortDCB.fNull = TRUE; //设为TRUE将使串行驱动程序忽略收到的空字节
PortDCB.ByteSize = 8; //每字节的位数
PortDCB.Parity = NOPARITY; //无奇偶校验
PortDCB.StopBits = ONESTOPBIT; //每字节一位停止位
//根据DCB结构配置端口
if (!SetCommState (m_hSerial, &PortDCB))
{
dwError = GetLastError ();
//不能配置串行端口
MessageBox (_T("配置串口失败"), _T("错误"), MB_OK | MB_ICONWARNING);
return FALSE;
}
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(m_hSerial,&CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 50;
CommTimeouts.WriteTotalTimeoutConstant = 100;
while (Dlg->m_hSerial != INVALID_HANDLE_VALUE)
{
//等待串口的事件发生
WaitCommEvent (Dlg->m_hSerial, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
Sleep(100);
ClearCommError(Dlg->m_hSerial,&dwErrorFlags,&ComStat);
//cbInQue返回输入缓冲区中的字符数
dwLength=ComStat.cbInQue; if(dwLength>0)
{
//从串口读取数
do
{
fReadState=ReadFile(Dlg->m_hSerial,&Byte,1,&dwBytes,NULL);
if(!fReadState)
{
MessageBox(NULL,TEXT("无法读取数据"),TEXT("错误"),MB_OK);
}
if(dwBytes==1)
{ ReceiveBuf[iCounter++] = Byte;
if(iCounter == 10)
{
iCounter=0;
}
strInChar=ReceiveBuf; }
}while (dwBytes == 1);
}
}
GetCommModemStatus (Dlg->m_hSerial, &dwCommModemStatus);
}
下面是初始化串口
PortDCB.DCBlength = sizeof (DCB);
//得到端口的默认设置信息
GetCommState (m_hSerial, &PortDCB);
//改变DCB结构设置
PortDCB.BaudRate = CBR_115200; //波特率
PortDCB.fParity = FALSE; //无奇偶校验
PortDCB.fNull = TRUE; //设为TRUE将使串行驱动程序忽略收到的空字节
PortDCB.ByteSize = 8; //每字节的位数
PortDCB.Parity = NOPARITY; //无奇偶校验
PortDCB.StopBits = ONESTOPBIT; //每字节一位停止位
//根据DCB结构配置端口
if (!SetCommState (m_hSerial, &PortDCB))
{
dwError = GetLastError ();
//不能配置串行端口
MessageBox (_T("配置串口失败"), _T("错误"), MB_OK | MB_ICONWARNING);
return FALSE;
}
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(m_hSerial,&CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 50;
CommTimeouts.WriteTotalTimeoutConstant = 100;
解决方案 »
- 难道是VA搞慢了VS?还是真的电脑不行了
- 求救(关于Visual Studio2008中数据类型的问题)
- CToolBarCtrl 工具栏 背景颜色
- VC6下的小技巧,个人感觉很有用,分享
- CxImage如何打开多页TIF?
- 如何打开csdn的邮件周刊,就是经常发送信箱的那东西?
- 什么是compound document
- 谁能来教教我这个菜鸟啊,我不会生成.exe文件
- 请帮帮我,有关在视图、文档的输入和存盘的问题,50分给你了!
- Wingsun:如何将自定义消息添加到消息循环的处理中?
- 求助---实时监控计算机蜂鸣器是否发声
- MFC 里的类, 怎么知道是在什么地方被实例化的?或者有什么方法指定它只能在某个类中被实例化?
do
{
fReadState=ReadFile(Dlg->m_hSerial,&Byte,1,&ReaddwBytes,NULL);//改动这块,换成ReaddwBytes这个变量
if(!fReadState)
{
MessageBox(NULL,TEXT("无法读取数据"),TEXT("错误"),MB_OK);}
/*全部这么改
if(dwBytes==1)//这句不对,改成:
if(fReadState==1)//fReadState返回的是实际读取的字节数
{ ReceiveBuf[iCounter++] = Byte;
if(iCounter == 10)
{
iCounter=0;
}
strInChar=ReceiveBuf;}
*/
iCounter=0;
for(int i=0;i<fReadState;i++)
{
ReceiveBuf[iCounter++] = Byte;
}
if(iCounter >= 10)
{
iCounter=0;
}
strInChar=ReceiveBuf;
dwBytes=0;}while (dwBytes >0);
http://download.csdn.net/detail/schlafenhamster/4195462
是一个用线程接受或发生的。
if(!fReadStart)
{ // if there was a problem, or the async. operation's still pending ...
if(GetLastError()==ERROR_IO_PENDING)
{ // asynchronous i/o is still in progress
if (WaitErr=WaitForSingleObject(m_osRead.hEvent,60000))// 1 minute
{ // time over
if(WaitErr==WAIT_TIMEOUT)
{ // time out
dwLength=0;
AfxMessageBox("Time out !");
}// end time out
}// end waiterr
} // end io_pending
else
{ // WaitErr=0.if you SetEvent(m_osRead.hEvent) anywhere else
GetOverlappedResult(m_idComDev,&m_osRead,&dwLength,FALSE);
m_osRead.Offset +=dwLength;
}
}
接收到数据后pBlock没处理吗?
确认波特率、数据位、停止位等参数是否与设备的一致。
读到后:
uldFile.Write()就把文件写入 uldFile (UpLoadFile) 中了。并没有处理和显示。