wince5.0串口读写问题
我要在wince5.0上实现一个串口转发的程序,就是将一个COM7上收到的数据转发到COM6,COM6收到的数据转发到COM7,一个字节一个字节转发就可以,但非常的慢,我用SetCommMask(),WaitCommEvent(),ClearCommError(),这几个函数,就不起作用串口的数据有时候会接收不到,谁有什么好办法么?
我把实现的代码贴出来:
我把实现的代码贴出来:
//我把串口的打开和线程的创建放在初始化函数中
BOOL CTransServiceDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
pDlg = this ;
Stat = m_com7.SerOpen(EXTERNAL_COMPORT, 115200, NOPARITY, 8);
sbuf.Format(L"open return: %02x",Stat);
pDlg->m_EditResp.ReplaceSel(sbuf);
if(Stat != 0)
{
sbuf.Format(L"open port 7 failed");
pDlg->m_EditResp.ReplaceSel(sbuf);
}
Stat = m_com6.SerOpen(USIP_COMPORT, 115200, NOPARITY, 8);
sbuf.Format(L"open return: %02x",Stat);
pDlg->m_EditResp.ReplaceSel(sbuf);
if(Stat != 0)
{
sbuf.Format(L"open port 6 failed");
pDlg->m_EditResp.ReplaceSel(sbuf);
}
h_usip_sr = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)usip_SendReceive,
NULL, 0, &dwThreadId_h_usip_sr);
if(!h_usip_sr)
AfxMessageBox(_T("usip send receive线程创建失败"));
h_pda_sr = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)pda_SendReceive,
NULL, 0, &dwThreadId_h_pda_sr);
if(!h_pda_sr)
AfxMessageBox(_T("usip send receive线程创建失败"));
return TRUE; // return TRUE unless you set the focus to a control
}
//com6转发到com7
void WINAPI usip_SendReceive(LPVOID lpParam)
{
#if 0
//这里原来是一个字节一个字节转发的 可以实现但非常的慢
BYTE ch,iret;
while(1)
{
iret = m_com6.ReceiveByte(&ch, 20);
if(iret!=0)
{
Sleep(1);
continue;
}
//Lib_Lcdprintf(" -%c- ",ch);
m_com7.SerWrite(&ch,1,20);
}
#else
//这里后来改的 看看有什么问题没有
COMSTAT cmState;
DWORD evtMask=0;
DWORD dwReadErrors;
int iret ;
int temp;
SetCommMask(m_com6.m_CommPort, EV_RXCHAR);
byte buff[4096];
memset(buff,0x00,4096);
m_com6.ResetPort();
while(TRUE)
{
Sleep(1);
if(WaitCommEvent(m_com6.m_CommPort,&evtMask,NULL))
{
ClearCommError(m_com6.m_CommPort,&dwReadErrors,&cmState);
if (cmState.cbInQue >0)
{
temp = cmState.cbInQue;
iret = m_com6.SerRead(buff,temp,20);
//SaveSend(buff, temp);
iret = m_com7.SerWrite(buff,temp,20);
memset(buff,0x00,4096);
}
}
}
#endif
}
//com7转发到com6
void WINAPI pda_SendReceive(LPVOID lpParam)
{
#if 0
BYTE ch,iret;
while(1)
{
iret = m_com7.ReceiveByte(&ch, 20);
if(iret!=0)
{
Sleep(1);
continue;
}
m_com6.SerWrite(&ch,1,20);
}
#else
COMSTAT cmState;
DWORD evtMask=0;
DWORD dwReadErrors;
int temp;
int iret ;
SetCommMask(m_com7.m_CommPort, EV_RXCHAR );
byte buff[4096];
memset(buff,0x00,4096);
while(TRUE)
{
Sleep(1);
if(WaitCommEvent(m_com7.m_CommPort,&evtMask,NULL))
{
ClearCommError(m_com7.m_CommPort,&dwReadErrors,&cmState);
if (cmState.cbInQue > 0)
{
temp = cmState.cbInQue;
iret = m_com7.SerRead(buff,temp,20);
iret = m_com6.SerWrite(buff,temp,20);
memset(buff,0x00,1024);
}
}
}
#endif
}
...全文
相关推荐
WINCE6.0下的串口通讯
**错误处理**:在实际开发中,需要对可能出现的错误进行处理,如打开串口失败、设置参数错误、读写超时等问题,确保程序的健壮性。7.
wince5_test1.rar_wince5_串口
标题中的“wince5_test1.rar_wince5_串口”表明这是一个关于Windows CE 5.0(简称WinCE5)操作系统的测试项目,重点涉及串口通信的示例。
labview编写wince6.0兼容性串口
5. **缓冲区管理**:合理设置读写缓冲区大小,以优化数据传输性能。6. **关闭串口**:在程序结束或异常时,记得正确关闭串口,释放系统资源。
wince6.0串口通讯的测试基本程序
同时,这样的基础程序也可以作为调试工具,帮助排查硬件或通信协议上的问题。总之,串口通信在WinCE 6.0系统中的实现涉及多个环节,从初始化、配置、读写操作到错误处理,每个步骤都需要细心处理。"
wince6.0 cp2102 usb转串口驱动
5. **串口通信**: 安装驱动后,用户可以通过WinCE 6.0的串口API(如CreateFile、WriteFile、ReadFile等)与通过CP2102连接的外部设备进行数据交换。


