📄 dtedlg.cpp
字号:
BYTE szTmp[1];
int iSize = sizeof(szTmp);
memset(szTmp, 0, sizeof(szTmp));
//清除错误事件
ClearCommError(m_hCommPort, &dwErrorFlags, &ComStat);
while(ComStat.cbInQue > 0)
{
ResetEvent(ovRead.hEvent);
if(!::ReadFile(m_hCommPort, szTmp, sizeof(szTmp), &dwBytesRead, &ovRead))
{
abContine = FALSE;
break;
}
switch(SerialReceiveState)
{
case SearchFrameBegin://search this frame first c0
ReadRecvBufferIndex = 0;
if(szTmp[0] == C0CHAR)
{
SerialReceiveState = RecvHeadFrame;
ReadRecvBufferIndex = 0;
}
else
{
SerialReceiveState = SearchFrameBegin;
ReadRecvBufferIndex=0;
dwBytesRead = 0;
}
break;
case RecvHeadFrame: //接收数据的头
if(szTmp[0] == C0CHAR)
{
ReadRecvBufferIndex = 0;
SerialReceiveState = RecvHeadFrame;
}
else
{
SerialReceiveState = RecvData;
SerialReceiveStateTemp = RecvData;
Protoal[ReadRecvBufferIndex] = szTmp[0];
ReadRecvBufferIndex++;
if (szTmp[0] == DBCHAR)
{
SerialReceiveStateTemp = RecvDBCHAR;
}
}
break;
case RecvData: //接收数据
if (szTmp[0]!= C0CHAR)
{
if (SerialReceiveStateTemp == RecvDBCHAR)
{
switch (szTmp[0])
{
//字符的转义工作
case DCCHAR:
Protoal[ReadRecvBufferIndex-1] = C0CHAR;
SerialReceiveStateTemp = RecvData;
break;
case DDCHAR:
Protoal[ReadRecvBufferIndex-1] = DBCHAR;
SerialReceiveStateTemp = RecvData;
break;
default:
Protoal[ReadRecvBufferIndex] = szTmp[0];
ReadRecvBufferIndex++;
if(szTmp[0] == DBCHAR)
SerialReceiveStateTemp = RecvDBCHAR;
break;
}
}
else
{
Protoal[ReadRecvBufferIndex] = szTmp[0];
ReadRecvBufferIndex++;
if(szTmp[0] == DBCHAR)
SerialReceiveStateTemp=RecvDBCHAR;
}
}
else
{
SerialReceiveState = SearchFrameBegin;
//处理数据
m_CanDeal = TRUE;
}
break;
default: break;
}
ClearCommError(m_hCommPort, &dwErrorFlags, &ComStat);
}
}while(0);
CloseHandle(ovRead.hEvent);
LeaveCriticalSection(&m_csLock);
ResetEvent(ov.hEvent);
}
break;
}
}
return 0;
}
//---------------------------------------------------------------
//功能:
//
//-----------------------------------------------------------------
void InvalidateHandle(HANDLE &hHandle)
{
hHandle = INVALID_HANDLE_VALUE;
}
//-------------------------------------------------------------------
//
//
//--------------------------------------------------------------------
void CloseAndCleanHandle(HANDLE &hHandle)
{
BOOL bRet;
bRet = CloseHandle(hHandle);
if(!bRet)
ASSERT(0);
InvalidateHandle(hHandle);
}
//---------------------------------------------------
//
//--------------------------------------------------
void Init()
{
m_abIsConnected = FALSE;
m_CanDeal = FALSE;
InitializeCriticalSection(&m_csLock);
//不需要人工重置
m_hThreadTerm = CreateEvent(0, true, 0 , 0 );
}
//---------------------------------------------------
//
//--------------------------------------------------
void ENDSerial()
{
m_abIsConnected = FALSE;
m_CanDeal = FALSE;
DeleteCriticalSection(&m_csLock);
SetEvent(m_hThreadTerm);
// SignalObjectAndWait(m_hThreadTerm, m_hRecvThread, INFINITE, FALSE);
// CloseAndCleanHandle(m_hRecvThread);
CloseAndCleanHandle(m_hThreadTerm);
CloseAndCleanHandle(m_hCommPort);
memset(&Protoal, 0, sizeof(Protoal));
}
//----------------------------------------------------------
//功能:打开串口
//
//----------------------------------------------------------------
BOOL OpenConnection(CString szPortName, DWORD dwBaudRate, BYTE byByteSize,
CString byParity, CString byStopBit)
{
m_hCommPort = CreateFile(szPortName,
GENERIC_READ | GENERIC_WRITE,
0, //不共享COM口
NULL, //无安全策略
OPEN_EXISTING, //打开已有的文件
FILE_FLAG_OVERLAPPED, //I/O重叠模式
0);
if(m_hCommPort == INVALID_HANDLE_VALUE)
{
AfxMessageBox("这个串口没有装备或者已经被其他设备占用了!");
return FALSE;
}
//COM口设置掩码参数
if(!::SetCommMask(m_hCommPort, EV_RXCHAR))
{
AfxMessageBox("串口掩码设置错误!");
return FALSE;
}
//COM口的通信参数设置
DCB dcb;
if (!::GetCommState (m_hCommPort,&dcb))
return FALSE;
dcb.BaudRate = dwBaudRate;//波特率
dcb.ByteSize = byByteSize;//数据尺寸
if(byParity == "None")
dcb.Parity = NOPARITY; //校验选择
else if(byParity == "Odd")
dcb.Parity = ODDPARITY;
else
dcb.Parity = EVENPARITY;
if(byStopBit == "1")
dcb.StopBits = ONESTOPBIT;//终止位
else if(byStopBit == "2")
dcb.StopBits = TWOSTOPBITS;
else
dcb.StopBits = ONE5STOPBITS;
if (!::SetCommState (m_hCommPort,&dcb))
{
AfxMessageBox("串口通信参数设置错误");
return FALSE;
}
//设置串口的缓冲区
SetupComm(m_hCommPort, 1024 ,1024);
//设置串口超时参数
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
if(!SetCommTimeouts(m_hCommPort, &timeouts))
{
AfxMessageBox("串口超时状态设置错误!");
return FALSE;
}
//清除缓冲区
PurgeComm(m_hCommPort, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
m_abIsConnected = TRUE;
return TRUE;
}
//------------------------------------------------------------------
//功能:关闭串口
//
//-------------------------------------------------------------
BOOL CloseConnection()
{
return TRUE;
}
//------------------------------------------------------------------
//功能:是否连接
//
//-------------------------------------------------------------
BOOL IsConnection()
{
return m_abIsConnected;
}
//------------------------------------------------------------------
//功能:组成SLIP报文
//
//-------------------------------------------------------------
void SerialWriteData(HANDLE &HComm, const unsigned char *msg, DWORD size)
{
UINT16 i;
UINT16 j;
UINT16 sendindex = 0;
if(INVALID_HANDLE_VALUE == HComm)
{
AfxMessageBox("串口无效!");
return;
}
unsigned char *sendbuffer = new unsigned char[2048];
sendbuffer[sendindex] = C0CHAR;
sendindex = (sendindex+1);
for (i=0; i<size; i++)
{
j=msg[i];
if (j==C0CHAR)
{
sendbuffer[sendindex] = DBCHAR;
sendindex++;
sendbuffer[sendindex] = DCCHAR;
sendindex++;
}
else if (j==DBCHAR)
{
sendbuffer[sendindex] = DBCHAR;
sendindex++;
sendbuffer[sendindex] = DDCHAR;
sendindex++;
}
else
{
sendbuffer[sendindex] = msg[i];
sendindex++;
}
}
sendbuffer[sendindex] = C0CHAR;
//发送数据
WRITEProtocol(HComm, sendbuffer, sendindex+1);
delete(sendbuffer);
}
//------------------------------------------------------------------
//功能:发送报文
//
//-------------------------------------------------------------
void WRITEProtocol(HANDLE &HComm, const unsigned char *data, DWORD dwSize)
{
int iRet = 0;
OVERLAPPED ov;
DWORD dwBytesWritten = 0;
DWORD dwSent = 0;
DWORD dwErrorFlags;
COMSTAT ComStat;
memset(&ov, 0, sizeof(ov));
if(INVALID_HANDLE_VALUE == HComm)
{
AfxMessageBox("串口无效!");
return;
}
ov.hEvent = CreateEvent(0, true, 0, 0);
iRet = WriteFile(HComm, data, dwSize, &dwBytesWritten, &ov);
if(!iRet)
{
if(GetLastError() == ERROR_IO_PENDING)
{
while(!GetOverlappedResult(HComm, &ov, &dwBytesWritten, TRUE))
{
DWORD dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
{
dwSent += dwBytesWritten;
continue;
}
else
{
ClearCommError(HComm, &dwErrorFlags, &ComStat);
break;
}
}
dwSent += dwBytesWritten;
if(dwSent != dwSize)
AfxMessageBox("发送的字节数不够!");
}
else
{ //一些其他错误
ClearCommError(HComm, &dwErrorFlags, &ComStat);
CloseHandle(ov.hEvent);
return;
}
}
CloseHandle(ov.hEvent);
return;
}
void CDTEDlg::OnClearrecvbutton()
{
// TODO: Add your control notification handler code here
EnterCriticalSection(&m_csLock);
aStr.Empty();
LeaveCriticalSection(&m_csLock);
m_recvedit.SetSel(0,-1);
m_recvedit.ReplaceSel("");
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -