16,472
社区成员
发帖
与我相关
我的任务
分享
//我把串口的打开和线程的创建放在初始化函数中
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
}