SettingCmmDlg.cpp
上传用户:asikq0571
上传日期:2014-07-12
资源大小:528k
文件大小:5k
源码类别:
Internet/IE编程
开发平台:
Visual C++
- // SettingCmmDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Peugeot.h"
- #include "SettingCmmDlg.h"
- #include "MainFrm.h"
- #include "PeugeotDoc.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CSettingCmmDlg dialog
- CSettingCmmDlg::CSettingCmmDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CSettingCmmDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CSettingCmmDlg)
- m_CmmType = 0;
- m_BaudRate = 5;
- m_StopBits = 0;
- m_ByteSize = 4;
- m_Parity = 2;
- m_sPortName = _T("");
- m_TCPIPPrtType = -1;
- m_RemoteIPPort = 0;
- m_LocalIPPort = 0;
- //}}AFX_DATA_INIT
- m_sRemoteIPAddr = _T("");
- m_sLocalIPAddr = _T("");
- }
- void CSettingCmmDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CSettingCmmDlg)
- DDX_Control(pDX, IDC_LOCAL_IPADDR, m_LocalIPAddr);
- DDX_Control(pDX, IDC_REMOTE_IPADDR, m_RemoteIPAddr);
- DDX_Control(pDX, IDC_PortName, m_cPortName);
- DDX_Radio(pDX, IDC_CMMTYPE, m_CmmType);
- DDX_Radio(pDX, IDC_BAUDRATE, m_BaudRate);
- DDX_Radio(pDX, IDC_STOPBITS, m_StopBits);
- DDX_Radio(pDX, IDC_BYTESIZE, m_ByteSize);
- DDX_Radio(pDX, IDC_PARITY, m_Parity);
- DDX_CBString(pDX, IDC_PortName, m_sPortName);
- DDX_Radio(pDX, IDC_TCP_IP_TYPE, m_TCPIPPrtType);
- DDX_Text(pDX, IDC_REMOTE_IPPORT, m_RemoteIPPort);
- DDV_MinMaxUInt(pDX, m_RemoteIPPort, 1, 9999);
- DDX_Text(pDX, IDC_LOCAL_IPPORT, m_LocalIPPort);
- DDV_MinMaxUInt(pDX, m_LocalIPPort, 1, 9999);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CSettingCmmDlg, CDialog)
- //{{AFX_MSG_MAP(CSettingCmmDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CSettingCmmDlg message handlers
- BOOL CSettingCmmDlg::OnInitDialog()
- {
- CMainFrame* pMainFrame;
- pMainFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd; //框架窗口指针
- CPeugeotDoc* pDoc;
- pDoc = (CPeugeotDoc*) pMainFrame->GetActiveDocument(); //文档指针
- m_CmmType = pDoc->m_CmmPortType;
- //读取COMM对象当前参数(波特率、校验方式、数字位、停止位)
- m_StopBits = pDoc->m_ComObj.m_nStopBits;
- m_ByteSize = pDoc->m_ComObj.m_nDataBits-4; //because dbc.bytesize (4~8)
- m_Parity = pDoc->m_ComObj.m_nParity;
- switch ( pDoc->m_ComObj.m_nBaudRate ) {
- case 300:
- m_BaudRate = 0;
- break;
- case 600:
- m_BaudRate = 1;
- break;
- case 1200:
- m_BaudRate = 2;
- break;
- case 2400:
- m_BaudRate = 3;
- break;
- case 4800:
- m_BaudRate = 4;
- break;
- case 9600:
- m_BaudRate = 5;
- break;
- case 14400:
- m_BaudRate = 6;
- break;
- case 19200:
- m_BaudRate = 7;
- break;
- case 38400:
- m_BaudRate = 8;
- break;
- case 56000:
- m_BaudRate = 9;
- break;
- case 57600:
- m_BaudRate = 10;
- break;
- case 115200:
- m_BaudRate = 11;
- break;
- default:
- m_BaudRate = 5;
- break;
- }
- //网络口参数
- m_TCPIPPrtType = pDoc->m_NetParam.TCPPrtType;
- m_RemoteIPPort = pDoc->m_NetParam.RemoteIPPort;
- m_LocalIPPort = pDoc->m_NetParam.LocalIPPort;
- CDialog::OnInitDialog();
- m_RemoteIPAddr.SetWindowText(pDoc->m_NetParam.RemoteIPAddr);
- m_LocalIPAddr.SetWindowText(pDoc->m_NetParam.LocalIPAddr);
- int i;
- CString s1 = pDoc->m_ComObj.m_szPortName;
- CString s2 = "";
- for (i=1; i<12; i++)
- {
- s2.Format("COM%d",i);
- if (CheckComm(s2) || s2 == s1)
- m_cPortName.AddString(s2);
- }
- m_cPortName.SelectString(-1,s1); //设置当前串口
- return TRUE;
- /*
- for (i=1;i<12;i++)
- {
- pDoc->m_ComObj.m_szPortName.Format("COM%d",i);
- if ( (pDoc->m_ComObj.OpenPort()) || (pDoc->m_ComObj.m_szPortName==s1) )
- m_cPortName.AddString(pDoc->m_ComObj.m_szPortName);
- pDoc->m_ComObj.ClosePort();
- }
- */
- // pDoc->m_ComObj.m_szPortName = s1; //按‘CANCLE’后,串口类的当前串口不变。
- }
- void CSettingCmmDlg::OnOK()
- {
- // TODO: Add extra validation here
- //::::OnOK()后,控件变量的环境销毁,无法再调用GetWindowText
- m_RemoteIPAddr.GetWindowText(m_sRemoteIPAddr);
- m_LocalIPAddr.GetWindowText(m_sLocalIPAddr);
- CDialog::OnOK();
- }
- bool CSettingCmmDlg::CheckComm(CString sPortName)
- {
- HANDLE hCom;
- hCom = INVALID_HANDLE_VALUE;
- int nTryTime = 1;
- while(nTryTime < 4)
- {
- hCom = CreateFile( _T("\\.\")+sPortName,
- GENERIC_READ|GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- FILE_FLAG_OVERLAPPED,
- NULL
- );
- if(hCom != INVALID_HANDLE_VALUE)
- {
- CloseHandle(hCom);
- return true;
- }
- nTryTime++;
- Sleep(10);
- }
- return false;
- }