comdlg.cpp
来自「本程序是通过串口」· C++ 代码 · 共 803 行 · 第 1/2 页
CPP
803 行
return 0;
}
//串口消息函数(自己建立的消息)
LRESULT CComDlg::OnCommNotify(WPARAM wParam, LPARAM lParam)
{
if( (!m_bConnected) || (wParam&EV_RXCHAR)!=EV_RXCHAR)
{
SetEvent(m_hPostMsgEvent); //允许允许发送下一个信息
return 0L;
}
//开始接收数据
int nLength;
char buf[4096/4];
CString str;
nLength=ReadComm(buf,100); //获取数据和长度
if(nLength)
{
for(int i=0;i<nLength;i++)
str+=buf[i];
m_receive.ReplaceSel(str);
}
SetEvent(m_hPostMsgEvent); //允许发送下一个信息
return 0L;
}
//读取串口数据,返回长度。
//输入参数:要读的长度
//注意事项:输入的要读的长度如果比实际缓冲里面的数据量大的话,返回的是较小的值
DWORD CComDlg::ReadComm(char *buf, DWORD dwLength)
{
DWORD length=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
length=min(dwLength, ComStat.cbInQue); //看上面的注意事项
ReadFile(h_com,buf,length,&length,&m_osRead);
return length;
}
void CComDlg::OnButton4()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("a");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnButton5()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("b");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnButton2()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("c");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnButton6()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("d");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnButton7()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("e");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnLeft()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("l");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnRight()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("r");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnMiddle()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("s");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnForword()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("y");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnBackoff()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("z");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
void CComDlg::OnStop()
{
// TODO: Add your control notification handler code here
COMSTAT ComStat;
ULONG nLength=0;
CString send("f");
UpdateData(true);
ULONG szLength = send.GetLength();
char *sz = new char[szLength];
if(sz==NULL)
return;
memcpy(sz,send.GetBuffer(szLength),szLength);
DWORD dwErrorFlags;
ClearCommError(h_com,&dwErrorFlags,&ComStat);
BOOL fState=WriteFile(h_com,sz,szLength,&nLength,&m_osWrite);
if(!fState)
{
ULONG my_error=GetLastError();
if(my_error==ERROR_IO_PENDING)
{
GetOverlappedResult(h_com,&m_osWrite,&nLength,TRUE);
}
else
nLength=0;
}
delete[] sz;
sz=NULL;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?