📄 comm.cpp
字号:
#include "stdafx.h"
#include "CommApp.h"
#include "CommAppDlg.h"
#include "Comm.h"
#include <windows.h>
#include <winbase.h>
#include <winnls.h>
extern HANDLE hSer;
extern HANDLE g_hevWriteEnable;
//int nLineCount = 0;
extern CEdit *pm_Edit;
extern CEdit *pm_Send;
extern CEdit *pm_Status;
//////////////////////////////////////////////////////////////////////////////////
void CommInit(CString IndexText, unsigned long Baudrate)
{
DCB dcb;
hSer = CreateFile( IndexText,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL );
if( hSer == INVALID_HANDLE_VALUE )
{
pm_Status->SetWindowText(TEXT("Open ")+IndexText+TEXT(" Failed!"));
return;
}
pm_Status->SetWindowText(TEXT("Open ")+IndexText+TEXT(" Success!"));
//set the DCB structure
dcb.DCBlength = sizeof( DCB );
GetCommState( hSer, &dcb );
dcb.fParity = FALSE;
dcb.fNull = FALSE;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.BaudRate = Baudrate;
dcb.ByteSize = 8;
// BOOL bSetCommStat =
SetCommState( hSer, &dcb );
//Set the timeout
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 10;
CommTimeOuts.ReadTotalTimeoutConstant = 10;
CommTimeOuts.WriteTotalTimeoutMultiplier = 50;
CommTimeOuts.WriteTotalTimeoutConstant = 100;
SetCommTimeouts( hSer, &CommTimeOuts );
return;
}
//Rcv Thread,Read the ComBuffer
void CommRcv(void)
{
BYTE ReadBuffer[2000] = {0};
TCHAR TReadBuffer[2000] = {0};
// BOOL bReadStatus = FALSE;
DWORD dwErrorFlags;
COMSTAT ComStat;
DWORD i;
DWORD dwBytesRead = 0;
SetCommMask(hSer, EV_RXCHAR);
while(1)
{
WaitCommEvent(hSer, NULL, NULL);
SetCommMask(hSer, EV_RXCHAR);
ClearCommError( hSer, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) continue;
// dwBytesToRead = ComStat.cbInQue;
if( !ReadFile( hSer, ReadBuffer, ComStat.cbInQue, &dwBytesRead, NULL) )
{
pm_Status -> SetWindowText(TEXT("Read COMM Failed!"));
return;
}
else
{
pm_Status -> SetWindowText(TEXT("Read COM Success!"));
MultiByteToWideChar(CP_ACP,
0,
(char *)ReadBuffer,
dwBytesRead,
TReadBuffer,
dwBytesRead);
if(TReadBuffer[0])
lstrcat(TReadBuffer, TEXT("\r\n"));
// nLineCount = ;
if( (pm_Edit->GetLineCount()) > 40)
{
// Clear the Output
pm_Edit -> SetSel(0,-1);
pm_Edit -> Clear();
}
// Now output
int nLen = pm_Edit->GetWindowTextLength();
pm_Edit -> SetFocus();
pm_Edit -> SetSel(nLen, nLen);
pm_Edit -> ReplaceSel(TReadBuffer);
// Clear the Buffer
for(i = 0; i < dwBytesRead + 2; i++)
{
ReadBuffer[i] = 0;
TReadBuffer[i] = 0;
}
}
}
}
//Trans Thread
void CommTrans(void)
{
DWORD dwBytesWrite;
char WriteBuffer[100] = {0};
TCHAR TWriteBuffer[100] = {0};
while(1)
{
if(WaitForSingleObject(g_hevWriteEnable, INFINITE) == WAIT_OBJECT_0)
{
// Write to Comm
pm_Send -> GetWindowText(TWriteBuffer, 100);
lstrcat(TWriteBuffer, TEXT("\r\n"));
WideCharToMultiByte(CP_ACP,
WC_COMPOSITECHECK|WC_DEFAULTCHAR,
TWriteBuffer,
lstrlen(TWriteBuffer),
WriteBuffer,
lstrlen(TWriteBuffer),
NULL,
NULL);
if(WriteFile(hSer, (char *)WriteBuffer, lstrlen(TWriteBuffer)*sizeof(char), &dwBytesWrite, NULL))
{
pm_Status -> SetWindowText(TEXT("Write COM Success!"));
}
else
{
DWORD i = GetLastError();
pm_Status -> SetWindowText(TEXT("Write COM Failed!"));
}
}
else
break;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -