⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yaoxindengdlg.cpp

📁 我设计的遗传算法代码
💻 CPP
字号:
// YAOXINDENGDlg.cpp : implementation file
//

#include "stdafx.h"
#include "YAOXINDENG.h"
#include "YAOXINDENGDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CYAOXINDENGDlg dialog

CYAOXINDENGDlg::CYAOXINDENGDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CYAOXINDENGDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CYAOXINDENGDlg)
	m_strEditReceiveMsg = _T("");
	m_strEditSendMsg = _T("");
	m_douEditDATA = 0.0;
	m_unEditNUMPORT = 0;
	m_unEditADDRESS = 0;
	m_unEditNUMLAP = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CYAOXINDENGDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CYAOXINDENGDlg)
	DDX_Control(pDX, IDC_RADIO1, m_ctrlRADIO);
	DDX_Control(pDX, IDC_COMBO_COMPORT, m_ctrlComboComPort);
	DDX_Text(pDX, IDC_EDIT_RECEIVEMSG, m_strEditReceiveMsg);
	DDX_Text(pDX, IDC_EDIT_SENDMSG, m_strEditSendMsg);
	DDX_Text(pDX, IDC_EDIT_DATA, m_douEditDATA);
	DDX_Text(pDX, IDC_EDIT_NUMPORT, m_unEditNUMPORT);
	DDX_Text(pDX, IDC_EDIT_ADDRESS, m_unEditADDRESS);
	DDX_Text(pDX, IDC_EDIT_NUMLAP, m_unEditNUMLAP);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CYAOXINDENGDlg, CDialog)
	//{{AFX_MSG_MAP(CYAOXINDENGDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
    ON_MESSAGE(WM_COMM_RXCHAR, OnComm)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
	ON_BN_CLICKED(IDC_BUTTON_SENDLDATA, OnButtonSendldata)
	ON_BN_CLICKED(IDC_BUTTON_SENDdigDATA, OnBUTTONSENDdigDATA)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CYAOXINDENGDlg message handlers

BOOL CYAOXINDENGDlg::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
    m_ctrlComboComPort.SetCurSel(0); //初始选择串口1
	//以下两句分别设置“打开串口”、“关闭串口”两个按状态的使能状态
	GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(m_bSerialPortOpened);
	GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(!m_bSerialPortOpened);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CYAOXINDENGDlg::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 CYAOXINDENGDlg::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 CYAOXINDENGDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


LONG CYAOXINDENGDlg::OnComm(WPARAM ch, LPARAM port)
{
	m_strEditReceiveMsg += ch;
	UpdateData(FALSE);  //将接收到的字符显示在接收编辑框中
	return 0;
}

void CYAOXINDENGDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
    int nPort=m_ctrlComboComPort.GetCurSel()+1; //得到串口号,想想为什么要加1
	if(m_SerialPort.InitPort(this, nPort, 9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
	{
		m_SerialPort.StartMonitoring();
		m_bSerialPortOpened=TRUE;
	}
	else
	{
		AfxMessageBox("没有发现此串口或被占用");
		m_bSerialPortOpened=FALSE;
	}
	GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(!m_bSerialPortOpened);
	GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(m_bSerialPortOpened);	
}

void CYAOXINDENGDlg::OnButtonClose() 
{
	// TODO: Add your control notification handler code here
	m_SerialPort.ClosePort();//关闭串口
    m_bSerialPortOpened=FALSE;
	GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(!m_bSerialPortOpened);
	GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(m_bSerialPortOpened);	
}

void CYAOXINDENGDlg::OnButtonSendldata() 
{
	// TODO: Add your control notification handler code here
char string[25]; 
	
if(!m_bSerialPortOpened)
{
	AfxMessageBox("串口没有打开");
    return;
}	
UpdateData(TRUE); //读入编辑框中的数据
CString  strSend="AAAA2305";

int i;
i=m_unEditADDRESS;
itoa(i, string, 16);
if(i<16)
{strSend+='0';
strSend+=string;
strSend+='b';
strSend+='0';}
else
{strSend+=string;
strSend+='b';
strSend+='0';}


i=m_unEditNUMLAP;
itoa(i, string, 16);
strSend+=string;


int checkedRadio;
checkedRadio=GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO6);
if(checkedRadio==IDC_RADIO1)
{strSend+='0';
 strSend+='1';}
if(checkedRadio==IDC_RADIO2)
{strSend+='0';
strSend+='2';}
if(checkedRadio==IDC_RADIO3)
{strSend+='0';
strSend+='5';}
if(checkedRadio==IDC_RADIO4)
{strSend+='0';
strSend+='6';}
if(checkedRadio==IDC_RADIO5)
{strSend+='0';
 strSend+='f';}
if(checkedRadio==IDC_RADIO6)
{strSend+='0';
strSend+='0';}


int j=5;
j+=m_unEditADDRESS;
j+=176;
j+=m_unEditNUMLAP;
if(checkedRadio==IDC_RADIO1)
{j+=1;}
if(checkedRadio==IDC_RADIO2)
{j+=2;}
if(checkedRadio==IDC_RADIO3)
{j+=5;}
if(checkedRadio==IDC_RADIO4)
{j+=6;}
if(checkedRadio==IDC_RADIO5)
{j+=15;}
if(checkedRadio==IDC_RADIO6)
{j+=0;}
j=~j;
j+=1;
itoa(j, string, 16);
strSend+=string[6];
strSend+=string[7];
SendMODBUSdata(strSend);
}







void CYAOXINDENGDlg::SendMODBUSdata(CString &strData)
{
CString SendMODBUSdata;
SendMODBUSdata=strData;
m_SerialPort.WriteToPort((LPCTSTR)SendMODBUSdata);
m_strEditSendMsg.Format("发送的灯数据包为:%s",SendMODBUSdata);
UpdateData(FALSE);

}

void CYAOXINDENGDlg::OnBUTTONSENDdigDATA() 
{
	// TODO: Add your control notification handler code here
char string1[25]; 
	
if(!m_bSerialPortOpened)
{
	AfxMessageBox("串口没有打开");
    return;
}	
UpdateData(TRUE); //读入编辑框中的数据
CString  strSend1="AAAA230b7d";
int j;
j=m_unEditNUMPORT;
itoa(j, string1, 16);
if(j<16)
{strSend1+='0';
strSend1+=string1;

}
else
{strSend1+=string1;

}
for (int k=0;k<8;k++)
{strSend1+='0';}

float n=m_douEditDATA;
j=int(n);
itoa(j, string1, 16);
if(j<16)
{strSend1+='0';
strSend1+=string1;
}
else
{strSend1+=string1;
}


strSend1+='8';
n=(n-int(n))*10;
j=int(n);
itoa(j, string1, 16);
strSend1+=string1;


n=(n-int(n))*10;
j=int(n);
itoa(j, string1, 16);
strSend1+='0';
strSend1+=string1;

n=(n-int(n))*10;
j=int(n);
itoa(j, string1, 16);
strSend1+='0';
strSend1+=string1;

int m=136;
m+=m_unEditNUMPORT;
m+=int(m_douEditDATA);

m+=128;
float c=(m_douEditDATA-int(m_douEditDATA))*10;
m+=int(c);
c=(c-int(c))*10;
m+=int(c);
c=c=(c-int(c))*10;
m+=int(c);
m=~m;
m+=1;
itoa(m, string1, 16);
strSend1+=string1[6];
strSend1+=string1[7];
SendMODBUSdata(strSend1);
	
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -