📄 m8dlg.cpp
字号:
for (int i = 0 ; i < n_Length;i++)
{
if (m_File.Read(n_Str.GetBuffer(2), 2) != 2) break;
n_Str.ReleaseBuffer(2);
n_Str.Remove(' ');
n_temp = HexToByte(n_Str);
m_SendData.m_HexData.SetAt(n_Count,n_temp);
n_Count++;
if ((n_Count-1) == 64 * (n_Count1+1))
{
n_Count1++;
m_SendData.m_HexPage.Add(n_Count1);
}
}
if (m_File.Read(n_Str.GetBuffer(2), 2) != 2) break; //行尾
n_Str.ReleaseBuffer(2);
n_Str.Remove(' ');
if (m_File.Read(n_Str.GetBuffer(2), 2) != 2) break; //行尾
n_Str.ReleaseBuffer(2);
n_Str.Remove(' ');
}
m_SendData.n_PageSize = 0 ;//从首页开始发送
GetDlgItem(IDC_SEND_AUTO)->EnableWindow(TRUE);
return TRUE;
}
BYTE CM8Dlg::HexToByte(CString n_ByteStr)
{
BYTE n_byte;
char ch = n_ByteStr[0];
if (ch >= '0' && ch <= '9') n_byte = ch - 0x30;
else if (ch >= 'A' && ch <= 'F') n_byte = ch - 0x37;
else if (ch >= 'a' && ch <= 'f') n_byte = ch - 0x57;
n_byte = (n_byte << 4);
ch = n_ByteStr[1];
if (ch >= '0' && ch <= '9') n_byte += ch - 0x30;
else if (ch >= 'A' && ch <= 'F') n_byte += ch - 0x37;
else if (ch >= 'a' && ch <= 'f') n_byte += ch - 0x57;
return n_byte;
}
void CM8Dlg::OnPortSet()
{
CSerialPort m_TestPort;
CString n_str,n_str1;
m_PortArray.RemoveAll();
for (int i=0; i<8; i++)
{
if (m_TestPort.InitPort(this, i+1, 9600))
{
n_str.Format(_T("COM%d"), i+1);
m_PortArray.Add(n_str);
}
}
CPortSetDlg n_Dlg;
n_Dlg.p_PortArray = &m_PortArray;
if (m_SendPortData.nPort != 0)
{
n_str.Format(_T("COM%d"), m_SendPortData.nPort);
n_Dlg.p_PortArray->InsertAt(0, n_str);
}
n_str.Format(_T("%d"), m_SendPortData.nBaud);
n_Dlg.m_Baud = n_str;
n_str.Format(_T("%c"), m_SendPortData.nParity);
n_Dlg.m_Parity = n_str;
n_str.Format(_T("%d"), m_SendPortData.nDatabits);
n_Dlg.m_DataBits = n_str;
n_str.Format(_T("%d"), m_SendPortData.nStopsbits);
n_Dlg.m_StopsBits = n_str;
if (n_Dlg.DoModal()== IDOK)
{
n_str = n_Dlg.m_Port.Right(1);
m_SendPortData.nPort = atoi(n_str);
m_SendPortData.nBaud = atoi(n_Dlg.m_Baud);
m_SendPortData.nParity = n_Dlg.m_Parity.GetAt(0);
m_SendPortData.nDatabits = atoi(n_Dlg.m_DataBits);
m_SendPortData.nStopsbits = atoi(n_Dlg.m_StopsBits);
if (nSendPort.InitPort(this, m_SendPortData.nPort, m_SendPortData.nBaud, m_SendPortData.nParity, m_SendPortData.nDatabits, m_SendPortData.nStopsbits))
{
nSendPort.StartMonitoring();
n_str = n_Dlg.m_Port + " " + n_Dlg.m_Baud + " " + n_Dlg.m_Parity + " " + n_Dlg.m_DataBits + " " + n_Dlg.m_StopsBits;
n_str1 = n_Dlg.m_Port+ " " + n_Dlg.m_Baud;
}
else
{
n_str = n_Dlg.m_Port + "初始化失败" ;
n_str1 = "串口设置";
}
SetDlgItemText(IDC_PORT_STATIC, n_str);
GetDlgItem(IDC_PORT_SET)->SetWindowText(n_str1);
}
}
void CM8Dlg::OnSendAuto()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_SEND_AUTO)->EnableWindow(false);
if (nSendPort.m_hComm == NULL)
{
::MessageBox(NULL, _T("端口没有设置!"), "告警", MB_ICONWARNING);
return;
}
if (m_ReceiveFlag) return;
switch (m_Type_B)
{
case 0://写入页
{
m_SendData.m_Type = 0x11;
m_Progress.SetPos((m_SendData.n_PageSize+1)*2);
m_Progress.ShowWindow(SW_SHOW);
}
break;
case 1://读出页
{
m_SendData.m_Type = 0x12;
m_Progress.SetPos((m_SendData.n_PageSize+1)*2 + 1);
m_Progress.ShowWindow(SW_SHOW);
}
break;
case 3://写入完毕
{
::MessageBox(NULL, _T("写入成功!"), "完成", MB_ICONWARNING);
m_Progress.ShowWindow(SW_HIDE);
return;
}
break;
case 4://写入失败
{
CString temp_str;
temp_str.Format(_T("%d"),m_SendData.n_PageSize);
::MessageBox(NULL, _T("第")+temp_str+_T("页校验失败"), "错误", MB_ICONWARNING);
m_Progress.ShowWindow(SW_HIDE);
return;
}
break;
default:
m_SendData.m_Type = 0x00;
}
if (m_SendData.Conversion())
{
nSendPort.WriteToPort(&m_SendData.m_SendBuf);
start = clock();
/*显示发送的字节*/
CString n_str;
m_SendEdit.GetWindowText(n_str);
int nLength = n_str.GetLength();
m_SendEdit.SetSel(nLength, nLength);
n_str.Empty();
for (int i=0; i<m_SendData.m_SendBuf.GetSize(); i++)
n_str += ByteToHex(m_SendData.m_SendBuf[i])+_T(" ");
m_SendEdit.ReplaceSel(n_str);
m_ReceiveFlag = TRUE; //允许接收报文
SetTimer(1, 300, 0);
}
else
{
SetTimer(2, 1, 0);
}
m_SendData.m_Type = m_Type_temp;
}
void CM8Dlg::OnTimer(UINT nIDEvent)
{
switch(nIDEvent)
{
case 1://超时
{
KillTimer(1);
KillTimer(2);
m_ReceiveCount = -1;
m_ReceiveStartCount = 0;
m_ReceiveStart = FALSE;
m_ReceiveFlag = FALSE;
::MessageBox(NULL, _T("超时!"), "超时", MB_ICONWARNING);
}
break;
case 2://写入成功
{
KillTimer(1);
KillTimer(2);
m_ReceiveFlag = false;
m_Type_B = 1;
OnSendAuto();
}
break;
case 3://读取一页成功
{
m_tt = 0;
KillTimer(1);
KillTimer(3);
m_ReceiveFlag = false;
m_SendData.n_PageSize++;
if(m_SendData.n_PageSize >= m_SendData.m_HexPage.GetSize())
{
m_Type_B = 3; //编程结束
}
else
{
for (int i =0 ; i < 64 ; i++)
{
CString J,K;
J = ByteToHex(m_ReceiveData.m_HexDataReceive.GetAt(i+1));
K = ByteToHex(m_SendData.m_HexData.GetAt((m_SendData.n_PageSize-1)*64+i));
if(J != K)
m_tt = 1 ;
}
if ( m_tt == 0)
{
m_Type_B = 0;
}
else
{
m_Type_B = 4; //编程失败
m_tt = 0;
}
}
OnSendAuto();
}
} //end of switch
CDialog::OnTimer(nIDEvent);
}
LONG CM8Dlg::OnCommunication(WPARAM ch, LPARAM port)
{
if (port != m_SendPortData.nPort) return -1;
CString n_str;//显示收到的字节
m_ReceveEdit.GetWindowText(n_str);
int nLength = n_str.GetLength();
m_ReceveEdit.SetSel(nLength, nLength);
m_ReceveEdit.ReplaceSel(ByteToHex(ch)+_T(" "));
if (!m_ReceiveFlag) return -1;
if (m_ReceiveCount == -1)
{
if (!m_ReceiveStart)
{
if (ch == 0x76 )
{
m_ReceiveStartCount++;
if (m_ReceiveStartCount >=3)
{
m_Receive.RemoveAll();
m_ReceiveStart = TRUE;
m_ReceiveStartCount = 0;
return 0;
}
}
else m_ReceiveStartCount = 0;
}
else
{
if (ch == 0x91)
{
SetTimer(2, 1, 0);
m_Receive.Add(ch);
m_ReceiveData.m_Receve = &m_Receive;
return 0;
}
if (ch == 0x92)
{
m_Receive.Add(ch);
m_ReceiveCount = 65 ;
}
}
}
else
{
if(m_ReceiveCount >0)
{
m_Receive.Add(ch);
m_ReceiveCount --;
}
if(m_ReceiveCount == 0)
{
m_ReceiveData.m_Receve = &m_Receive;
m_ReceiveData.ProcessReceving();
m_ReceiveCount = -1;
m_ReceiveStartCount = 0;
m_Receive.RemoveAll();
SetTimer(3, 1, 0);
}
}
return 0;
}
CString CM8Dlg::ByteToHex(BYTE n_byte)
{
CString n_str;
BYTE temp = n_byte >> 4;
if (temp < 10) n_str.Insert(0, (char)(temp+0x30));
else n_str.Insert(0, (char)(temp+0x37));
temp = (n_byte << 4) >> 4;
temp = n_byte%16;
if (temp < 10) n_str.Insert(1, (char)(temp+0x30));
else n_str.Insert(1, (char)(temp+0x37));
return n_str;
}
void CM8Dlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -