wince5.0串口读写问题

puyoupuyou90 puyoupuyou90 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我要在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

}







...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
puyoupuyou90
VC/MFC 16,551 成员 421,605 内容