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

📄 vcsampledlg.cpp

📁 modbus通讯
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// VcSampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VcSample.h"
#include "VcSampleDlg.h"

#include "ComFile.h"
#include ".\vcsampledlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const int MAX_BPS=38400;
const int RESIDUE_NUM=0;
const int Y_COMPONENT_MAX=256;
const int X_COMPONENT_MAX=256;
const int M_COMPONENT_MAX=2000;
const int SM_COMPONENT_MAX=256;
const int S_COMPONENT_MAX=992;
const int T_COMPONENT_MAX=256;
const int C_COMPONENT_MAX=256;
const int D_COMPONENT_MAX=8000;
const int SD_COMPONENT_MAX=256;
const int Z_COMPONENT_MAX=16;
const int T_COMPONENT_CHAR_MAX=256;
const int C_COMPONENT_CHAR_MAX=200;
const int GREAT_VALUE=1;
const int GREAT_NUM=2;
const int SUCCESS_SPINT=3;
/************************************************************************/
/* 定义一个函数指针类型                                                */
/*OpenComm 打开串口                                                     */
typedef int (__stdcall *OpenComm)(int,int,int,int,int,int,int,int);
//* 关闭串口
typedef int (__stdcall *CloseComm)(int);
//*读多个线圈
typedef int (__stdcall *ReaddMultiBit)(int,int,char*,int,unsigned char[]);
//*读多个寄存器
typedef int (__stdcall *ReaddMultiWord)(int,int,char*,int,short[]);
//*写单个线圈
typedef int (__stdcall *WrtOneBit)(int,int,char*,char);
//*写多个线圈
typedef int (__stdcall *WrtMultBit)(int,int,char*,int,char[]);
//*写单个寄存器
typedef int (__stdcall *WrtOneWord)(int,int,char*,short);
//*写多个寄存器
typedef int (__stdcall *WrtMultWord)(int,int,char*,int,short[]);
//回送诊断校验
typedef int (__stdcall *ACKPack)(int,int,short,short*);
/************************************************************************/
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CVcSampleDlg dialog

CVcSampleDlg::CVcSampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVcSampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVcSampleDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVcSampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVcSampleDlg)
	DDX_Control(pDX, IDOK, m_btnOk);
	DDX_Control(pDX, IDC_OperMsg, m_EdMsg);
	DDX_Control(pDX, IDC_CmbVerftyCode, m_cmbVerftyCode);
	DDX_Control(pDX, IDC_CmbItem, m_cmbItem);
	DDX_Control(pDX, IDC_OpertionBtn, m_btnOperation);
	DDX_Control(pDX, IDC_EdValue, m_edValue);
	DDX_Control(pDX, IDC_EdStartAddr, m_edStartAddr);
	DDX_Control(pDX, IDC_EdNum, m_edNum);
	DDX_Control(pDX, IDC_CmbSationNo, m_cmbSationNo);
	DDX_Control(pDX, ID_EXIT, m_BtnExit);
	DDX_Control(pDX, IDC_EDIT2, m_CmbTimes);
	DDX_Control(pDX, IDC_EDIT1, m_EdOverTime);
	DDX_Control(pDX, IDC_COMBO5, m_cmbParityBit);
	DDX_Control(pDX, IDC_COMBO4, m_cmbSTopBit);
	DDX_Control(pDX, IDC_COMBO3, m_cmbDataBit);
	DDX_Control(pDX, IDC_COMBO2, m_cmbBps);
	DDX_Control(pDX, IDC_COMBO1, m_cmbCommName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVcSampleDlg, CDialog)
	//{{AFX_MSG_MAP(CVcSampleDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_EXIT, OnExit)
	ON_WM_KEYDOWN()
	ON_CBN_SELCHANGE(IDC_COMBO2, OnSelchangeCombo2)
	ON_BN_CLICKED(IDC_RdMultiBit, OnRdMultiBit)
	ON_BN_CLICKED(IDC_RdMultiWord, OnRdMultiWord)
	ON_BN_CLICKED(IDC_WrtOneBit, OnWrtOneBit)
	ON_BN_CLICKED(IDC_WrtMultBit, OnWrtMultBit)
	ON_BN_CLICKED(IDC_WrtOneWord, OnWrtOneWord)
	ON_BN_CLICKED(IDC_WrtMultWord, OnWrtMultWord)
	ON_BN_CLICKED(IDC_VerfityCode, OnVerfityCode)
	ON_BN_CLICKED(IDC_OpertionBtn, OnOpertionBtn)
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_CmbItem, OnCbnSelchangeCmbitem)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_OpertionBtn2, OnBnClickedOpertionbtn2)
	ON_BN_CLICKED(IDC_ResetBtn, OnBnClickedResetbtn)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVcSampleDlg message handlers

BOOL CVcSampleDlg::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
	/*
	*	初始化成员变量
	*/
	m_nCommName=0;
	// TODO: Add extra initialization here
	/*
	*	增加串口名称
	*/

	m_cmbCommName.AddString("COM1");
	m_cmbCommName.AddString("COM2");
	m_cmbCommName.AddString("COM3");
	m_cmbCommName.AddString("COM4");
	m_cmbCommName.AddString("COM5");
	m_cmbCommName.AddString("COM6");
	m_cmbCommName.AddString("COM7");
	m_cmbCommName.AddString("COM8");
	m_cmbCommName.AddString("COM9");
	m_cmbCommName.AddString("COM10");
	m_cmbCommName.SetCurSel(0);

	/*
	*	增加波特率
	*/
	m_cmbBps.AddString("19200");
	m_cmbBps.AddString("9600");
	m_cmbBps.AddString("4800");
	m_cmbBps.AddString("2400");
	m_cmbBps.AddString("1200");
	m_cmbBps.AddString("38400");
	m_cmbBps.SetCurSel(0);
	/*
	*	增加校验码
	*/
	m_cmbParityBit.AddString("无");    
	m_cmbParityBit.AddString("奇校验");
	m_cmbParityBit.AddString("偶校验"); 
	m_cmbParityBit.SetCurSel(2);
	/*
	*	增加停止位 
	*/
	m_cmbSTopBit.AddString("1");
	m_cmbSTopBit.AddString("1.5");
	m_cmbSTopBit.AddString("2");
	m_cmbSTopBit.SetCurSel(0);
	/*
	*	增加数据位
	*/
	m_cmbDataBit.AddString("4");
	m_cmbDataBit.AddString("5");
	m_cmbDataBit.AddString("6");
	m_cmbDataBit.AddString("7");
	m_cmbDataBit.AddString("8");
	m_cmbDataBit.SetCurSel(0);
	/*
	*	设置默认选择为RTU,读多个线圈
	*/
	CButton *pWnd=NULL; 
	pWnd =(CButton*) GetDlgItem(IDC_RTU);
	pWnd->SetCheck(1);

	pWnd =(CButton*) GetDlgItem(IDC_RdMultiBit);
	pWnd->SetCheck(1);


	m_CmbTimes.SetWindowText("1");
	m_EdOverTime.SetWindowText("1000") ;
	/*
	*	初始化初始参数,设置输入框、按钮的有效性
	*/
	//定义按钮的指针变量
	CButton *pRdEnabled=NULL; 
	//获取读多个线圈按钮的指针
	pRdEnabled =(CButton*) GetDlgItem(IDC_RdMultiBit);
	//设置读多个线圈的为无效
	pRdEnabled->EnableWindow(false);
	pRdEnabled =(CButton*) GetDlgItem(IDC_RdMultiWord);
	pRdEnabled->EnableWindow(false);
	pRdEnabled =(CButton*) GetDlgItem(IDC_WrtOneBit);
	pRdEnabled->EnableWindow(false);
	pRdEnabled =(CButton*) GetDlgItem(IDC_WrtMultBit);
	pRdEnabled->EnableWindow(false);
	pRdEnabled =(CButton*) GetDlgItem(IDC_WrtOneWord);
	pRdEnabled->EnableWindow(false);
	pRdEnabled =(CButton*) GetDlgItem(IDC_WrtMultWord);
	pRdEnabled->EnableWindow(false);

	pRdEnabled =(CButton*) GetDlgItem(IDC_VerfityCode);
	pRdEnabled->EnableWindow(false);

	pRdEnabled =(CButton*) GetDlgItem(IDC_ResetBtn);
	pRdEnabled->EnableWindow(false);
	pRdEnabled =(CButton*) GetDlgItem(IDC_OpertionBtn);
	pRdEnabled->EnableWindow(false);
	//设置诊断码的无效并隐藏 begin
	m_cmbVerftyCode.EnableWindow(false);
	m_cmbVerftyCode.ShowWindow(SW_HIDE);

	CStatic *tVergtyCode=NULL;
	tVergtyCode =(CStatic *) GetDlgItem(IDC_stVertfyCode);
	tVergtyCode->ShowWindow(SW_HIDE);
	//设置诊断码的无效并隐藏 end
	m_edValue.EnableWindow(false);
	m_edStartAddr.EnableWindow(false);
	m_edNum.EnableWindow(false);
	m_cmbItem.EnableWindow(false);
	m_cmbSationNo.EnableWindow(false);
	/*
	*	控制元件类型、输入框的有效性
	*/
	AddItem(0);
	/*
	*	增加站点号
	*/
	CString strStationNo;
	for(int i=1;i<=31;i++)
	{
		strStationNo.Format("%d",i);
		m_cmbSationNo.AddString(strStationNo);
	}
	m_cmbSationNo.SetCurSel(0);
	//获取DLL的句柄
	m_hInst=::LoadLibrary("ModbusLib.dll");
	if(m_hInst==NULL)
	{
		MessageBox("ModbusLib.dll不存在或加载失败","提示框",MB_ICONERROR);
		CDialog::OnCancel();
		return FALSE;
	}
	m_nOperIndex=0;
	m_pStartaddr=NULL;
	m_chBitValue[MAX_NUM]='\0';
	m_shWordValue[MAX_NUM]='\0';
	//重新启动通信选项命令
	m_bListen=false;

	UpdateData(TRUE);
	m_pComFile=NULL;	 
	m_pComFile=new CComFile();
	if (m_pComFile!=NULL)
	{
		if(!m_pComFile->openComFile()) 
		{
			MessageBox("com.ini文件不存在","提示框",MB_ICONERROR);
			CDialog::OnCancel();
			return FALSE;
		}
	}
	//根据COM初始化串口参数设置 begin
	//串口号、停止位、校验码、格式
	strComInfo cominfo;
	CString strvalue;
	int nFind=0;
	cominfo=m_pComFile->getComInfo();
	nFind=m_cmbCommName.FindString(0,cominfo.strComNumber);
	if(cominfo.strComNumber=="COM1")
	{
		nFind=0;
	}
	if(nFind==-1)
		nFind=0;
	m_cmbCommName.SetCurSel(nFind);

	nFind=m_cmbSTopBit.FindString(0,cominfo.strStopBits);
	if(cominfo.strStopBits=="1")
	{
		nFind=0;
	} else if(cominfo.strStopBits="1.5")
	{
		nFind=1;
	} else if(cominfo.strStopBits=="2")
	{
		nFind=2;
	}
	m_cmbSTopBit.SetCurSel(nFind)  ;

	nFind=m_cmbParityBit.FindString(0,cominfo.strParity);
	if(nFind==-1)
		nFind=0;
	m_cmbParityBit.SetCurSel(nFind)  ;
	if(cominfo.strFormats =="ASCII")
	{
		pWnd =(CButton*) GetDlgItem(IDC_ASCII);
		pWnd->SetCheck(1);
		pWnd =(CButton*) GetDlgItem(IDC_RTU);
		pWnd->SetCheck(0);
	} else
	{
		pWnd =(CButton*) GetDlgItem(IDC_RTU);
		pWnd->SetCheck(1);
		pWnd =(CButton*) GetDlgItem(IDC_ASCII);
		pWnd->SetCheck(0);

	}	 
	//数据位、次数、超时时间、波特率
	CString strTimes,strOvertTime,strPerSecond,strDataBits;	 
	strTimes.Format("%d",cominfo.ntimes);
	strOvertTime.Format("%d",cominfo.nOverTime);
	strPerSecond.Format("%d",cominfo.nPersScond);
	strDataBits.Format("%d",cominfo.nDataBits);

	m_CmbTimes.SetWindowText(strTimes);
	m_EdOverTime.SetWindowText(strOvertTime);

	nFind=m_cmbDataBit.FindString(0,strDataBits);
	if(nFind==-1)
		nFind=0;
	m_cmbDataBit.SetCurSel(nFind)  ;
	nFind=m_cmbBps.FindString(0,strPerSecond);
	if(nFind==-1)
		nFind=0;
	m_cmbBps.SetCurSel(nFind)  ;
	m_edStartAddr.SetWindowText("0");
	CButton *BtnRead=NULL;
	BtnRead=(CButton*)GetDlgItem(IDC_OpertionBtn);
	BtnRead->SetWindowText("读");
	BtnRead=(CButton*)GetDlgItem(IDC_OpertionBtn2);
	BtnRead->SetWindowText("清除");
	//根据COM初始化串口参数设置 begin
	BOOL bSuccess=false;
	bSuccess=LoadJPGFile(".//title_vc.jpg");
	if(!bSuccess)
	{
		MessageBox("title_vc.jpg文件不存在","提示框",MB_ICONERROR);
		CDialog::OnCancel();
		return FALSE;
	}
	Invalidate();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVcSampleDlg::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 CVcSampleDlg::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
	{
		CPaintDC dc(this);
		DrawImage(5,5,&dc);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVcSampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVcSampleDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	//保存串口参数begin
	//串口号、停止位、校验码、格式
	CString strComName,strStopBits,strParity,strFormats;
	m_cmbCommName.GetWindowText(strComName);
	m_cmbSTopBit.GetWindowText(strStopBits);
	m_cmbParityBit.GetWindowText(strParity);
	CButton *pWnd=NULL;
	pWnd =(CButton*) GetDlgItem(IDC_ASCII);
	if (pWnd->GetCheck())
	{
		strFormats="ASCII";
	}
	else
	{
		strFormats="RTU";
	}
	//数据位、次数、超时时间、波特率
	CString strTimes,strOvertTime,strPerSecond,strDataBits;	
	int nTimes,nOvertTime,nPerSecond,nDataBits;
	nTimes=1;
	nOvertTime=1000;
	nPerSecond=19200;
	nDataBits=8;
	m_CmbTimes.GetWindowText(strTimes);  
	m_EdOverTime.GetWindowText(strOvertTime);
	m_cmbBps.GetWindowText(strPerSecond);
	m_cmbDataBit.GetWindowText(strDataBits);
	sscanf(strTimes,"%d",&nTimes);
	sscanf(strOvertTime,"%d",&nOvertTime);
	sscanf(strPerSecond,"%d",&nPerSecond);
	sscanf(strDataBits,"%d",&nDataBits);
	strComInfo cominfo;
	cominfo.nDataBits=nDataBits;
	cominfo.nOverTime=nOvertTime;
	cominfo.nPersScond=nPerSecond;
	cominfo.ntimes=nTimes;
	cominfo.strComNumber=strComName;
	cominfo.strFormats=strFormats;
	cominfo.strParity=strParity;
	cominfo.strStopBits=strStopBits;
	m_pComFile->WriteComInfo(cominfo);
	//保存串口参数end

	//释放DLL的句柄

⌨️ 快捷键说明

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