📄 +
字号:
char StringOut[128];
sprintf(StringOut, "the key value is %d\n", pMsg->wParam);
OutputDebugString(StringOut);
switch (pMsg->wParam) //the key value
{
case 13:
//Enter
if (GetFocus() == GetDlgItem(MY_OK_BUTTON))
{
OnOKButton();
return TRUE;
}
else if (GetFocus() == GetDlgltem(MY_EXIT_BUTTON))
{
OnExitButton();
return true;
}
break;
}
}
CCommtestDlg dlg;
m_pMainWnd=&dlg;
dlg.DoModal();
m_hIcon=AfxGetAPP()->Loadlcon
m_ListBox[0].SubclassDlgItem(IDC_LIST1, this); //生成列表框,编辑框
................ //另3个列表框,编辑框也如此编写
m_Edit[0].SubclassDlgItem(IDC_EDITI, this);
................ //另3个列表框,编辑框也如此编写
for (int i; 0; i < 4; i++)
{
if (m_Ports[i].InitPort(this, i + 1, i == = 0 ? 9600 : 19200))
//初始化配置对话框
m_Ports[i].StartMonitoring();
else
{
m_Edit[i].SetWindowText("NOT FOUND");
m_Edit[i].EnableWindow(FALSE);
m_ListBox[i].EnableWindow(FALSE);
}
}
void CCommtestdlg::OnSendButtonlButton1()
{
char but[100];
memset(&buf, 0, sizeof(buf));
GetDlgItemText(IDC_ EDIT1, buf, sizeof(buf)); //得到编辑框中内容
if (strcmp("NOT FOUND", buf == 0)
return ;
if (((CButton*)
GetdlgItem(IDC_CHECK1))->GetCheck()) //得到检查框
buf[strlen(buf)] = 13; m_Ports[0].WriteToPort(buf);
//写数据到COM口
}
if (port <= 0 || port > 4)
return - 1;
if (ch == 13 && ((CButton*)GetDlgItem(IDC_ CHECKS + port - 1))
->GetCheck())
{
m_ListBox[port - 1].AddString(m_strReceived[port - 1]);
m_ListBox[port - 1].SetSel(m_ListBox[port - 1].GetCount() - 1,
TRUE);
(m_strReceived[port - 1]).Empty();
}
else if (((CButton*)GetdlgItem(IDC_CHECKS + port - 1))->GetCheck())
m_strReceived[port - 1] += (char)ch;
else
{
CString string;
string += (char)ch;
m_ListBox[port - 1].AddString(string);
m_ListBox[port - 1].SetSel(m_ListBox[port - 1].GetCount() - 1,
TRUE);
}
if (port <= 0 || port > 4)
return - 1;
CString string;
string = "Clear To Send";
m_ListBox[port - 1].AddString(string);
m_ListBox[port - 1].SetSel(m_ListBox[port - 1].GetCowit() - 1,
TRUE);
m_ListBox[0].ResetContent();
char buf[50];
CFileDialog m_mapwayopen(TRUE, NULL, NULL, NULL,
"txt文件(*.txt)|*.txt||");
CString m_wayfilename;
CFile m_nWayFile;
if (m_mapwayopen.DoModal() == IDOK)
{
m_wayfilename = m_mapwayopen.GetPathName();
m_nWayFile.Open(m_wayfilename, CFile::modeRead);
m_wayfilename += '\0';
SetDlgItemText(IDC_EDIT1, m_wayfilename); //得到编辑框中内容
int len = m_nWayFile.Read(buf, 50);
while (len)
{
m_Ports[0].WriteTbPort(buf);
len = m_nWayFile.Read(buf, 50);
}
}
CConfigDlg::CConfigDlg(CWnd *pParent, DCB dcb): CDialog(CConfigDlg
::IDD, pParent)
{
m_dcb = dcb;
}
CString strTemp;
strTemp.Format("%d", m_dcb.BaudRate); //显示在对话框中
((CComboBox*)GetDlgItem(IDqBAUDRATECOMBO))->SelectString(0,
strTemp);
((CComboBox*)GetDlgItem(IDC - PARIIYCOMBO))->SetCurSel
(m_dcb.Parity);
((CComboBox*)GetDlgItem(IDC_STOPBITSCOMBO))->SetCurSel
(m_dcb.StopBits);
strTemp.Format("%d", m_dcb.ByteSize);
((CComboBox*)GetdlgItem(IDC_DATABITSCOMBO))->SelectString(0,
strTemp);
m_hComm=NULL;
m_ov.Offset=0;
m_ov.OffsetHigh=0;
m_ov.hEvent=NULL;
m_hWriteEvent"NULL;
m_ShutdownEvent=NULL;
m_szWriteBuffer=NULL;
m_bThreadAlive=FALSE;
do
{ SetEvent(m_hShutdownEvent);
}while (m_bThreadAlive);
TRACE("Thread ended\n");
delete [] m_szWriteBuffer;
UINT portnr, // portnumber (1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR、EV_CTS等
UINT writebuffersize) // size to the writebuffer
assert(portnr > 0 && portnr < 5);
assert(pPortOwner != NULL);
if (m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
}
while (m_bThreadAlive);
TRACE("Thread ended\n");
}
if (m_ov.hEvent != NULL)
ResetEvent(m_ov.hEvent);
m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_hWriteEvent != NULL)
ResetEvent(m_hWriteEvent);
m_hWriteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_hEventArray[0] = m_hShutdownEvent;
m_hEventArray[1] = m_ov.hEvent;
m_hEventArray[2] = m_hWriteEvent;
m_pOwner = pPortOwner;
if (m_szWriteBuffer != NULL)
delete []m_szWriteBuffer;
m_szWriteBuffer = new char[writebuffersize];
m_nPortNr = portnr;
m_nWriteBufferSize = writebuffersize;
m_dwCommEvents = dwCommEvents;
EnterCriticalSection(&m_csCommunicationSync);
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
sprintf(szPort, "COM%d", portnr);
sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity,
databits, stopbits);
m_hComm = CreateFile
(
szPort,
GENERICes_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0
);
m_CommTimeouts.ReadIntervalTimeout = 1000;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
if (SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if (SetCommMask(m_hComm, dwCommEvents))
{
if (GetCommState(m_hComm, &m_dcb))
{
m_dcb.fRtsControl = RTS_CONTROL_ENABLE;
if (BuildCommDCB(szBaud, &m_dcb))
{
if (SetCommState(m_hComm, &m_dcb))
else
ProcessErrorMessage("SetCommState()");
}
else
ProcessErrorMessage("BuildCommDCB()");
}
else
ProcessErrorMessage("GetCommState()");
}
else
ProcessErrorMessage("SetCommMask()");
else
ProcessErrorMessage("SetCommTimeouts()");
delete []szPort;
delete []szBaud PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR
| PURGE_RXABORT | PURGE_TXABORT);
LeaveCriticalSection(&m_csCommunicationSync);
TRACE(
"Initial isation for communicationport %d completed.\nUse Startmonitor to communicate. \n", portnr);
if (!(m_Thread = AfxBeginThread(CommThread, this)))
return FALSE;
TRACE("Thread started\n");
TRACE("Thread resumed\n");
m_Thread->ResumeThread();
FormatMessage
(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID
(LANGesNEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &1pMsgBuf,
0,
NULL
);
sprintf(Temp,
"WARNING: %s Failed with the following error: \n%s\nPort:%d\n",
(char*)ErrorText, 1pMsgBuf, m_nPortNr);
MessageBox(NULL, Temp, "Application Error", MB_ICONSTOP);
ResetEvent(port->m_hWriteEvent);
EnterCriticalSection(&port->m_csCommunicationSync);
if (bWrite)
{
port->m_ov.Ofset = 0;
port->m_ov.OffsetHigh = 0;
PurgeComm(port->m_hComm, PURGE_RXCLEAR | PURGE_RXABORT |
PURGE_TXABORT);
bResult = WriteFile(port->m_hComm, port->m_szWriteBuffer, strlen
((char*)port->m_szWriteBuffer), &BytesSent, &port->m_ov);
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
BytesSent = 0;
Write = FALSE;
break;
}
default:
{
port->ProcessErrorMessage("WriteFile()");
}
}
}
else
{
LeaveCriticalSection(&port->m_csCommunicationSync);
}
}
if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(port->m_hComm, &port->m_ov,
&BytesSent, TRUE);
LeaveCriticalSection(&port->m_csCommunicationSync);
if (!bResult)
{
port->ProcessErrorMessage(
"GetOverlappedResults() in WriteFile()");
}
}
if (BytesSent != strlen((char*)port->m_szWriteBuffer))
{
TRACE(
"WARNING: WriteFileQ error.. Bytes Sent: %d; Message Length: % d \ n ", BytesSent, strlen((char*)port->m_szWriteBuffer));
}
assert(m_hComm != 0);
memset(m_szWriteBuffer, 0, sizeof(m_szWriteBuffer));
strcpy(m_szWriteBuffer, string);
SetEvent(m_hWriteEvent);
CCommtestDlg dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -