本帖最后由 bqgup 于 2018-9-9 00:26 编辑
大家先看一下完成后的上位机,我只是写了一个比较简单的,后面复杂的还请各位大佬自己开发
1、首先下载这个软件,C语言编程软件,非常经典的软件。
没有软件的请自己找安装包下载,下载好软件之后,在你电脑上你喜欢的盘建一个文件夹用来盛放自己的上位机软件文件,我把文件夹建在了桌面上,命名为“bqgup上位机”。
2、打开软件,File->New->Projects->MFC AppWizard[exe]
3、
点击上图的Insert Activex
4、View->Class Wizard->
此时点击确定即可
然后往这两个函数中分别添加代码
OnInitDialog函数代码
- OnInitDialog函数代码[/align]BOOL CCcomDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- //Add "About..." menu item to system menu.
- //IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if(pSysMenu != NULL)
- {
- Cstring strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if(!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX.strAboutMenu);
- }
- }
- // 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
-
- if(m_ctrlComm.GetPortOpen())
-
- m_ctrlComm.SetPortOpen(FALSE);
- m_ctrlComm.SetCommPort(10);
- if(!m_ctrlComm.GetPortOpen())
- m_ctrlComm.SetPortOpen(TRUE);
- else
- AfxMessageBox("cannot open serial port");
- m_ctrlComm.SetSettings("9600,n,8,1");
- m_ctrlComm.SetInputMode(1);
- m_ctrlComm.SetRThreshold(1);
- m_ctrlComm.SetInputLen(0);
- m_ctrlComm.GetInput();
- return TRUE; // return TRUE unless you set the focus to a control
- }
复制代码
OnOnCommMscomm1()函数代码
- OnOnCommMscomm1()函数代码
- void CCcomDlg::OnOnCommMscomm1()
- {
- // TODO: Add your control notification handler code here
- m_strRXData = " ";
- VARIANT variant_inp;
- COleSafeArray safearray_inp;
- LONG len,k;
- BYTE rxdata[2048];
- CString strtemp;
- if(m_ctrlComm.GetCommEvent() == 2)
- {
- variant_inp = m_ctrlComm.GetInput();
- safearray_inp = variant_inp;
- len = safearray_inp.GetOneDimSize();
- for(k = 0; k < len; k++)
- {
- BYTE bt = *(char*)(rxdata + k);
- strtemp.Format("%c",bt);
- m_strRXData += strtemp;
- }
- }
- UpdateData(FALSE);
-
- }
复制代码