📄 moduledlg.cpp
字号:
// ModuleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Module.h"
#include "ModuleDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CModuleDlg dialog
CModuleDlg::CModuleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CModuleDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CModuleDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CModuleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CModuleDlg)
DDX_Control(pDX, IDC_EDIT_NAME, m_EditName);
DDX_Control(pDX, IDC_EDIT_IP, m_EditIP);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CModuleDlg, CDialog)
//{{AFX_MSG_MAP(CModuleDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_MESSAGE(WM_DATACOME,OnDataCome)
ON_BN_CLICKED(IDC_BUTTON_QUIT, OnButtonQuit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModuleDlg message handlers
BOOL CModuleDlg::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
lenofAcceptAddr = 16;
m_send = _T("");
m_accept = _T("");
send_pacType= _T("");
recv_pacType= _T("");
m_buff = new char [RECEIVE_BUFFSIZE];
m_data = new double [10];
int send_pacNum=0;
int recv_pacNum=0;
int Mod_Num=2;
a_AT=0;
e_ET=0;
a_integ=e_integ=a=e=0.0;
if(RecieveInitial() == false)
{
MessageBox("recieve error!");
return false;
}
if(SendInitial() == false)
{
MessageBox("Send error!");
return false;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CModuleDlg::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 CModuleDlg::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 CModuleDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CModuleDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
//获取本地机器的IP地址、机器名,并在界面上显示
char hostName[100];
char hostIP[50];
if (GetHostInfo(hostIP, hostName))
{
m_EditName.SetWindowText(hostName);
m_EditIP.SetWindowText(hostIP);
}
}
//获取主机IP和主机名
BOOL CModuleDlg::GetHostInfo(char * outIP, char * outName)
{
char name[300];
if (gethostname(name, 300) == 0)
{
if (outName)
{
strcpy(outName, name);
}
PHOSTENT hostinfo;
if ((hostinfo = gethostbyname(name)) != NULL)
{
LPCSTR pIP = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
strcpy(outIP, pIP);
return TRUE;
}
}
return FALSE;
}
bool CModuleDlg::OnDataCome(WPARAM wParam,LPARAM lParam)
{
SOCKADDR_IN m_AcceptaddrTemp;
memset(m_buff,'\0',RECEIVE_BUFFSIZE);//清空内存
if(recvfrom(udp_accept,m_buff,RECEIVE_BUFFSIZE,0,(LPSOCKADDR)&m_AcceptaddrTemp,&lenofAcceptAddr)==SOCKET_ERROR)
return false;
if(m_buff[0]!='#')
return false;
int StarIndex = 0;
CString m_temp = _T("");
m_accept = m_buff;
m_accept.Delete(0,1); //删除最左边(从0开始)的一个字符#
StarIndex = m_accept.Find('#'); //找到#号的位置
recv_pacType = m_accept.Left(StarIndex); //取出#号左边的字符串
m_accept.Delete(0,StarIndex +1);
if(recv_pacType=="Ready")//表明数据包类型为广播消息
{
//解析主仿真机地址
m_Sendaddr.sin_addr = m_AcceptaddrTemp.sin_addr;
send_pacType="1111";
Module_Type=2;
m_send.Format("#%s#%d*",send_pacType,Module_Type);
sendto(udp_send,m_send,m_send.GetLength(),0,(SOCKADDR *)&m_Sendaddr,16);
}
else if(recv_pacType=="5555")//表明数据包类型模块输入量
{
StarIndex = m_accept.Find('*'); //找到*号的位置
m_temp = m_accept.Left(StarIndex); //取出*号左边的字符串
recv_pacNum = atoi(m_temp); //将字符串转化为int
m_accept.Delete(0,StarIndex +1);
for(int m=0;m<recv_pacNum;m++)
{
StarIndex = m_accept.Find('*');
m_temp = m_accept.Left(StarIndex);
m_data[m] = atof(m_temp);
if(m<(recv_pacNum-1)) m_accept.Delete(0,StarIndex+1);
}
ModSimulation();
}
return true;
}
bool CModuleDlg::RecieveInitial()
{
// TODO: Add your command handler code here
WORD m_wVersionRequested;
WSADATA m_wsaData;
int m_err;
m_wVersionRequested = MAKEWORD( 1, 1);//version 1.1
m_err = WSAStartup( m_wVersionRequested, &m_wsaData );
if ( m_err != 0 )
{
MessageBox("Can't find a usable Winsock DLL!");
WSACleanup( );
return FALSE;
}
if ( LOBYTE( m_wsaData.wVersion ) != 1 ||HIBYTE( m_wsaData.wVersion ) != 1 )
{
MessageBox("You don't have Winsock or your Winsock version is too low!");
WSACleanup( );
return FALSE;
}
if ((udp_accept=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET)
//创建一个TCP/IP的数据报的socket
{
MessageBox("Socket error!");
WSACleanup();
return false;
}
m_Acceptaddr.sin_family=AF_INET;
m_Acceptaddr.sin_port=RECEIVE_PORT;
m_Acceptaddr.sin_addr.s_addr=INADDR_ANY;
if( bind(udp_accept,(SOCKADDR *)&m_Acceptaddr,sizeof(m_Acceptaddr) ))
{
MessageBox("重复连接或绑定错误!");
return false;
}
//设置m_SforVoiceOut为非阻塞,当有数据到达时,发WM_DATACOME消息
if( WSAAsyncSelect(udp_accept,this->m_hWnd,WM_DATACOME,FD_READ)==SOCKET_ERROR )
{
MessageBox("连接错误!");
WSACleanup();
return false;
}
return true;
}
//创建TCP/IP的socket:发送端
bool CModuleDlg::SendInitial()
{
WORD m_wVersionRequested;
WSADATA m_wsaData;
int m_err;
m_wVersionRequested = MAKEWORD( 1, 1);//version 1.1
m_err = WSAStartup( m_wVersionRequested, &m_wsaData );
if ( m_err != 0 )
{
MessageBox("Can't find a usable Winsock DLL!");
WSACleanup( );
return FALSE;
}
if ( LOBYTE( m_wsaData.wVersion ) != 1 ||HIBYTE( m_wsaData.wVersion ) != 1 )
{
MessageBox("You don't have Winsock or your Winsock version is too low!");
WSACleanup( );
return FALSE;
}
if ((udp_send=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET)
//创建一个TCP/IP的数据报的socket
{
MessageBox("Socket error!");
WSACleanup();
return false;
}
m_Sendaddr.sin_family=AF_INET;
m_Sendaddr.sin_port=SEND_PORT;
m_Sendaddr.sin_addr.s_addr=INADDR_ANY;
return TRUE;
}
void CModuleDlg::ModSimulation()
{
if( Module_Type==2)
{
a=m_data[0];
e=m_data[1];
AT=m_data[2];
ET=m_data[3];
obj_qc.Diff(a,e,AT,ET,a_AT,e_ET);//取差
send_pacType="0000";
send_pacNum=2;
m_send.Format("#%s#%d*%f*%f*",send_pacType,send_pacNum,a_AT,e_ET);
sendto(udp_send,m_send,m_send.GetLength(),0,(SOCKADDR *)&m_Sendaddr,16);
}
if( Module_Type==3)
{
a_AT=m_data[0];
e_ET=m_data[1];
DT=m_data[2];
//电视跟踪
obj_tv.Television(a_AT,e_ET,DT,a_integ,e_integ);
send_pacType="0000";
send_pacNum=2;
m_send.Format("#%s#%d*%f*%f*",send_pacType,send_pacNum,a_integ,e_integ);
sendto(udp_send,m_send,m_send.GetLength(),0,(SOCKADDR *)&m_Sendaddr,16);
}
}
void CModuleDlg::OnButtonQuit()
{
// TODO: Add your control notification handler code here
closesocket(udp_accept);
closesocket(udp_send);
this->OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -