📄 wzdprtio.cpp
字号:
// PortIO.cpp: implementation of the CPortIO class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WzdPrtIO.h"
CString message;
//////////////////////////////////////////////////////////////////////
// Open Serial Port
//////////////////////////////////////////////////////////////////////
BOOL CWzdPortIO::OpenCOM(int n, CFileException *e, int baud, int parity, int databits, int stopbits)
{
CString portName;
portName.Format("COM%d:",n);
if (Open( portName, CFile::modeReadWrite, e))
{
DCB dcb;
::GetCommState( (HANDLE)m_hFile, &dcb );
if (baud!=-1) dcb.BaudRate = baud;
if (databits!=-1) dcb.ByteSize = databits;
if (stopbits!=-1) dcb.StopBits = stopbits;
if (parity!=-1) dcb.Parity = parity;
::SetCommState( (HANDLE)m_hFile, &dcb );
return(TRUE);
}
return(FALSE);
}
//////////////////////////////////////////////////////////////////////
// Send to Port
//////////////////////////////////////////////////////////////////////
void CWzdPortIO::Send(LPSTR lpBuf, int len)
{
// initialize the structure we will pass to thread
m_SendData.pFile=this;
m_SendData.lpBuf=lpBuf;
m_SendData.len=len;
// start the thread
AfxBeginThread(SendThread,&m_SendData);
}
UINT SendThread( LPVOID pParam )
{
// get data from thread creator
SENDDATA *pSend=(SENDDATA *)pParam;
::PurgeComm((HANDLE)pSend->pFile->m_hFile,PURGE_RXCLEAR);
// do the write
pSend->pFile->Write( pSend->lpBuf, pSend->len);
return 0;
}
//////////////////////////////////////////////////////////////////////
// Listen to Port
//////////////////////////////////////////////////////////////////////
void CWzdPortIO::Listen(int hdrSz, CWnd *pWnd, UINT msg)
{
// cancel timeouts! we want to wait forever until next message comes in
COMMTIMEOUTS cto;
::GetCommTimeouts( (HANDLE)m_hFile, &cto );
cto.ReadIntervalTimeout = 0;
cto.WriteTotalTimeoutMultiplier = 0;
cto.WriteTotalTimeoutConstant = 0;
::SetCommTimeouts( (HANDLE)m_hFile, &cto );
// initialize the structure we will pass to thread
m_RecvData.pFile=this;
m_RecvData.hdrSz=hdrSz;
m_RecvData.pWnd=pWnd;
m_RecvData.msg=msg;
// start the thread
AfxBeginThread(RecvThread,&m_RecvData);
}
UINT RecvThread( LPVOID pParam )
{
// get data from thread creator
RECVDATA *pRecv=(RECVDATA *)pParam;
char *pHdr=new char[pRecv->hdrSz];
pHdr[pRecv->hdrSz-1]='\0';
int i;
int j;
int len;
int error;
CString hex;
while(TRUE)
{
try
{
len=pRecv->pFile->Read(pHdr,pRecv->hdrSz-1);
// int temp;
// temp=0;
// for(i=0;i<pRecv->hdrSz-2;i++)
// {
// temp+=pHdr[i];
// }
// temp=temp % 256;
// if(temp==pHdr[pRecv->hdrSz-2])
// {
message="";
for(j=0;j<16;j++)
{
for(i=0;i<14;i++)
{
hex.Format("%02x",pHdr[j*14+i]);
hex=hex.Right(2);
message+=hex;
message+=" ";
}
message+="\r";
}
hex.Format("%02x",pHdr[224]);
hex=hex.Right(2);
message+=hex;
// TRACE("\nMessage:%s",message);
pRecv->pWnd->PostMessage(pRecv->msg,1);
// }
}
catch (CFileException *e)
{
error=e->m_cause;
e->Delete();
}
}
return 0;
}
CString CWzdPortIO::GetValue()
{
return(message);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -