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

📄 operationtooldlg.cpp

📁 采用文档类方法实现的一种串口通讯协议。可以借鉴参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// OperationToolDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OperationTool.h"
#include "OperationToolDlg.h"
//#include "sprotocol.h"
#include "comsetup.h"

#include <direct.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()


/////////////////////////////////////////////////////////////////////////////
// COperationToolDlg dialog

COperationToolDlg::COperationToolDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COperationToolDlg::IDD, pParent)
{	
	//{{AFX_DATA_INIT(COperationToolDlg)
	m_nCount = 0;
	m_nTeam = -1;
	m_nItem = -1;
	m_nValue = 0;
	m_strSlaveSponse = _T("");	
	m_strInInformation = _T("");		
	//}}AFX_DATA_INIT
	m_nCMDH12 = 0;
	m_nParaCount = 0;
	m_nComPort = 1;
	m_nSensornumber = 0;
	m_lDatalogLength = 0;
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void COperationToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COperationToolDlg)
	DDX_Control(pDX, IDC_Item, m_cbItem);
	DDX_Control(pDX, IDC_Team, m_cbTeam);
	DDX_Control(pDX, IDC_SlaveSponse, m_recSlaveSponse);
	DDX_Text(pDX, IDC_Count, m_nCount);
	DDX_CBIndex(pDX, IDC_Team, m_nTeam);
	DDX_CBIndex(pDX, IDC_Item, m_nItem);
	DDX_Text(pDX, IDC_SlaveSponse, m_strSlaveSponse);
	DDX_Text(pDX, IDC_Input, m_strInInformation);
	DDX_Text(pDX, IDC_Value, m_nValue);
	DDV_MinMaxInt(pDX, m_nValue, 0, 255);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COperationToolDlg, CDialog)
	//{{AFX_MSG_MAP(COperationToolDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_Convert, OnConvert)
	ON_CBN_SELCHANGE(IDC_Team, OnSelchangeTeam)
	ON_CBN_SELCHANGE(IDC_Item, OnSelchangeItem)
	ON_BN_CLICKED(ID_Next, OnNext)
	ON_BN_CLICKED(ID_Initial, OnInitial)
	ON_BN_CLICKED(ID_ComSetup, OnComSetup)
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(ID_CANCEL, OnCancel)
	ON_BN_CLICKED(ID_OpenFile, OnOpenFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COperationToolDlg message handlers

BOOL COperationToolDlg::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
	
	CenterWindow();
	// TODO: Add extra initialization here
	for (int r = 0; r < VERPACKETMAX; r++)
	{
		m_abyDataH[r]=0;
	}
	
	//add big item to team combobox	
	mf_AddTeamName();

	//display some information 
	CString strInf;
	strInf = "CMD1 in Host request packet is HOST_GET.\r\n";
	strInf += "CMD2 in slave request packet is SLAVE_RESP_GET.";
	((CEdit *)GetDlgItem(IDC_Input))->SetWindowText(strInf);


	//intial display 
	((CButton *)GetDlgItem(ID_Convert))->EnableWindow(FALSE);
	((CButton *)GetDlgItem(ID_Next))->EnableWindow(FALSE);
	((CEdit *)GetDlgItem(IDC_Value))->EnableWindow(FALSE);			
	
	//Open COM
	m_ComDev.InitPort(this, m_nComPort, 19200);

	m_theApp = (COperationToolApp *)::AfxGetApp();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void COperationToolDlg::OnSelchangeTeam() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);	

	switch (m_nTeam)
	{
	case 0:
		mf_AddBasicInstrument();
		m_strInInformation = "Packets to Get basic instrument information, sent from Host to Slave (HOST_GET).";
		break;
	case 1:
		mf_AddSensorInformation();
		m_strInInformation = "Packets to GET information on static information of Sensors.";
		break;
	case 2:
		mf_AddMiscellaneousInformation();
		m_strInInformation = "Packets to GET information on Miscellaneous  information .";
		break;
	case 3:
		mf_AddOperationParameters();
		m_strInInformation = "Packets to GET information on operation parameters.";
		break;
	case 4:
		mf_AddSlaveDataStored();
		m_strInInformation = "Packets to GET information on Data stored in slaves.";
		break;
	case 5:
		mf_AddSetSensorValue();
		m_strInInformation = "Packets to SET value on Sensors.";
		break;
	case 6:
		mf_AddSetOperationPara();
		m_strInInformation = "Packets to SET value on Operation parameters.";
		break;
	case 7:
		mf_AddCommandSlaveOperation();
		m_strInInformation = "Commands to Request slave to perform some operation.";
		break;
	case 8:
		mf_AddCommandSpecialFunction();
		m_strInInformation  = "The Special functions are those commands that will not be revealed to outsiders,";
		m_strInInformation += "and may cause serious consequence. Must use very carefully. Usually, it is for ";
		m_strInInformation += "internal debug only. Or manufacturing use it for some initial setting.         ";
		break;	
	default:
		break;
	}
	
	mf_DispMsg("", 1);
}

void COperationToolDlg::OnSelchangeItem() 
{
	// TODO: Add your control notification handler code here	
	UpdateData(TRUE);
	((CButton *)GetDlgItem(ID_OpenFile))->ShowWindow(SW_HIDE);

	switch(m_nTeam)
	{
	case 0:			//Get basic instrument information
		{
			m_nCMDH12 = m_nItem + 0x2020;
			m_nValue = 0;
		}
		break;

	case 1:			//GET information on static information of Sensors
		{
			m_nCMDH12 = m_nItem + 0x2030;

			if ((m_nCMDH12 > 0x2031) && (m_nCMDH12 < 0x203C))
			{
				if (m_nSensornumber <= 0)
				{
					mf_DispMsg("", 1);
					mf_DispMsg("Please run GET_SEN_SUMMERAY and GET_SENSOR_NAME and GET_SEN_DATAFORMAT etc. first!");
					return;
				}
			}

			m_nValue = 0;
		}
		break;

	case 2:		//GET information on Miscellaneous  information 
		{
			m_nCMDH12 = m_nItem + 0x2040;
			m_nValue = 0;
		}
		break;

	case 3:		//GET information on operation parameters
		{
			if (m_nItem < 0x0F)
			{
				m_nCMDH12 = m_nItem + 0x2050;
			}
			else
			{
				m_nCMDH12 = m_nItem + 0x2050 + 1;	//because the command has an interval
			}
			
			//set  nCount of parameter
			if  ((m_nCMDH12 == 0x206A) || (m_nCMDH12 == 0x206B) || (m_nCMDH12 == 0x206C))
			{
				m_nValue = 1;
			}
			else
			{
				m_nValue = 0;
			}
		}
		break;

	case 4:		//GET information on Data stored in slaves
		{
			m_nCMDH12 = m_nItem + 0x2080;
			
			switch(m_nCMDH12)
			{
			case 0x2081:
				{
					m_nValue = 2;
				}
				break;

			case 0x2082:
				{
					m_nValue = 3;
				}
				break;

			case 0x2084:
				{
					if (m_nSensornumber <= 0)	//this item needs sensors' information
					{		
						mf_DispMsg("", 1);
						mf_DispMsg("Please run GET_SEN_SUMMERAY and GET_SENSOR_NAME and GET_SEN_DATAFORMAT etc. first!");
						return;
					}

					if (m_lDatalogLength <= 0)
					{
						mf_DispMsg("", 1);
						mf_DispMsg("Please run GET_SIZE_DATALOG first!");
						return;
					}

					m_nValue = 2;
				}
				break;

			default:
				{
					m_nValue = 0;
				}
				break;
			}	
		}
		break;

	case 5:		//SET value on Sensors
		{
			m_nCMDH12 = m_nItem + 0x2220;
			if ((m_nCMDH12 >= 0x2220) && (m_nCMDH12 < 0x222A))
			{
				if (m_nSensornumber <= 0)
				{			
					mf_DispMsg("", 1);
					mf_DispMsg("please run GET_SEN_SUMMARY and GET_SENSORNAME and GET_SEN_DATAFORMAT items first!");
					return;
				}
			}

			switch(m_nCMDH12)
			{
			case 0x2220:
			case 0x2222:
			case 0x2224:
			case 0x2226:
			case 0x2228:
				m_nValue = 5;
				break;
			case 0x2221:
			case 0x2223:
			case 0x2225:
			case 0x2227:
			case 0x2229:			
				m_nValue = 4 * m_nSensornumber;
				break;
			case 0x222A:
				m_nValue = 999;
				break;
			default:
				m_nValue = 0;
				break;
			}	
		}
		break;

	case 6:		//SET value on Operation parameters
		{
			if (m_nItem < 0x15)
			{
				m_nCMDH12 = m_nItem + 0x2240;
			}
			else if (m_nItem == 0x15)
			{
				m_nCMDH12 = 0x225E;
			}
			else if (m_nItem == 0x20)
			{
				m_nCMDH12 = 0x226E;
			}
			else
			{
				//because the command has an interval of 10
				m_nCMDH12 = m_nItem + 0x2240 + 10;
			}
			
			//set needed parameter nCount
			switch(m_nCMDH12)
			{
			case 0x2243:
			case 0x2245:
			case 0x2246:
			case 0x2247:
			case 0x2248:
			case 0x2249:
			case 0x224F:
			case 0x2251:
			case 0x2253:
			case 0x2254:
			case 0x225E:
			case 0x2268:
			case 0x2269:
				m_nValue = 1;
				break;
			case 0x2240:
			case 0x2244:
			case 0x224A:
			case 0x2250:
			case 0x2252:
			case 0x2263:
			case 0x2264:
			case 0x2266:
			case 0x226E:
				m_nValue = 2;
				break;
			case 0x2241:
			case 0x224E:
				m_nValue = 3;
				break;
			case 0x2242:
				m_nValue = 6;
				break;
			case 0x224C:
			case 0x2265:
				m_nValue = 4;
				break;
			case 0x2267:
				m_nValue = 12;
				break;
			case 0x224D:
				m_nValue = 17;
				break;
			case 0x224B:
			case 0x2260:
			case 0x2261:
			case 0x2262:
				m_nValue = 255;		//nCount is not distinct.
				break;
			default:
				m_nValue = 0;
				break;
			}
		}
		break;

	case 7:		//Commands to Request slave
		{
			m_nCMDH12 = m_nItem + 0x2280;

			if  ((m_nCMDH12 == 0x2280) || (m_nCMDH12 == 0x2288) || (m_nCMDH12 == 0x228A))
			{
				m_nValue = 1;
			}
			else if  (m_nCMDH12 == 0x2281)
			{
				m_nValue = 255;
			}
			else
			{
				m_nValue = 0;
			}
		}
		break;

	case 8:		//Special functions
		{
			m_nCMDH12 = m_nItem + 0x6020;
			
			switch(m_nCMDH12)
			{
			case 0x6020:
			case 0x6021:
				m_nValue = 9;
				break;
			case 0x6022:
			case 0x6026:
				m_nValue = 5;
				break;
			case 0x6023:
				m_nValue = 0;
			case 0x6024:
			case 0x6025:			
				m_nValue = 255;
				break;
			case 0x6027:
				m_nValue = 1;
				break;
			default:
				break;
			}
		}
		break;

	default:
		break;
	}
	
	mf_DispMsg("", 1);

	if (m_nValue != 0 )
	{
		((CButton *)GetDlgItem(ID_Convert))->EnableWindow(FALSE);
		((CButton *)GetDlgItem(ID_Next))->EnableWindow(TRUE);
		((CEdit *)GetDlgItem(IDC_Value))->EnableWindow(TRUE);
		((CButton *)GetDlgItem(ID_Next))->SetWindowText("Start");
	}	//if 0 parameter enable control to status
	else
	{
		((CButton *)GetDlgItem(ID_Convert))->EnableWindow(TRUE);
		((CButton *)GetDlgItem(ID_Next))->EnableWindow(FALSE);
		((CEdit *)GetDlgItem(IDC_Value))->EnableWindow(FALSE);
		((CButton *)GetDlgItem(ID_Next))->SetWindowText("Stop");
	}
}

//
//split receive apcket into data
//
void COperationToolDlg::mf_SplitPacket(BYTE *I_pbyReceivePacket)
{
	ASSERT(I_pbyReceivePacket != NULL);

	CString		strInf;
	CString		strLast;
	BYTE		byCMDSHigh = 0;
	BYTE		abyDataS[VERPACKETMAX] = "";

	short int	nReturnCMDS = 0;
	short int	nRetParamCount = 0;
	
	for(int r = 0; r < VERPACKETMAX; r++)
	{
		abyDataS[r] = 0;
	}
	//demodem receive packets into relevant data
	mf_DemVerPac(I_pbyReceivePacket, nReturnCMDS, nRetParamCount, abyDataS);
	
	//according the received commands' high byte to split work into diffent parts
	byCMDSHigh = (nReturnCMDS & 0xFF00) >> 8;		

	if (((m_nCMDH12 + 0x100) == nReturnCMDS) || (byCMDSHigh == 0x71))
	{
		switch(byCMDSHigh)
		{
		case 0x21:
			mf_Deal21Proc(nReturnCMDS, nRetParamCount, abyDataS);
			break;
		case 0x23:
			mf_Deal23Proc(nReturnCMDS, nRetParamCount, abyDataS);
			break;
		case 0x61:
			mf_Deal61Proc(nReturnCMDS, nRetParamCount, abyDataS);
			break;
		case 0x71:
			mf_Deal71Proc(nRetParamCount, abyDataS);
			break;
		default:
			{
				strLast.Format("0X%X", nReturnCMDS);
				strInf  = "The return Command2 is ";
				strInf += strLast;
				strInf += "\n\r";

⌨️ 快捷键说明

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