📄 conmunication.cpp
字号:
}
void CConmunication::OnBUTTONClose()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
void CConmunication::OnRadio1()
{
// TODO: Add your control notification handler code here
bFile=TRUE;
bVerify=FALSE;
CButton *pWnd=(CButton *)GetDlgItem(IDC_BUTTON_Send);
pWnd->EnableWindow(TRUE);
times=0;
m_ConEdit.ShowWindow(SW_HIDE);
m_Label.ShowWindow(SW_HIDE);
CFileDialog CflDlg(TRUE,"*.bin","",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"二进制文件 (*.bin)|*.bin||",this);
CflDlg.m_ofn.lStructSize=sizeof(OPENFILENAME);
CflDlg.m_ofn.lpstrInitialDir="e:\\temp";
CString str1;
int n=CflDlg.DoModal();
if(n==IDOK)
{
strFileName=CflDlg.GetPathName();
file.Open(strFileName,CFile::modeRead|CFile::typeBinary|CFile::shareExclusive,NULL);
size=file.Read(bindata,sizeof(bindata));
FileLength=file.GetLength();
InitProgress(0,FileLength*600);
CButton *pWnd=(CButton *)GetDlgItem(IDC_RADIO1);
pWnd->SetCheck(FALSE);
}
CByteArray reArr;
if(reArr.GetSize()!=0)
reArr.RemoveAll();
ControlByte=ControlByte | 0x07;
reArr.Add(ControlByte); //给数组赋值
m_MsComm.SetRThreshold(1);
m_MsComm.SetSThreshold(0);
COleVariant varOut;
varOut=COleVariant(reArr); //将数据转换为变体数据类型
m_MsComm.SetOutput (varOut);
str1="要发送的文件为:";
str1=str1+strFileName;
m_ShowReceive=str1;
UpdateData(FALSE);
}
void CConmunication::InitProgress(int begin,int max)
{
m_Progress.SetRange(begin,max);
m_Progress.SetPos(0);
m_Progress.ShowWindow(SW_SHOW);
}
BOOL CConmunication::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization he
ControlByte=0;
bSended=FALSE;
UpdateData(FALSE);
CButton *pWnd;
pWnd=(CButton *)GetDlgItem(IDC_RADIO4);
pWnd->SetCheck(TRUE);
pWnd=(CButton *)GetDlgItem(IDC_RADIO6);
pWnd->SetCheck(TRUE);
pWnd=(CButton *)GetDlgItem(IDC_BUTTON_Send);
pWnd->EnableWindow(FALSE);
bReady=FALSE;
bSending=FALSE;
ReSend=FALSE;
bThread=TRUE;
m_ConEdit.ShowWindow(SW_HIDE);
m_Label.ShowWindow(SW_HIDE);
bFile=FALSE;
bHand=FALSE;
m_Rate="9600,N,8,1";
m_MsComm.SetOutBufferSize(20);
m_MsComm.SetInBufferSize(30);
m_MsComm.SetCommPort(1);
if(!m_MsComm.GetPortOpen())
m_MsComm.SetPortOpen(TRUE);
else
AfxMessageBox("不能打开串口1!");
m_MsComm.SetSettings(m_Rate);
m_MsComm.SetRThreshold(1);
m_MsComm.SetSThreshold(0);
m_MsComm.SetInputLen(0);
m_MsComm.SetInputMode(1);
m_Close.ShowWindow(SW_HIDE);
m_Send.ShowWindow(SW_HIDE);
m_Clear.ShowWindow(SW_HIDE);
/* COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.WriteTotalTimeoutConstant=50;
CommTimeOuts.ReadTotalTimeoutConstant=50;
CommTimeOuts.ReadIntervalTimeout=0x00000FFF;
CommTimeOuts.ReadTotalTimeoutMultiplier=0;
CommTimeOuts.WriteTotalTimeoutMultiplier=0;
SetCommTimeouts(m_MsComm.m_hWnd, &CommTimeOuts);*/
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BEGIN_EVENTSINK_MAP(CConmunication, CDialog)
//{{AFX_EVENTSINK_MAP(CConmunication)
ON_EVENT(CConmunication, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CConmunication::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
CByteArray reArr;
if(m_MsComm.GetCommEvent()==2)
{
Sleep(1000);
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG k;
BYTE rxdata[200];
for(int j=0;j<200;j++)
rxdata[j]=0;
variant_inp=m_MsComm.GetInput(); //读缓冲区
safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
CString str;
m_ShowReceive="";
if(bVerify)
{
if(len<=4)
{
if(rxdata[0]==0x08 && rxdata[1]==0x08 && rxdata[2]==0x08)
{
m_MsComm.SetRThreshold(3);
if(reArr.GetSize()!=0)
reArr.RemoveAll();
for(int j=0;j<FileLength;j++)
reArr.Add(bindata[j]);
m_MsComm.SetOutput(COleVariant(reArr));
if(FileLength<16)
AfxMessageBox("数据发送完毕!");
}
else if(rxdata[0]==0x09 && rxdata[1]==0x09 && rxdata[2]==0x09)
m_MsComm.SetOutput(COleVariant(reArr));
else if(rxdata[0]==0x0A && rxdata[1]==0x0A && rxdata[2]==0x0A)
{
m_MsComm.SetRThreshold(FileLength);
if(reArr.GetSize()!=0)
reArr.RemoveAll();
for(int j=0;j<3;j++)
reArr.Add(0x0A);
m_MsComm.SetOutput(COleVariant(reArr));
}
else
AfxMessageBox("通信错误,请重发数据!");
}
else
{
for(int i=0;i<len-1;i++)
{
if(bindata[i]==rxdata[i])
{
m_Progress.SetPos((i+times*16)*600);
Sleep(600);
UpdateData(FALSE);
}
else
{
AfxMessageBox("校验错误!");
if(reArr.GetSize()!=0)
reArr.RemoveAll();
for(int j=0;j<3;j++)
reArr.Add(0x09); //给数组赋值,重发数据
m_MsComm.SetOutput(COleVariant(reArr));
m_MsComm.SetRThreshold(3);
m_Progress.SetPos(times*16*600);
break;
}
}//结束循环
if(i==len-2 && !bFinish)
{
FileLength=file.Read(bindata,sizeof(bindata));
if(FileLength<16)
bFinish=TRUE;
Sleep(1000);
for(int j=0;j<3;j++)
reArr.Add(0x08); //给数组赋值,准备发送数据
m_MsComm.SetOutput(COleVariant(reArr));
m_MsComm.SetRThreshold(3);
times++;
}
}
}
else //bVerify==FALSE
{
if(rxdata[0]==ControlByte && len<3)
{
bVerify=TRUE;
CByteArray reArr;
if(reArr.GetSize()!=0)
reArr.RemoveAll();
for(int j=0;j<3;j++)
reArr.Add(0x08); //给数组赋值,控制字节正确
m_MsComm.SetOutput(COleVariant(reArr));
m_MsComm.SetRThreshold(3);
}
else if(rxdata[0]==rxdata[1] && rxdata[1]==rxdata[2] && rxdata[0]==0x07)
{
if(MessageBox("传输错误!重试吗?","警告",MB_OK)==MB_OK)
{
if(reArr.GetSize()!=0)
reArr.RemoveAll();
reArr.Add(ControlByte); //给数组赋值
m_MsComm.SetRThreshold(1);
m_MsComm.SetSThreshold(0);
COleVariant varOut;
varOut=COleVariant(reArr); //将数据转换为变体数据类型
m_MsComm.SetOutput (varOut);
}
}
else
{
if(reArr.GetSize()!=0)
reArr.RemoveAll();
for(int j=0;j<3;j++)
reArr.Add(0x07); //给数组赋值,重发控制字节
m_MsComm.SetOutput(COleVariant(reArr));
m_MsComm.SetRThreshold(3);
}
}
}
}
void CConmunication::OnRadio2()
{
// TODO: Add your control notification handler code here
bHand=TRUE;
times=0;
CButton *pWnd=(CButton *)GetDlgItem(IDC_BUTTON_Send);
pWnd->EnableWindow(TRUE);
//m_MsComm.SetSThreshold(0);
//m_MsComm.SetRThreshold(0);
m_ShowReceive="请填写发送内容!";
UpdateData(FALSE);
m_ConEdit.ShowWindow(SW_SHOW);
m_Label.ShowWindow(SW_SHOW);
m_Close.ShowWindow(SW_SHOW);
m_Send.ShowWindow(SW_SHOW);
m_Clear.ShowWindow(SW_SHOW);
}
void CConmunication::OnOutofmemoryProgress1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CConmunication::OnRadio3()
{
// TODO: Add your control notification handler code here
/* If Me.lockbit1.Value Then
controlbyte = &H90
Else
If Me.lockbit12.Value = True Then
controlbyte = &HA0
Else
If Me.lockbit123.Value = True Then
controlbyte = &HB0
Else
controlbyte = &H80
End If
End If
End If
frmMain.startaddr = setfrm.set_cstartaddr
frmMain.endaddr = setfrm.set_cendaddr
setfrm.Text2 = Hex(frmMain.startaddr)
setfrm.Text3 = Hex(frmMain.endaddr)*/
//mpu
if(bLock1)
ControlByte=0x90;
else
if (bLock12)
ControlByte=0xA0;
else
if (bLock123)
ControlByte=0xB0;
else
ControlByte=0x80;
}
void CConmunication::OnRadio4()
{
// TODO: Add your control notification handler code here
//FLASH
ControlByte=0;
}
void CConmunication::OnRadio5()
{
// TODO: Add your control notification handler code here
//AUTO
if(bLock1)
ControlByte=0xD0;
else
if (bLock12)
ControlByte=0xE0;
else
if (bLock123)
ControlByte=0xF0;
else
ControlByte=0xC0;
}
void CConmunication::OnRadio6()
{
// TODO: Add your control notification handler code here
ControlByte = ControlByte & 0xCF;
}
void CConmunication::OnRadio7()
{
// TODO: Add your control notification handler code here
bLock1=TRUE;
ControlByte = (ControlByte & 0xCF) | 0x10;
}
void CConmunication::OnRadio8()
{
// TODO: Add your control notification handler code here
bLock12=TRUE;
ControlByte = (ControlByte & 0xCF) | 0x20;
}
void CConmunication::OnRadio9()
{
// TODO: Add your control notification handler code here
bLock123=TRUE;
ControlByte = (ControlByte & 0xCF) | 0x30;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -