📄 ultrasonicview.cpp
字号:
// m_openserial.SetWindowText("打开串口");
m_ctrlComm.SetPortOpen(FALSE);
// }
// else
// {
if (com==0)
{
m_ctrlComm.SetCommPort(1); //选择com1
m_com="COM1";
}
else
{
m_ctrlComm.SetCommPort(2); //选择com2
m_com="COM2";
}
m_bandrate =9600;
m_data = 8;
m_parity = 0;
m_stop = 1;
if(m_realorhis==0)
{ m_chnow="";
if(IsS1==true) m_chnow+=" 南端_板1";
if(IsS2==true) m_chnow+=" 南端_板2";
if(IsS3==true) m_chnow+=" 南端_板3";
if(IsS4==true) m_chnow+=" 南端_板4";
if(IsN1==true) m_chnow+=" 北端_板1";
if(IsN2==true) m_chnow+=" 北端_板2";
if(IsN3==true) m_chnow+=" 北端_板3";
if(IsN4==true) m_chnow+=" 北端_板4";
if(IsCH1==true) m_chnow+="_通道1 ";
if(IsCH2==true) m_chnow+="_通道2 ";
if(IsCH3==true) m_chnow+="_通道3 ";
if(IsCH4==true) m_chnow+="_通道4 ";
if(IsCH5==true) m_chnow+="_通道5 ";
if(IsCH6==true) m_chnow+="_通道6 ";
if(IsCH7==true) m_chnow+="_通道7 ";
if(IsCH8==true) m_chnow+="_通道8 ";
if(IsCH9==true) m_chnow+="_速度 ";
}
UpdateData(FALSE);
if( !m_ctrlComm.GetPortOpen())
m_ctrlComm.SetPortOpen(TRUE);//打开串口
else
AfxMessageBox("cannot open serial port");
// m_openserial.SetWindowText("关闭串口");
m_ctrlComm.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位
m_ctrlComm.SetInputMode(1); // 以二进制方式检取数据
m_ctrlComm.SetRThreshold(1); //参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
m_ctrlComm.SetInputLen(0); //设置当前接收区数据长度为0
m_ctrlComm.GetInput();//先预读缓冲区以清除残留数据
// }
if(IsCH1==true) m_CH.SetCurSel(0);
if(IsCH2==true) m_CH.SetCurSel(1);
if(IsCH3==true) m_CH.SetCurSel(2);
if(IsCH4==true) m_CH.SetCurSel(3);
if(IsCH5==true) m_CH.SetCurSel(4);
if(IsCH6==true) m_CH.SetCurSel(5);
if(IsCH7==true) m_CH.SetCurSel(6);
if(IsCH8==true) m_CH.SetCurSel(7);
if(IsCH9==true) m_CH.SetCurSel(8);
}
void CUltrasonicView::OnSendcommand()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); //读取编辑框内容
CByteArray hexdata;
int len=String2Hex(m_strTXData,hexdata); //此处返回的len可以用于计算发送了多少个十六进制数
m_ctrlComm.SetOutput(COleVariant(hexdata)); //发送十六进制数据
}
int CUltrasonicView::String2Hex(CString str, CByteArray &senddata)
{
int hexdata,lowhexdata;
int hexdatalen=0;
int len=str.GetLength();
senddata.SetSize(len/2);
for(int i=0;i<len;)
{
char lstr,hstr=str[i];
if(hstr==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
lstr=str[i];
hexdata=ConvertHexChar(hstr);
lowhexdata=ConvertHexChar(lstr);
if((hexdata==16)||(lowhexdata==16))
break;
else
hexdata=hexdata*16+lowhexdata;
i++;
senddata[hexdatalen]=(char)hexdata;
hexdatalen++;
}
senddata.SetSize(hexdatalen);
return hexdatalen;
}
char CUltrasonicView::ConvertHexChar(char ch)
{
if((ch>='0')&&(ch<='9'))
return ch-0x30;
else if((ch>='A')&&(ch<='F'))
return ch-'A'+10;
else if((ch>='a')&&(ch<='f'))
return ch-'a'+10;
else return (-1);
}
int CUltrasonicView::Char2Int(char ch0,char ch1)
{
int rxint;
int highint,lowint;
switch(ch0)
{
case '0': highint=0;break;
case '1': highint=1;break;
case '2': highint=2;break;
case '3': highint=3;break;
case '4': highint=4;break;
case '5': highint=5;break;
case '6': highint=6;break;
case '7': highint=7;break;
case '8': highint=8;break;
case '9': highint=9;break;
case 'A': highint=10;break;
case 'B': highint=11;break;
case 'C': highint=12;break;
case 'D': highint=13;break;
case 'E': highint=14;break;
case 'F': highint=15;break;
default:
AfxMessageBox("1数据格式错误!");
return(-1);
}
switch(ch1)
{
case '0': lowint=0;break;
case '1': lowint=1;break;
case '2': lowint=2;break;
case '3': lowint=3;break;
case '4': lowint=4;break;
case '5': lowint=5;break;
case '6': lowint=6;break;
case '7': lowint=7;break;
case '8': lowint=8;break;
case '9': lowint=9;break;
case 'A': lowint=10;break;
case 'B': lowint=11;break;
case 'C': lowint=12;break;
case 'D': lowint=13;break;
case 'E': lowint=14;break;
case 'F': lowint=15;break;
default:
AfxMessageBox("2数据格式错误!");
return(-1) ;
}
rxint=highint*16+lowint;
return rxint;
}
void CUltrasonicView::OnViewdataNow()
{
// TODO: Add your command handler code here
ShellExecute(NULL,NULL,_T("notepad.exe"), _T(strf),NULL,SW_SHOWNORMAL);
}
void CUltrasonicView::OnSelchangeCh()
{
// 通道选择
int nIndex=m_CH.GetCurSel();
CUltrasonicDoc* pDoc=GetDocument();
if(nIndex!=CB_ERR)
switch(nIndex)
{
case 0: channel=0;break;
case 1: channel=1;break;
case 2: channel=2;break;
case 3: channel=3;break;
case 4: channel=4;break;
case 5: channel=5;break;
case 6: channel=6;break;
case 7: channel=7;break;
case 8: channel=8;break;
}
CRect rc(1,260,935,516);//45,845
InvalidateRect(rc);
}
void CUltrasonicView::OnBUTTONClearRX()
{
// 清空接收区
for(int i=0;i<9;i++) {rxnum[i]=0;rxoldnum[i]=0;}
m_Progress.SetPos(0);
Allnum=0;
numx=0;
*rxnum=0;
*rxoldnum=0;
m_strRXData="";
numx=0;
channelnum=0;
m_chnow="";
m_strRXData = _T("");
m_strTXData = _T("");
CRect rc(45,260,845,516);
InvalidateRect(rc);
UpdateData(FALSE);
}
void CUltrasonicView::OnRealTimeWave()
{
// 查看实时波形
realorhis=m_realorhis=0;
GetDlgItem(IDC_zoom)->EnableWindow(true);
GetDlgItem(IDC_zoom_up)->EnableWindow(true);
GetDlgItem(IDC_zoom_down)->EnableWindow(true);
Onzoom();
CRect rc(45,260,845,516);
InvalidateRect(rc);
UpdateData(FALSE); //更新编辑框内容
}
void CUltrasonicView::OnHistoryWave()
{
// 查看历史波形
realorhis=m_realorhis=1;//置实时/历史标志
m_chnow="";//清空通道使用栏
GetDlgItem(IDC_zoom)->EnableWindow(true);
GetDlgItem(IDC_zoom_up)->EnableWindow(true);
GetDlgItem(IDC_zoom_down)->EnableWindow(true);
int i,j,l;
for(i=0;i<8;i++)
for(j=0;j<33000;j++)
RxDataOld[i][j]=-1;
for(i=0;i<8;i++) rxoldnum[i]=0;
int cha;
char header1[100],header2[100];
int chx[8];
char ch1,ch2,ch3;
numx=0;
/////////////打开历史数据文件////////////////////
LPCTSTR lpszFilter="文本文件(*.txt)|*.txt|任何文件|*.*||";
CFileDialog dlg(TRUE,lpszFilter,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,lpszFilter,NULL);
CString a;
if(dlg.DoModal() == IDOK)
{
a = dlg.GetPathName();
}
m_path=a;
UpdateData(FALSE);
CString b;
if(a.Find("板1")>=5) b="新板1参数.txt";
else
if(a.Find("板2")>=5) b="新板2参数.txt";
else
if(a.Find("板3")>=5) b="新板3参数.txt";
else
if(a.Find("板4")>=5) b="新板4参数.txt";
else
{AfxMessageBox("文件名没包含板号信息,无法显示!"); return;}
/////////////写入各通道对应数组////////////////////
fstream file2;
char cha1,cha2,cha3,cha4;
if(b=="") AfxMessageBox("请先选择板号文件!");
else
{
b=CurrentDirectory+"\\"+b;
file2.open(b,ios::in);
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk1=(cha1-48)*100+(cha2-48)*10+cha3-48;
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk2=(cha1-48)*100+(cha2-48)*10+cha3-48;
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk3=(cha1-48)*100+(cha2-48)*10+cha3-48;
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk4=(cha1-48)*100+(cha2-48)*10+cha3-48;
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk5=(cha1-48)*100+(cha2-48)*10+cha3-48;
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk6=(cha1-48)*100+(cha2-48)*10+cha3-48;
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk7=(cha1-48)*100+(cha2-48)*10+cha3-48;
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk8=(cha1-48)*100+(cha2-48)*10+cha3-48;
file2.read(&cha1,1);
file2.read(&cha2,1);
file2.read(&cha3,1);
file2.read(&cha4,1); //cha4为空格
m_kk9=(cha1-48)*100+(cha2-48)*10+cha3-48;
UpdateData(FALSE);
}
file2.close();
/////////////写入各通道对应数组////////////////////
fstream file1;
if(a=="") AfxMessageBox("请先选择数据文件!");
else
{
file1.open(a,ios::in);
file1.getline((char*)header1,80,'\n');
file1.getline((char*)header2,80,'\n');
for(j=4;j<80 && header1[j]!='\n';j++) m_chnow+=header1[j]; //显示使用通道
int chnum=header2[7]-48;//获取通道数目
for(i=0,l=8;i<chnum;i++)
{
chx[i]=header1[l]-49;
l+=6;
}
UpdateData(FALSE);
while(!file1.eof())
{
int c=numx%chnum;
cha=chx[c];
file1.read(&ch1,1);
file1.read(&ch2,1);
file1.read(&ch3,1);
RxDataOld[cha][rxoldnum[cha]]=Char2Int(ch1,ch2);
rxoldnum[cha]++;
numx++;
if(numx>=650000) break;
}
}
file1.close();
switch(cha)
{
case 0: channel=0;m_CH.SetCurSel(0);break;
case 1: channel=1;m_CH.SetCurSel(1);break;
case 2: channel=2;m_CH.SetCurSel(2);break;
case 3: channel=3;m_CH.SetCurSel(3);break;
case 4: channel=4;m_CH.SetCurSel(4);break;
case 5: channel=5;m_CH.SetCurSel(5);break;
case 6: channel=6;m_CH.SetCurSel(6);break;
case 7: channel=7;m_CH.SetCurSel(7);break;
case 8: channel=8;m_CH.SetCurSel(8);break;
}
Onzoom();
CRect rc(1,260,935,516);//45,845
InvalidateRect(rc);
}
void CUltrasonicView::OnSetParameter()
{
// TODO: Add your command handler code here
ParaDlg paradlg;
paradlg.DoModal();
}
void CUltrasonicView::OnViewwaveCh1()
{
// TODO: Add your command handler code here
WaveDlg* pdlg=new WaveDlg;
pdlg->Create(IDD_DIALOG_WAVE,this);
pdlg->ShowWindow(SW_SHOW);
pdlg->SetWindowText("通道一波形");
pdlg->DisplayWave(0);
}
void CUltrasonicView::OnViewwaveCh2()
{
// TODO: Add your command handler code here
WaveDlg* pdlg=new WaveDlg;
pdlg->Create(IDD_DIALOG_WAVE,this);
pdlg->ShowWindow(SW_SHOW);
pdlg->SetWindowText("通道二波形");
pdlg->DisplayWave(1);
}
void CUltrasonicView::OnViewwaveCh3()
{
// TODO: Add your command handler code here
WaveDlg* pdlg=new WaveDlg;
pdlg->Create(IDD_DIALOG_WAVE,this);
pdlg->ShowWindow(SW_SHOW);
pdlg->SetWindowText("通道三波形");
pdlg->DisplayWave(2);
}
void CUltrasonicView::OnViewwaveCh4()
{
// TODO: Add your command handler code here
WaveDlg* pdlg=new WaveDlg;
pdlg->Create(IDD_DIALOG_WAVE,this);
pdlg->ShowWindow(SW_SHOW);
pdlg->SetWindowText("通道四波形");
pdlg->DisplayWave(3);
}
void CUltrasonicView::OnViewwaveCh5()
{
// TODO: Add your command handler code here
WaveDlg* pdlg=new WaveDlg;
pdlg->Create(IDD_DIALOG_WAVE,this);
pdlg->ShowWindow(SW_SHOW);
pdlg->SetWindowText("通道五波形");
pdlg->DisplayWave(4);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -