📄 self diagnosedlg.cpp
字号:
// Self diagnoseDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Self diagnose.h"
#include "Self diagnoseDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSelfdiagnoseDlg dialog
CSelfdiagnoseDlg::CSelfdiagnoseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSelfdiagnoseDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSelfdiagnoseDlg)
m_ReadResult = _T("3.28 ");
m_WriteHigh = 0;
m_WriteLow = 0;
m_WriteAddress = 0;
m_ReadAddress = _T("60");
//}}AFX_DATA_INIT
ComNum = 1;
ComPara = _T("1200,n,8,1");
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSelfdiagnoseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSelfdiagnoseDlg)
DDX_Control(pDX, IDC_BTN_WRITE, m_CtlWrite);
DDX_Control(pDX, IDC_BTN_READ, m_CtlRead);
DDX_Control(pDX, IDC_EDIT_WRITE_SHOWADDR, m_CtlWriteAddress);
DDX_Control(pDX, IDC_EDIT_WRITE_LOW, m_CtlWriteLow);
DDX_Control(pDX, IDC_EDIT_WRITE_HIGH, m_CtlWriteHigh);
DDX_Control(pDX, IDC_EDIT_READ_SHOWRESULT, m_CtlReadResult);
DDX_Control(pDX, IDC_EDIT_READ_SHOWADDR, m_CtlReadAddress);
DDX_Control(pDX, IDC_COMBO_WRITE_ITEM, m_CtlWriteItem);
DDX_Control(pDX, IDC_COMBO_READ_ITEM, m_CtlReadItem);
DDX_Control(pDX, IDC_MSCOMM1, m_Com);
DDX_Text(pDX, IDC_EDIT_READ_SHOWRESULT, m_ReadResult);
DDX_Text(pDX, IDC_EDIT_WRITE_HIGH, m_WriteHigh);
DDX_Text(pDX, IDC_EDIT_WRITE_LOW, m_WriteLow);
DDX_Text(pDX, IDC_EDIT_WRITE_SHOWADDR, m_WriteAddress);
DDX_Text(pDX, IDC_EDIT_READ_SHOWADDR, m_ReadAddress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSelfdiagnoseDlg, CDialog)
//{{AFX_MSG_MAP(CSelfdiagnoseDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO_COM1, OnRadioCom1)
ON_BN_CLICKED(IDC_RADIO_COM2, OnRadioCom2)
ON_BN_CLICKED(IDC_RADIO_READ, OnRadioRead)
ON_BN_CLICKED(IDC_RADIO_WRITE, OnRadioWrite)
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
ON_BN_CLICKED(IDC_BTN_READ, OnBtnRead)
ON_CBN_EDITCHANGE(IDC_COMBO_READ_ITEM, OnEditchangeComboReadItem)
ON_CBN_EDITCHANGE(IDC_COMBO_WRITE_ITEM, OnEditchangeComboWriteItem)
ON_EN_CHANGE(IDC_EDIT_WRITE_HIGH, OnChangeEditWriteHigh)
ON_EN_CHANGE(IDC_EDIT_WRITE_LOW, OnChangeEditWriteLow)
ON_CBN_SELCHANGE(IDC_COMBO_READ_ITEM, OnSelchangeComboReadItem)
ON_CBN_SELCHANGE(IDC_COMBO_WRITE_ITEM, OnSelchangeComboWriteItem)
ON_BN_CLICKED(IDC_BTN_WRITE, OnBtnWrite)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSelfdiagnoseDlg message handlers
BOOL CSelfdiagnoseDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_CtlWriteItem.InsertString(0,"温度(℃)");
m_CtlWriteItem.InsertString(1,"电压(V)");
m_CtlWriteItem.InsertString(2,"发送光功率(mW)");
m_CtlWriteItem.InsertString(3,"接收光功率(mW)");
m_CtlWriteItem.SetCurSel(0);
m_CtlReadItem.InsertString(0,"温度(℃)");
m_CtlReadItem.InsertString(1,"电压(V)");
m_CtlReadItem.InsertString(2,"发送光功率(mW)");
m_CtlReadItem.InsertString(3,"接收光功率(mW)");
m_CtlReadItem.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSelfdiagnoseDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSelfdiagnoseDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSelfdiagnoseDlg::OnRadioCom1()
{
// TODO: Add your control notification handler code here
ComNum = 1;
}
void CSelfdiagnoseDlg::OnRadioCom2()
{
// TODO: Add your control notification handler code here
ComNum = 2;
}
void CSelfdiagnoseDlg::OnRadioRead()
{
// TODO: Add your control notification handler code here
m_CtlWriteAddress.EnableWindow(false);
m_CtlWriteLow.EnableWindow(false);
m_CtlWriteHigh.EnableWindow(false);
m_CtlWriteItem.EnableWindow(false);
m_CtlReadResult.EnableWindow();
m_CtlReadAddress.EnableWindow();
m_CtlReadItem.EnableWindow();
}
void CSelfdiagnoseDlg::OnRadioWrite()
{
// TODO: Add your control notification handler code here
m_CtlWriteAddress.EnableWindow();
m_CtlWriteLow.EnableWindow();
m_CtlWriteHigh.EnableWindow();
m_CtlWriteItem.EnableWindow();
m_CtlReadResult.EnableWindow(false);
m_CtlReadAddress.EnableWindow(false);
m_CtlReadItem.EnableWindow(false);
}
void CSelfdiagnoseDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
if(m_Com.GetPortOpen())
m_Com.SetPortOpen(FALSE);
m_Com.SetCommPort(ComNum); //选择com号
// m_Com.SetInBufferSize(1024);
if( !m_Com.GetPortOpen())
m_Com.SetPortOpen(TRUE);//打开串口
else
AfxMessageBox("cannot open serial port");
m_Com.SetSettings(ComPara); //波特率,校验,个数据位,个停止位
m_Com.SetInputMode(1);//二进制方式传送文件
m_Com.SetInputLen(4); //设置当前接收区数据长度,每次读取字节数
m_Com.SetRThreshold(4);
m_Com.GetInput();//先预读缓冲区以清除残留数据
}
void CSelfdiagnoseDlg::OnBtnRead()
{
// TODO: Add your control notification handler code here
m_ReadResult = _T(" ");
SendInformation[1] = _T("01");
SendInformation[2] = _T("01");
// CString temp;CString useful;
// temp.Format("%d",SendInformation[1]);
// useful.Format("%d",SendInformation[0]);
// useful += temp;
// m_Com.SetOutput(COleVariant(SendInformation[0]));
CByteArray hexdata;
int len0=String2Hex(SendInformation[0] ,hexdata);
m_Com.SetOutput(COleVariant(hexdata));
int len1=String2Hex(SendInformation[1] ,hexdata);
m_Com.SetOutput(COleVariant(hexdata));
int len2=String2Hex(SendInformation[2] ,hexdata);
m_Com.SetOutput(COleVariant(hexdata));
//发送数据
}
void CSelfdiagnoseDlg::OnBtnWrite()
{
// TODO: Add your control notification handler code here
/* SendInformation[1] = 1;
CString temp;CString useful;
useful.Format("%d",SendInformation[0]);
for(int i = 1;i < 4;i ++)
{
temp.Format("%d",SendInformation[i]);
useful += temp;
}
m_Com.SetOutput(COleVariant(useful)); */
}
BEGIN_EVENTSINK_MAP(CSelfdiagnoseDlg, CDialog)//
//{{AFX_EVENTSINK_MAP(CSelfdiagnoseDlg)
ON_EVENT(CSelfdiagnoseDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CSelfdiagnoseDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
// VARIANT input;//variant_inp;
// COleSafeArray safearray_inp;
// long len,k;
// BYTE rxdata[10]; //设置BYTE数组 An 8-bit integerthat is not signed.
//// float ShowData[7];
// input = m_Com.GetInput(); //读缓冲区
// safearray_inp = input; //VARIANT型变量转换为ColeSafeArray型变量
// len=safearray_inp.GetOneDimSize(); //得到有效数据长度
// for(k=0;k<len;k++)
// safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
// m_ReadResult = rxdata[0];
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
CString strtemp;
if(m_Com.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{
variant_inp = m_Com.GetInput(); //读缓冲区
safearray_inp = variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k); //字符型
strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
m_ReadResult = m_ReadResult + strtemp; //加入接收编辑框对应字符串
}
// m_strReceive += "\r\n";
}
UpdateData(FALSE); //更新编辑框内容
m_Com.SetInBufferCount(0);
}
void CSelfdiagnoseDlg::OnEditchangeComboReadItem()
{
// TODO: Add your control notification handler code here
}
void CSelfdiagnoseDlg::OnEditchangeComboWriteItem()
{
// TODO: Add your control notification handler code here
}
void CSelfdiagnoseDlg::OnChangeEditWriteHigh()
{
// 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();
}
void CSelfdiagnoseDlg::OnChangeEditWriteLow()
{
// 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();
}
void CSelfdiagnoseDlg::OnSelchangeComboReadItem()
{
// TODO: Add your control notification handler code here
switch(m_CtlReadItem.GetCurSel())
{
case 0:
{
m_ReadAddress = _T("60");
SendInformation[0] = m_ReadAddress;
UpdateData(false);
break;
}
case 1:
{
m_ReadAddress = _T("62");
SendInformation[0] = m_ReadAddress;
UpdateData(false);
break;
}
case 2:
{
m_ReadAddress = _T("66");
SendInformation[0] = m_ReadAddress;
UpdateData(false);
break;
}
case 3:
{
m_ReadAddress = _T("68");
SendInformation[0] = m_ReadAddress;
UpdateData(false);
break;
}
default:
break;
}
}
void CSelfdiagnoseDlg::OnSelchangeComboWriteItem()
{
// TODO: Add your control notification handler code here
/* switch(m_CtlWriteItem.GetCurSel())
{
case 0:
{
m_WriteAddress = 0;//2
SendInformation = m_WriteAddress;
UpdateData(false);
break;
}
case 1:
{
m_WriteAddress = 8;//10
SendInformation = m_WriteAddress;
UpdateData(false);
break;
}
case 2:
{
m_WriteAddress = 24;//26
SendInformation = m_WriteAddress;
UpdateData(false);
break;
}
case 3:
{
m_WriteAddress = 32;//34
SendInformation = m_WriteAddress;
UpdateData(false);
break;
}
default:
break;
}
*/
}
int CSelfdiagnoseDlg::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;
}
//这是一个将字符转换为相应的十六进制值的函数
//功能:若是在0-F之间的字符,则转换为相应的十六进制字符,否则返回-1
char CSelfdiagnoseDlg::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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -