complDlg.cpp
上传用户:zhuozewu
上传日期:2022-07-26
资源大小:37k
文件大小:6k
- // complDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "compl.h"
- #include "complDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CComplDlg dialog
- CComplDlg::CComplDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CComplDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CComplDlg)
- m_sReseved = 0;
- m_sSend = 0;
- m_sPort = 0;
- m_nBt = 3600;
- m_nSETBT = 4;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CComplDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CComplDlg)
- DDX_Control(pDX, IDC_EDIT_SEND, m_cEditsend);
- DDX_Control(pDX, IDC_SETUPCOM, m_cSetupcomm);
- DDX_Control(pDX, IDC_SEND, m_cSend);
- DDX_Control(pDX, IDC_RESEVED, m_cReseved);
- DDX_Control(pDX, IDC_OPENCOMM, m_cOpencomm);
- DDX_Control(pDX, IDC_CLOSECOMM, m_cClosecomm);
- DDX_Text(pDX, IDC_EDIT_RESEVED, m_sReseved);
- DDX_Text(pDX, IDC_EDIT_SEND, m_sSend);
- DDX_CBIndex(pDX, IDC_COMBO_SELECT, m_sPort);
- DDX_Text(pDX, IDC_EDIT_BAUT, m_nBt);
- DDX_CBIndex(pDX, IDC_BTSETUP, m_nSETBT);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CComplDlg, CDialog)
- //{{AFX_MSG_MAP(CComplDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_RESEVED, OnReseved)
- ON_BN_CLICKED(IDC_SEND, OnSend)
- ON_BN_CLICKED(IDC_SETUPCOM, OnSetupcom)
- ON_CBN_SELCHANGE(IDC_BTSETUP, OnSelchangeBtsetup)
- ON_BN_CLICKED(IDC_CLOSECOMM, OnClosecomm)
- ON_BN_CLICKED(IDC_OPENCOMM, OnOpencomm)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CComplDlg message handlers
- BOOL CComplDlg::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
- m_cClosecomm.EnableWindow(false);
- m_cSend.EnableWindow(false);
- m_cSetupcomm.EnableWindow(false);
- m_cReseved.EnableWindow(false);
-
- // TODO: Add extra initialization here
-
- 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 CComplDlg::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);
- dc.SetBkColor(RGB(250,0,0));
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CComplDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CComplDlg::OnReseved()
- {
- // TODO: Add your control notification handler code here
- UpdateData(true);
- //CSetupc::OnSetup();
- DWORD length=0;
- unsigned ch[10];
- UpdateData(false);
-
- ReadFile(hCom,ch,1,&length,NULL);
- ch[0]=ch[0]-3435973632;
- m_sReseved=ch[0];
- SetCommMask(hCom,EV_TXEMPTY);
- UpdateData(false);
- //
- }
- void CComplDlg::OnSend()
- {
- // TODO: Add your control notification handler code here
- UpdateData(true);
- //CSetupc::OnSetup();
- DWORD length=0;
- unsigned ch[10];
- ch[0]=m_sSend;
- WriteFile(hCom,ch,1,&length,NULL);
- m_cReseved.EnableWindow(true);
- }
- void CComplDlg::OnSetupcom()
- {
- // TODO: Add your control notification handler code here
- UpdateData(true);
- // TODO: Add your control notification handler code here
- //HANDLE hCom;
- SetupComm(hCom,1024,1024);
- COMMTIMEOUTS Timeouts;
- //DCB dcb;
- Timeouts.ReadIntervalTimeout=50;
- Timeouts.ReadTotalTimeoutConstant=500;
- Timeouts.ReadTotalTimeoutMultiplier=50;
- Timeouts.WriteTotalTimeoutConstant=500;
- Timeouts.WriteTotalTimeoutMultiplier=50;
- SetCommTimeouts(hCom,&Timeouts);
- int m_nSetupbt;
- switch(m_nSETBT)
- {case 0: m_nSetupbt=1200;break;
- case 1: m_nSetupbt=2400;break;
- case 2: m_nSetupbt=3600;break;
- case 3: m_nSetupbt=4800;break;
- case 4: m_nSetupbt=9600;break;
- case 5: m_nSetupbt=19200;break;}
- GetCommState(hCom,&dcb);
- dcb.BaudRate=10000;//m_nSetupbt;
- dcb.ByteSize=8;
- dcb.StopBits=ONESTOPBIT;
- dcb.Parity=NOPARITY;
- SetCommState(hCom,&dcb);
- UpdateData(false);
- m_cSend.EnableWindow(true);
- m_cSend.SetFocus();
- m_cSetupcomm.EnableWindow(false);
- m_cEditsend.SetFocus();
- }
- void CComplDlg::OnSelchangeBtsetup()
- {
- // TODO: Add your control notification handler code here
- UpdateData(true);
- m_nBt=m_nSetupbt;
- UpdateData(false);
- }
- void CComplDlg::OnClosecomm()
- {
- // TODO: Add your control notification handler code here
- CloseHandle(hCom);
- m_cOpencomm.EnableWindow(true);
- m_cOpencomm.SetFocus();
- m_cReseved.EnableWindow(false);
- m_cSend.EnableWindow(false);
- m_cClosecomm.EnableWindow(false);
- m_cSetupcomm.EnableWindow(false);
-
- }
- void CComplDlg::OnOpencomm()
- {
- // TODO: Add your control notification handler code here
- switch(m_sPort)
- {case 0: m_strPort="COM1"; break;
- case 1: m_strPort="COM2"; break;}
- hCom=CreateFile(m_strPort,GENERIC_READ|GENERIC_WRITE,
- 0,NULL,OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- m_cClosecomm.EnableWindow(true);
- m_cSetupcomm.EnableWindow(true);
- m_cSetupcomm.SetFocus();
- m_cSend.EnableWindow(false);
- m_cOpencomm.EnableWindow(false);
- m_cReseved.EnableWindow(false);
-
- }
- void CComplDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- CComplDlg::OnClosecomm();
- CDialog::OnCancel();
- }