📄 commtestdlg.cpp
字号:
if((EV_RXCHAR&wParam)!=EV_RXCHAR)
return FALSE;
//判断是否写入文件,并打开文件,将指针定在文件末尾
if(judgefile)
{
getdatafile.Open(m_savefilename,CFile::modeCreate+CFile::modeNoTruncate+CFile::modeWrite,&e);
getdatafile.SeekToEnd();
}
//将数据读入CString变量中
CString /*AcceptRead,*/StrTemp;
BYTE abIn[MAXBLOCK];
int len;
// AcceptRead.Empty();//清空CString变量,接收新读入的字符
len=ReadBlock(abIn,MAXBLOCK);
// AfxMessageBox("I have getten it!");
if(!len)
{
AfxMessageBox("读字符出错,请检查。");
return FALSE;
}
for(int i=0;i<len;i++)
{
BYTE bt=abIn[i];
if(m_ctrlHexDisplay.GetCheck())
StrTemp.Format("%02x ",bt);
else
StrTemp.Format("%c",bt);
// AcceptRead+=StrTemp;
m_strRXData+=StrTemp;
if(judgefile)
{
// getdatafile.Write(AcceptRead,AcceptRead.GetLength());
getdatafile.Write(StrTemp,1);
// getdatafile.Close();
}
}
m_rxlen+=len;
//将字符显示在指定位置
if(judgefile)
getdatafile.Close();
m_EditRXData.SetWindowText(m_strRXData);
DispStatus();
// SetEvent(hPostToWnd);//允许再次发来事件
return TRUE;
}
int CCommtestDlg::ReadBlock(BYTE *abIn, int MaxLength)
{
BOOL JudgeRead;
COMSTAT ComStat;
DWORD dwErrorFlags,dwLength;
ClearCommError(m_hCom,&dwErrorFlags,&ComStat);
if(dwErrorFlags>0)
{
AfxMessageBox("读串口错,请检查参数设置。");
PurgeComm(m_hCom,PURGE_RXABORT|PURGE_RXCLEAR);
return 0;
}
dwLength=((DWORD)MaxLength<ComStat.cbInQue?MaxLength:ComStat.cbInQue);
memset(abIn,0,MaxLength);
//如果有字符即读入
if(dwLength)
{
JudgeRead=ReadFile(m_hCom,abIn,dwLength,&dwLength,&m_osRead);//读出字符至abIn处
if(!JudgeRead)
{
//如果重叠操作未完成,等待直到操作完成
if(GetLastError()==ERROR_IO_PENDING)
{
// WaitForSingleObject(m_osRead.hEvent,INFINITE);
GetOverlappedResult(m_hCom,&m_osRead,&dwLength,TRUE);
m_osRead.Offset=0;
// m_osRead.Offset=(m_osRead.Offset+dwLength)%MAXBLOCK;
}
else
dwLength=0;
}
}
return dwLength;
}
int CCommtestDlg::WriteBlock(char *abOut, int MaxLength)
{
BOOL JudgeWrite;
COMSTAT ComStat;
DWORD dwErrorFlags,dwLength,lentest;
m_osWrite.Offset=0;
ClearCommError(m_hCom,&dwErrorFlags,&ComStat);
if(dwErrorFlags>0)
{
AfxMessageBox("写串口错!请检查参数设置。");
PurgeComm(m_hCom,PURGE_TXABORT|PURGE_TXCLEAR);
return 0;
}
// CString l;
dwLength=MaxLength;
lentest=0;
JudgeWrite=WriteFile(m_hCom,abOut,dwLength,&lentest,&m_osWrite);
// l.Format("%d",dwLength);
// AfxMessageBox(l);
if(!JudgeWrite)
{
if(GetLastError()==ERROR_IO_PENDING)
{
// AfxMessageBox("11111111");
GetOverlappedResult(m_hCom,&m_osWrite,&lentest,TRUE);
// l.Format("%ld",lentest);
// AfxMessageBox(l);
}
else
lentest=0;
}
return lentest;
}
void CCommtestDlg::OnButtenMenualsend()
{
// TODO: Add your control notification handler code here
CString filepath;
char abOut[MAXBLOCK];
int OutNum,length;
if(!m_bConnected)
{
AfxMessageBox("串口未打开!");
return;
}
m_sendbutten.EnableWindow(FALSE);
m_transfile.EnableWindow(FALSE);
m_emptytx.EnableWindow(FALSE);
m_sFilePath.GetWindowText(filepath);
memset(abOut,0,MAXBLOCK);
//判断是否文件写,若是则打开文件并写入缓冲区
if(!filepath.IsEmpty())
{
CFile fsendout;
CFileException e;
fsendout.Open(filepath,CFile::modeRead,&e);
OutNum=fsendout.Read(abOut,MAXBLOCK);
while(OutNum)//执行循环后,文件已读完。
{
//如果选择了十六进制发送,则转换为字符形式发送出去,否则直接发送字符数组
if(m_cHexSend.GetCheck())
{
//如果读到的字符中最后有个单独的数据,则将读取长度退回一
if(abOut[OutNum-1]==' '||abOut[OutNum-1]=='\r\n')
OutNum--;
if(!OutNum)
{
AfxMessageBox("读十六进制文件出错,请检查格式!");
return;
}
//将十六进制字符串转换为CString类,为十六进制转换成字符的函数作准备
CString StrHexData;
abOut[OutNum]=NULL;
StrHexData=CString(abOut);
char SendOut[MAXBLOCK];
int len=String2Hex(StrHexData,SendOut);
length=WriteBlock(SendOut,len);
}
else
length=WriteBlock(abOut,OutNum);
m_txlen+=OutNum;
OutNum=fsendout.Read(abOut,MAXBLOCK);//准备下一次循环
//continue;
}//while(OutNum)
fsendout.Close();
// if(!length)
// {
// AfxMessageBox("已写完!");
m_sendbutten.EnableWindow(TRUE);
m_transfile.EnableWindow(TRUE);
m_emptytx.EnableWindow(TRUE);
DispStatus();
return;
// }
// continue;
// return;
}//if(!filepath.IsEmpty())
//读文本框内容
CString str;
// CString strtest;
GetDlgItem(IDC_EDIT_TXDATA)->GetWindowText(str);
char SendOut[MAXBLOCK];
int len=str.GetLength();
for(int i=0;i<len;i++)
// {
abOut[i]=str.GetAt(i);
// strtest.Format("%c",abOut[i]);
// AfxMessageBox(strtest);
// }
if(m_cHexSend.GetCheck())
{
CString StrHexData;
abOut[len]=NULL;
StrHexData=CString(abOut);
len=String2Hex(StrHexData,SendOut);
length=WriteBlock(SendOut,len);
}
else
length=WriteBlock(abOut,len);
m_txlen+=length;
/* if(!length)
AfxMessageBox("无数据能写入缓冲区!");*/
m_sendbutten.EnableWindow(TRUE);
m_transfile.EnableWindow(TRUE);
m_emptytx.EnableWindow(TRUE);
DispStatus();
return;
}
int CCommtestDlg::String2Hex(CString str, char *SendOut)
{
int hexdata,lowhexdata;
int hexdatalen=0;
int len=str.GetLength();
//SendOut.SetSize(len/2);
for(int i=0;i<len;)
{
char lstr,hstr=str[i];
if(hstr==' '||hstr=='\r'||hstr=='\n')
{
i++;
continue;
}
i++;
if (i>=len)
break;
lstr=str[i];
hexdata=ConvertHexData(hstr);
lowhexdata=ConvertHexData(lstr);
if((hexdata==16)||(lowhexdata==16))
break;
else
hexdata=hexdata*16+lowhexdata;
i++;
SendOut[hexdatalen]=(char)hexdata;
hexdatalen++;
}
//senddata.SetSize(hexdatalen);
return hexdatalen;
}
char CCommtestDlg::ConvertHexData(char ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
if((ch>='A')&&(ch<='F'))
return ch-'A'+10;
if((ch>='a')&&(ch<='f'))
return ch-'a'+10;
else return(-1);
}
void CCommtestDlg::OnButtonTransfile() //选择文件按钮事件函数
{
// TODO: Add your control notification handler code here
CFile transfile;
CFileDialog mFileDlg(TRUE, NULL,NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , "文本文件(*.txt)|*.txt|All Files (*.*)|*.*| |", AfxGetMainWnd());
if(mFileDlg.DoModal()==IDOK)
{
char outputbuffer[512];
CFileException e;
BOOL file;
int outnum;
m_strTXData.Empty();//初始化显示文件窗口
m_edittxdata.SetWindowText(m_strTXData);
m_filepath=mFileDlg.GetPathName();//设置显示路径窗口
m_sFilePath.SetWindowText(m_filepath);
file=transfile.Open(m_filepath,CFile::modeRead,&e);
outnum=transfile.Read(outputbuffer,sizeof(outputbuffer)-1);
while(outnum)
{
outputbuffer[outnum]=NULL;
m_strTXData+=CString(outputbuffer);
outnum=transfile.Read(outputbuffer,sizeof(outputbuffer)-1);
}
transfile.Close();
m_edittxdata.SetWindowText(m_strTXData);
m_strTXData.Empty();
}
}
void CCommtestDlg::OnCheckAutosend()
{
// TODO: Add your control notification handler code here
m_bAutoSend=!m_bAutoSend;
if(m_bAutoSend)
SetTimer(1,1000,NULL);
else
KillTimer(1);
}
void CCommtestDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
OnButtenMenualsend();
CDialog::OnTimer(nIDEvent);
}
void CCommtestDlg::OnButtonEmptytext()
{
// TODO: Add your control notification handler code here
m_strTXData.Empty();
m_filepath.Empty();
GetDlgItem(IDC_STATIC_FILEPAHT)->SetWindowText(m_filepath);
m_edittxdata.SetWindowText(m_strTXData);
}
void CCommtestDlg::OnCheckWritetofile()
{
// TODO: Add your control notification handler code here
if(m_judgesend.GetCheck())
{
mkdir("c:\\commtest");
m_savefilename="c:\\commtest\\test1.txt";
}
else
{
m_savefilename.Empty();
}
GetDlgItem(IDC_STATIC_FILENAME)->SetWindowText(m_savefilename);
}
void CCommtestDlg::OnButtonFilename()
{
// TODO: Add your control notification handler code here
if(!m_judgesend.GetCheck())
AfxMessageBox("你未指定写入到文件项。");
else
{
CFileDialog savefiledlg(FALSE,"txt",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , "文本文件(*.txt)|*.txt| |", AfxGetMainWnd());
if(savefiledlg.DoModal()==IDOK)
{
m_savefilename=savefiledlg.GetPathName();
GetDlgItem(IDC_STATIC_FILENAME)->SetWindowText(m_savefilename);
}
}
}
void CCommtestDlg::OnButtonSaveedit()
{
// TODO: Add your control notification handler code here
CFile getdatafile;
CFileException e;
m_EditRXData.GetWindowText(m_strRXData);
if(m_savefilename.IsEmpty())
AfxMessageBox("请选择存放文件处!");
else
{
getdatafile.Open(m_savefilename,CFile::modeCreate+CFile::modeNoTruncate+CFile::modeWrite,&e);
getdatafile.Write(m_strRXData,m_strRXData.GetLength());
getdatafile.Close();
}
}
void CCommtestDlg::OnButtonEmptyrx()
{
// TODO: Add your control notification handler code here
m_strRXData.Empty();
m_EditRXData.SetWindowText(m_strRXData);
}
void CCommtestDlg::OnButtonPortstatu()
{
// TODO: Add your control notification handler code here
/* static int i=0;
int j;
j=m_ComboSeriou.GetCurSel();
if(i==j)*/
m_portstatu_but=!m_portstatu_but;
if(!m_portstatu_but)
GetDlgItem(IDC_BUTTON_PORTSTATU)->SetWindowText("串口打开");
else
GetDlgItem(IDC_BUTTON_PORTSTATU)->SetWindowText("串口关闭");
OnSelchangeComboSeriou();
return;
}
void CCommtestDlg::OnButtonClose()
{
// TODO: Add your control notification handler code here
CloseConnection();
CDialog::OnOK();
}
void CCommtestDlg::CloseConnection()
{
if(!m_bConnected)
return;
m_bConnected=FALSE;
// SetEvent(m_hPostMsgEvent);
// SetEvent(m_osRead.hEvent);
SetCommMask(m_hCom,0);
// SetEvent(m_osRead.hEvent);
// AfxMessageBox("out");
WaitForSingleObject(m_pThread->m_hThread,INFINITE);
// AfxMessageBox("yes");
m_pThread=NULL;
CloseHandle(m_hCom);
}
void CCommtestDlg::DispStatus()
{
CWnd *static_status=GetDlgItem(IDC_STATIC_PORTSTATU);
CWnd *static_txlen=GetDlgItem(IDC_STATIC_TXLEN);
CWnd *static_rxlen=GetDlgItem(IDC_STATIC_RXLEN);
CString status_str,txlen_str,rxlen_str;
if(m_bConnected)
status_str="STATUS:"+m_SeriouStr+" OPENED,"+m_BaudStr+','+m_JiaoyanStr+','+m_DataStr+','+m_StopStr;
else
status_str="STATUS:"+m_SeriouStr+" CLOSED,"+m_BaudStr+','+m_JiaoyanStr+','+m_DataStr+','+m_StopStr;
static_status->SetWindowText(status_str);
txlen_str.Format("TX:%ld",m_txlen);
static_txlen->SetWindowText(txlen_str);
rxlen_str.Format("RX:%ld",m_rxlen);
static_rxlen->SetWindowText(rxlen_str);
}
void CCommtestDlg::OnSelchangeComboBaud()
{
// TODO: Add your control notification handler code here
int baudindex;
baudindex=m_ComboBaud.GetCurSel();
if(baudindex!=CB_ERR)
{
SetCommParameter();
m_ComboBaud.GetLBText(baudindex,m_BaudStr);
DispStatus();
}
}
void CCommtestDlg::OnSelchangeComboData()
{
// TODO: Add your control notification handler code here
int dataindex;
dataindex=m_ComboData.GetCurSel();
if(dataindex!=CB_ERR)
{
SetCommParameter();
m_ComboData.GetLBText(dataindex,m_DataStr);
DispStatus();
}
}
void CCommtestDlg::OnSelchangeComboJiaoyan()
{
// TODO: Add your control notification handler code here
int jiaoyanindex;
jiaoyanindex=m_ComboJiaoyan.GetCurSel();
if(jiaoyanindex!=CB_ERR)
{
SetCommParameter();
m_ComboJiaoyan.GetLBText(jiaoyanindex,m_JiaoyanStr);
DispStatus();
}
}
void CCommtestDlg::OnSelchangeComboStop()
{
// TODO: Add your control notification handler code here
int stopindex;
stopindex=m_ComboStop.GetCurSel();
if(stopindex!=CB_ERR)
{
SetCommParameter();
m_ComboStop.GetLBText(stopindex,m_StopStr);
DispStatus();
}
}
void CCommtestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
}
void CCommtestDlg::OnButtonReset()
{
// TODO: Add your control notification handler code here
m_rxlen=0;
m_txlen=0;
DispStatus();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -