📄 demodlg.cpp
字号:
//将一个字符串作为十六进制串转化为一个字节数组,字节间可用空格分隔,
//返回转换后的字节数组长度,同时字节数组长度自动设置。
int CDemoDlg::Str2Hex(CString str, char *data)
{
int t,t1;
int rlen=0;//字节数组长度
int len=str.GetLength();
//data.SetSize(len/2);
for(int i=0;i<len;)
{
char l;
char h=str[i];
if(h==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
l=str[i];
t=HexChar(h);
t1=HexChar(l);
if((t==16)||(t1==16))
break;
else
t=t*16+t1;//?????
i++;
data[rlen]=(char)t;
rlen++;
}
return rlen;
}
char CDemoDlg::HexChar(char c)
{
if((c>='0')&&(c<='9'))
return c-0x30;
else if((c>='A')&&(c<='F'))
return c-'A'+10;
else if((c>='a')&&(c<='f'))
return c-'a'+10;
else
return 0x10;
}
void CDemoDlg::OnCheckAutosend()
{
// TODO: Add your control notification handler code here
m_bAutoSend=!m_bAutoSend;
if(m_bAutoSend)
{
if(m_Port.m_hComm==NULL)
{
m_bAutoSend=!m_bAutoSend;
m_ctrlAutoSend.SetCheck(0);
AfxMessageBox("串口没有打开,请打开串口");
return;
}
else
SetTimer(1,m_nCycleTime,NULL);//设置递定时器1
}
else
{
KillTimer(1);//关掉定时器
}
}
void CDemoDlg::OnChangeEditCycletime()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_CYCLETIME);
CString strText;
pEdit->GetWindowText(strText);
m_nCycleTime=atoi(strText);
}
void CDemoDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
m_ctrlAutoSend.SetCheck(0); //强行关闭自动发送
KillTimer(1); //关闭定时器
KillTimer(4);
m_Port.ClosePort(); //关闭串口
m_ReceiveData.Empty(); //清空接收数据字符串
}
void CDemoDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CString strStatus;
switch(nIDEvent)
{
case 1:
OnButtonManualsend();
break;
/*case 2:
// m_ctrlSavePath.SetWindowText(m_strCurPath);
KillTimer(2);
break;*/
case 3:
m_ctrlManualSend.EnableWindow(TRUE);
m_ctrlAutoSend.EnableWindow(TRUE);
// m_ctrlSendFile.EnableWindow(TRUE);
// m_strSendFilePathName=m_strTempSendFilePathName;
// m_ctrlEditSendFile.SetWindowText(m_strSendFilePathName);//m_strSendFilePathName
KillTimer(3);
if(!(m_ctrlAutoSend.GetCheck()))
{
if (m_Port.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
{
m_Port.StartMonitoring();
strStatus.Format("STATUS:COM%d OPENED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
m_ctrlIconOpenoff.SetIcon(m_hIconRed);
}
else
{
AfxMessageBox("Failed to reset send buffer size!");
m_ctrlIconOpenoff.SetIcon(m_hIconOff);
}
m_ctrlPortStatus.SetWindowText(strStatus);
}
break;
/* case 4:
// m_animIcon.ShowNextImage();
// break;*/
default:
break;
}
CDialog::OnTimer(nIDEvent);
}
void CDemoDlg::OnChangeEditSend()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);
}
static long rxdatacount=0; //该变量用于接收字符计数
LONG CDemoDlg::OnCommunication(WPARAM ch, LPARAM port)
{
if (port <= 0 || port > 4)
return -1;
rxdatacount++; //接收的字节计数
CString strTemp;
strTemp.Format("%ld",rxdatacount);
strTemp="RX:"+strTemp;
m_ctrlRXCOUNT.SetWindowText(strTemp); //显示接收计数
if(m_bStopDispRXData) //如果选择了“停止显示”接收数据,则返回
return -1; //注意,这种情况下,计数仍在继续,只是不显示
//若设置了“自动清空”,则达到50行后,自动清空接收编辑框中显示的数据
/*if((m_ctrlAutoClear.GetCheck())&&(m_ctrlReceiveData.GetLineCount()>=50))
{
m_ReceiveData.Empty();
UpdateData(FALSE);
}*/
//如果没有“自动清空”,数据行达到400后,也自动清空
//因为数据过多,影响接收速度,显示是最费CPU时间的操作
if(m_ctrlReceiveData.GetLineCount()>400)
{
m_ReceiveData.Empty();
m_ReceiveData="***The Length of the Text is too long, Emptied Automaticly!!!***\r\n";
UpdateData(FALSE);
}
//如果选择了"十六进制显示",则显示十六进制值
CString str;
if(m_ctrlHexReceive.GetCheck())
str.Format("%02X ",ch);
else
str.Format("%c",ch);
//以下是将接收的字符加在字符串的最后,这里费时很多
//但考虑到数据需要保存成文件,所以没有用List Control
int nLen=m_ctrlReceiveData.GetWindowTextLength();
m_ctrlReceiveData.SetSel(nLen, nLen);
m_ctrlReceiveData.ReplaceSel(str);
nLen+=str.GetLength();
m_ReceiveData+=str;
return 0;
}
void CDemoDlg::OnButtonSyn0() //同步1
{
// TODO: Add your control notification handler code here
CString str;//临时变量
str=m_strSendData;
m_strSendData = SY0 ;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(0);
/* CString strTemp;
str="同步0";
strTemp.Format("当前运动状态:%s",str);
m_ctrlControlStatus.SetWindowText(strTemp);// 显示当前运动状态
*/
}
void CDemoDlg::OnButtonSyn1() //同步1
{
// TODO: Add your control notification handler code here
// char data[512];
CString str;//临时变量
str=m_strSendData;
m_strSendData=SY1;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(1);
}
void CDemoDlg::OnButtonSyn2() //同步2
{
// TODO: Add your control notification handler code here
// char data[512];
CString str;//临时变量
str=m_strSendData;
m_strSendData=SY2;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(2);
}
void CDemoDlg::OnButtonStartmotor() //启动电机
{
// TODO: Add your control notification handler code here
// char data[512];
CString str;//临时变量
str=m_strSendData;
m_strSendData=STARTMOTOR;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(041);
}
void CDemoDlg::OnButtonUrgencystop()
{
// TODO: Add your control notification handler code here
// char data[512];
CString str;//临时变量
str=m_strSendData;
m_strSendData=URGENCYSTOP;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(055);
}
void CDemoDlg::OnButtonOnward() //前进
{
// TODO: Add your control notification handler code here
CString str;//临时变量
str=m_strSendData;
m_strSendData=ONWARD;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(111);
}
void CDemoDlg::OnButtonReverse()
{
// TODO: Add your control notification handler code here
CString str;//临时变量
str=m_strSendData;
m_strSendData=REVERSE;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(110);
}
void CDemoDlg::OnButtonTurnleft() //左转
{
// TODO: Add your control notification handler code here
CString str;//临时变量
str=m_strSendData;
m_strSendData=TURNLEFT;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(121);
}
void CDemoDlg::OnButtonTurnright()
{
// TODO: Add your control notification handler code here
CString str;//临时变量
str=m_strSendData;
m_strSendData=TURNRIGHT;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(120);
}
void CDemoDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
CString str;//临时变量
str=m_strSendData;
m_strSendData=STOP;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(29);
}
void CDemoDlg::OnButtonClosemotor() // 4号命令,关闭电机
{
// TODO: Add your control notification handler code here
// char data[512];
CString str;//临时变量
str=m_strSendData;
m_strSendData=CLOSEMOTOR;
StrtoHex(m_strSendData);
CountDisplay(m_strSendData);
m_strSendData=str;
CurrentControlStatus(040);
}
void CDemoDlg::CountDisplay(CString str)//用于显示命令的字节数
{
TX_count+=(long)((str.GetLength()+1)/3);
CString strTemp;
strTemp.Format("TX:%d",TX_count);
m_ctrlTXCount.SetWindowText(strTemp);// 显示计数
}
void CDemoDlg::OnButtonCountreset()
{
// TODO: Add your control notification handler code here
rxdatacount=0;
CString strTemp;
strTemp.Format("%ld",rxdatacount);
strTemp="RX:"+strTemp;
m_ctrlRXCOUNT.SetWindowText(strTemp);
TX_count=0;
strTemp.Format("%ld",TX_count);
strTemp="TX:"+strTemp;
m_ctrlTXCount.SetWindowText(strTemp);
}
void CDemoDlg::OnSelendokComboDatabits()
{
// TODO: Add your control notification handler code here
//char temp;
int i=m_DataBits.GetCurSel();
switch(i)
{
case 0:
i=8;
break;
case 1:
i=7;
break;
case 2:
i=6;
break;
}
m_nDatabits=i;
CString strStatus;
if (m_Port.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
{
if(!m_bOpenPort)
{
m_Port.StartMonitoring();
m_ctrlIconOpenoff.SetIcon(m_hIconRed);
strStatus.Format("STATUS:COM%d OPENED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
}
else
{
m_ctrlIconOpenoff.SetIcon(m_hIconOff);
strStatus.Format("STATUS:COM%d CLOSED,%d,%c,%d,%d",m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits);
}
m_ctrlPortStatus.SetWindowText(strStatus);
}
else
{
AfxMessageBox("没有成功,请重试");
m_ctrlIconOpenoff.SetIcon(m_hIconOff);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -