wince5.0串口读写问题

puyoupuyou90 2009-10-26 09:32:38
我要在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

}







...全文
137 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
puyoupuyou90 2009-10-29
  • 打赏
  • 举报
回复
没人踩么?
puyoupuyou90 2009-10-27
  • 打赏
  • 举报
回复
我的代码写有没有问题啊?
puyoupuyou90 2009-10-26
  • 打赏
  • 举报
回复
自己先顶了~~~~

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧