📄 dtedlg.cpp
字号:
// DTEDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DTE.h"
#include "DTEDlg.h"
#include <time.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define MAX_SIZE 2048
HANDLE m_hRecvThread;
HANDLE m_hThreadTerm;
HANDLE m_hCommPort;
BOOL m_CanDeal;
BOOL m_abIsConnected;
CRITICAL_SECTION m_csLock;
unsigned char Protoal[MAX_SIZE];
//--------------------------------------
UINT RecvThread(LPVOID pvParam);
UINT DealThread(LPVOID pvParam);
//---------------------------
extern DWORD localIP;
//SLIP转义的状态量
UINT8 SerialReceiveState = SearchFrameBegin;
UINT8 SerialReceiveStateTemp;
UINT16 ReadRecvBufferIndex = 0;
//--------------------------------------
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDTEDlg dialog
CDTEDlg::CDTEDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDTEDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDTEDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDTEDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDTEDlg)
DDX_Control(pDX, IDC_CLEARRECVBUTTON, m_clearrecvbutton);
DDX_Control(pDX, IDC_RECVRICHEDIT, m_recvedit);
DDX_Control(pDX, IDC_PKTRICHEDIT, m_pktedit);
DDX_Control(pDX, IDC_SENDBUTTON, m_sendbutton);
DDX_Control(pDX, IDC_PRICOMBO, m_pricombo);
DDX_Control(pDX, IDC_VERIFYCOMBO, m_verifycombo);
DDX_Control(pDX, IDC_BUATCOMBO, m_buatcombo);
DDX_Control(pDX, IDC_STOPCOMBO, m_stopcombo);
DDX_Control(pDX, IDC_IPADDRESS, m_IPADDRESS);
DDX_Control(pDX, IDC_SERIALBUTTON, m_serialbutton);
DDX_Control(pDX, IDC_DATACOMBO, m_datacombo);
DDX_Control(pDX, IDC_PORTCOMBO, m_portcombo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDTEDlg, CDialog)
//{{AFX_MSG_MAP(CDTEDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SERIALBUTTON, OnSerialbutton)
ON_COMMAND(ID_LOCALIP, OnLocalip)
ON_WM_TIMER()
ON_COMMAND(ID_QUERYLOCALIP, OnQuerylocalip)
ON_COMMAND(ID_PKTNUM, OnPktnum)
ON_COMMAND(ID_MTU, OnMtu)
ON_COMMAND(ID_ROUTE, OnRoute)
ON_COMMAND(ID_EFFECIP, OnEffecip)
ON_COMMAND(ID_TAKEROUTE, OnTakeroute)
ON_COMMAND(ID_EXITROUTE, OnExitroute)
ON_COMMAND(ID_DEBUGOPEN, OnDebugopen)
ON_UPDATE_COMMAND_UI(ID_DEBUGOPEN, OnUpdateDebugopen)
ON_COMMAND(ID_DEBUGOFF, OnDebugoff)
ON_UPDATE_COMMAND_UI(ID_DEBUGOFF, OnUpdateDebugoff)
ON_BN_CLICKED(IDC_SENDBUTTON, OnSendbutton)
ON_BN_CLICKED(IDC_CLEARRECVBUTTON, OnClearrecvbutton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDTEDlg message handlers
BOOL CDTEDlg::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
//初始化串口设置的一些参数:端口号、波特率等参数
UpdateData(FALSE);
//------------------初始化端口号----------------
m_portcombo.SelectString(-1, "COM1"); //选择端口
m_buatcombo.SelectString(-1, "9600"); //速率
m_datacombo.SelectString(-1, "8"); //8数据位
m_stopcombo.SelectString(-1, "1"); //1位停止位
m_verifycombo.SelectString(-1, "None");//无校验
//---------------------------------------------
m_isDebug = 0;
//------------------------------------------------
//初始化优先级
//-------------------------------------------------
m_pricombo.AddString("普通");
m_pricombo.AddString("优先");
m_pricombo.AddString("紧急");
m_pricombo.SelectString(-1, "普通");
//初始化IP地址和分组号
PacketID = 0;
memset(&OwnUserCode, 0, sizeof(OwnUserCode));
//--------------------------------------------------
//初始化RichEdit
aStr = _T("");
//初始化网络ID
IDindex = 0;
//初始化一些信息和线程
Init();
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDTEDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CDTEDlg::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 CDTEDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//-----------------------------------------------------------------------------
//初始化串口,打开串口
//------------------------------------------------------------------------------
void CDTEDlg::OnSerialbutton()
{
CString CTemp;
m_serialbutton.GetWindowText(CTemp);
if( CTemp == "打开串口")
{
CComboBox *Win = (CComboBox*)GetDlgItem(IDC_PORTCOMBO);
int i=Win->GetCurSel();
CString sCom;
Win->GetLBText(i, sCom);//获得端口号
Win = (CComboBox*)GetDlgItem(IDC_BUATCOMBO);
i = Win->GetCurSel();
CString sBaut;
DWORD Baut;
Win->GetLBText(i, sBaut); //获得波特率
Baut = atoi((const char *)sBaut);
Win = (CComboBox*)GetDlgItem(IDC_DATACOMBO);
i = Win->GetCurSel();
CString sData;
BYTE Data;
Win->GetLBText(i, sData); //获得数据位
Data = atoi((const char *)sData);
Win = (CComboBox*)GetDlgItem(IDC_STOPCOMBO);
i = Win->GetCurSel();
CString sStop;
Win->GetLBText(i, sStop); //获得停止位
Win = (CComboBox*)GetDlgItem(IDC_VERIFYCOMBO);
i = Win->GetCurSel();
CString sVerify;
Win->GetLBText(i, sVerify); //获得校验位
//初始化串口
Init();
if(OpenConnection(sCom, Baut, Data, sVerify, sStop))
{
m_abIsConnected = TRUE;
//设置定时器
SetTimer(1, 100, NULL);
//线程开始运行
m_hRecvThread = AfxBeginThread(RecvThread, NULL);
AfxMessageBox("串口设置正确!");
m_serialbutton.SetWindowText("关闭串口");
}
}
//关闭串口
else
{ //关闭定时器
KillTimer(1);
ENDSerial();
m_serialbutton.SetWindowText("打开串口");
}
}
//--------------------------------------------------------------
//作用:设置本机IP地址
// 数据格式:03H+IP地址
//
//-------------------------------------------------------------
void CDTEDlg::OnLocalip()
{
int nRetCode = dlgLocalIP.DoModal();
DWORD IP=0;
IP = localIP;
if(nRetCode == IDOK)
{
DTE_OutData DTEOutData;
int iLen = 0;
unsigned char *Temp = NULL;
DTEOutData.Command = ParaUserID;
DTEOutData.Length = 0x04;
DTEOutData.Data.SetLocalIP.IP[0] = (IP&0xff000000)>>24;
DTEOutData.Data.SetLocalIP.IP[1] = (IP&0x00ff0000)>>16;
DTEOutData.Data.SetLocalIP.IP[2] = (IP&0x0000ff00)>>8;
DTEOutData.Data.SetLocalIP.IP[3] = (IP&0x000000ff);
iLen = 6;
//将源地址存入到本地的OwnUserCode
for(int i=0;i<4;i++)
OwnUserCode.byte[i] = DTEOutData.Data.SetLocalIP.IP[i];
Temp = (unsigned char *)(&DTEOutData);
SerialWriteData(m_hCommPort, Temp, iLen);
}
}
//----------------------------------------------------------
// 查询本机IP地址
// 数据格式:03H+00H
//
//-----------------------------------------------------------
void CDTEDlg::OnQuerylocalip()
{
// TODO: Add your command handler code here
UINT16 iLen = 0;
unsigned char *Temp = NULL;
Query_OutData DTEQueryData;
DTEQueryData.Command = ParaUserID;
DTEQueryData.CommandAdd = 0x00;
Temp = (unsigned char *)(&DTEQueryData);
iLen = 2;
SerialWriteData(m_hCommPort, Temp, iLen);
Temp = NULL;
}
//------------------------------------------------------------
//100ms 的定时器用于处理收到的消息
//------------------------------------------------------------
void CDTEDlg::OnTimer(UINT nIDEvent)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -