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

📄 mp2kdlg.cpp

📁 这是我用VC写的一个串口通信的程序,希望大学喜欢啊!
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// Mp2KDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Mp2K.h"
#include "Mp2KDlg.h"
#include "mscomm.h"
#include "ConstantDefine.h"
#include "DelaySetting.h"
#include <winbase.h>
#include <afxwin.h>
#include <winuser.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMp2KDlg dialog
/****************************Global variable*****************************/
struct Data_Struct
{
	BYTE SentData[250];
	BYTE ReceivedData[250];
	long DataToWrite;
	long DataToRead;
}Data;

struct PackagedData_Struct
{
	BYTE PackagedData[1024];
	long PackagedDataLen;
}Packaged_Data,Unpackaged_Data;

struct OperationModeNode
{
	BYTE Operation_Mode;
	OperationModeNode* next;
}ModeNode0,ModeNode1,ModeNode2,ModeNode3,ModeNode4,*pCurCheckedMode,*pCurSedMode;

struct ReceivedDataNode
{
	BYTE SentData[250];
	long DataToWrited;
	ReceivedDataNode* next;
}DataNode0,DataNode1,DataNode2,DataNode3,DataNode4,*pCurCheckedNode,*pCurSedNode;

WORD CRC_Check;
CMSComm SerialCom;
CString ComSettings;
BYTE /*OperationMode = 0,*/PreviousOperation = 0;
DelaySetting SetDelay;
CComboBox *pCombo;
int LEDCounter,LoopCounter;
bool LEDLoop = false;
bool MComLoop = false,SCom1Loop = false;
bool SCom2Loop = false,SCom3Loop = false,SCom4Loop = false;
/************************for GetInputData function***********************/
VARIANT input;
COleSafeArray SafeArray;
long len;
/***********************************************************************/

CMp2KDlg::CMp2KDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMp2KDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMp2KDlg)
	m_MComFail = 0;
	m_MComLoopSuccess = 0;
	m_MComLoopFail = 0;
	m_MComSuccess = 0;
	m_SCom1Fail = 0;
	m_SCom1LoopFail = 0;
	m_SCom1Success = 0;
	m_SCom2Fail = 0;
	m_SCom2LoopFail = 0;
	m_SCom2LoopSuccess = 0;
	m_SCom2Success = 0;
	m_SCom3Fail = 0;
	m_SCom3LoopFail = 0;
	m_SCom3LoopSuccess = 0;
	m_SCom3Success = 0;
	m_SCom4Fail = 0;
	m_SCom4LoopFail = 0;
	m_SCom4LoopSuccess = 0;
	m_SCom4Success = 0;
	m_TestBaud = -1;
	m_SCom1LoopSuccess = 0;
	m_CurSel = 0;
	m_VersionString = _T("Version:");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMp2KDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMp2KDlg)
	DDX_Control(pDX, IDC_SELECTPCCOM, m_PCCom);
	DDX_Control(pDX, ID_STOPLOOP, m_StopLoop);
	DDX_Control(pDX, ID_SET, m_Set);
	DDX_Control(pDX, ID_STARTLOOP, m_StartLoop);
	DDX_Text(pDX, ID_MCOMFEDIT, m_MComFail);
	DDV_MinMaxInt(pDX, m_MComFail, 0, 100000);
	DDX_Text(pDX, ID_MCOMLOOSEDIT, m_MComLoopSuccess);
	DDV_MinMaxLong(pDX, m_MComLoopSuccess, 0, 10000000);
	DDX_Text(pDX, ID_MCOMLOOPFEDIT, m_MComLoopFail);
	DDV_MinMaxLong(pDX, m_MComLoopFail, 0, 10000000);
	DDX_Text(pDX, ID_MCOMSEDIT, m_MComSuccess);
	DDV_MinMaxInt(pDX, m_MComSuccess, 0, 100000);
	DDX_Text(pDX, ID_SCOM1FEDIT, m_SCom1Fail);
	DDV_MinMaxInt(pDX, m_SCom1Fail, 0, 100000);
	DDX_Text(pDX, ID_SCOM1LOOPFEDIT, m_SCom1LoopFail);
	DDV_MinMaxLong(pDX, m_SCom1LoopFail, 0, 10000000);
	DDX_Text(pDX, ID_SCOM1SEDIT, m_SCom1Success);
	DDV_MinMaxInt(pDX, m_SCom1Success, 0, 100000);
	DDX_Text(pDX, ID_SCOM2FEDIT, m_SCom2Fail);
	DDV_MinMaxInt(pDX, m_SCom2Fail, 0, 100000);
	DDX_Text(pDX, ID_SCOM2LOOPFEDIT, m_SCom2LoopFail);
	DDV_MinMaxLong(pDX, m_SCom2LoopFail, 0, 10000000);
	DDX_Text(pDX, ID_SCOM2LOOPSEDIT, m_SCom2LoopSuccess);
	DDV_MinMaxLong(pDX, m_SCom2LoopSuccess, 0, 10000000);
	DDX_Text(pDX, ID_SCOM2SEDIT, m_SCom2Success);
	DDV_MinMaxInt(pDX, m_SCom2Success, 0, 100000);
	DDX_Text(pDX, ID_SCOM3FEDIT, m_SCom3Fail);
	DDV_MinMaxInt(pDX, m_SCom3Fail, 0, 100000);
	DDX_Text(pDX, ID_SCOM3LOOPFEDIT, m_SCom3LoopFail);
	DDV_MinMaxLong(pDX, m_SCom3LoopFail, 0, 10000000);
	DDX_Text(pDX, ID_SCOM3LOOPSEDIT, m_SCom3LoopSuccess);
	DDV_MinMaxLong(pDX, m_SCom3LoopSuccess, 0, 10000000);
	DDX_Text(pDX, ID_SCOM3SEDIT, m_SCom3Success);
	DDV_MinMaxInt(pDX, m_SCom3Success, 0, 100000);
	DDX_Text(pDX, ID_SCOM4FEDIT, m_SCom4Fail);
	DDV_MinMaxInt(pDX, m_SCom4Fail, 0, 100000);
	DDX_Text(pDX, ID_SCOM4LOOPFEDIT, m_SCom4LoopFail);
	DDV_MinMaxLong(pDX, m_SCom4LoopFail, 0, 10000000);
	DDX_Text(pDX, ID_SCOM4LOOPSEDIT, m_SCom4LoopSuccess);
	DDV_MinMaxLong(pDX, m_SCom4LoopSuccess, 0, 10000000);
	DDX_Text(pDX, ID_SCOM4SEDIT, m_SCom4Success);
	DDV_MinMaxInt(pDX, m_SCom4Success, 0, 100000);
	DDX_Text(pDX, ID_SCOM1LOOPSEDIT, m_SCom1LoopSuccess);
	DDV_MinMaxLong(pDX, m_SCom1LoopSuccess, 0, 10000000);
	DDX_CBIndex(pDX, IDC_SELECTPCCOM, m_CurSel);
	DDX_Text(pDX, IDC_VERSIONEDIT, m_VersionString);
	DDV_MaxChars(pDX, m_VersionString, 50);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMp2KDlg, CDialog)
	//{{AFX_MSG_MAP(CMp2KDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_RESET, OnReset)
	ON_BN_CLICKED(ID_MCOM, OnMcom)
	ON_BN_CLICKED(ID_SCOM1, OnScom1)
	ON_BN_CLICKED(ID_SCOM2, OnScom2)
	ON_BN_CLICKED(ID_SCOM3, OnScom3)
	ON_BN_CLICKED(ID_SCOM4, OnScom4)
	ON_BN_CLICKED(ID_9600BPS, On9600bps)
	ON_BN_CLICKED(ID_19200BPS, On19200bps)
	ON_BN_CLICKED(ID_38400BPS, On38400bps)
	ON_BN_CLICKED(ID_LOOP, OnLoop)
	ON_BN_CLICKED(ID_SET, OnSet)
	ON_WM_TIMER()
	ON_BN_CLICKED(ID_STARTLOOP, OnStartloop)
	ON_BN_CLICKED(ID_STOPLOOP, OnStoploop)
	ON_BN_CLICKED(IDC_CLEARMCOM, OnClearmcom)
	ON_BN_CLICKED(IDC_CLEARSCOM1, OnClearscom1)
	ON_BN_CLICKED(IDC_CLEARSCOM2, OnClearscom2)
	ON_BN_CLICKED(IDC_CLEARSCOM3, OnClearscom3)
	ON_BN_CLICKED(IDC_CLEARSCOM4, OnClearscom4)
	ON_CBN_CLOSEUP(IDC_SELECTPCCOM, OnCloseupSelectpccom)
	ON_BN_CLICKED(IDC_FINISH, OnFinish)
	ON_BN_CLICKED(IDC_GETVERSION, OnGetversion)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_EVENTSINK_MAP(CMp2KDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CMp2KDlg)
	ON_EVENT(CMp2KDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMp2KDlg message handlers

BOOL CMp2KDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	if(!SerialCom.Create(NULL,WS_VISIBLE | WS_CHILD,CRect(0,0,50,50),this,IDC_MSCOMM1))
	{
		AfxMessageBox("创建MSComm控件失败!");
		return false;
	}

	if(!SetDelay.Create(IDD_SETDELAYDIALOG,this))
	{
		AfxMessageBox("创建子对话框失败!");
		return false;
	}
	
	// Set the default setting
	SerialCom.SetCommPort(1);
	ComSettings = "19200,N,8,1";
	SerialCom.SetSettings(ComSettings);
	SerialCom.SetInBufferSize(300);
	SerialCom.SetInBufferCount(0);
	SerialCom.SetInputMode(1);
	SerialCom.SetInputLen(0);
	SerialCom.SetSThreshold(0);
	SerialCom.SetRThreshold(1);
	SerialCom.SetPortOpen(true);
	SetDelay.m_DelaySelect = 0;
	m_StopLoop.EnableWindow(false);

	ModeNode0.next = &ModeNode1;
	ModeNode1.next = &ModeNode2;
	ModeNode2.next = &ModeNode3;
	ModeNode3.next = &ModeNode4;
	ModeNode4.next = &ModeNode0;
	pCurCheckedMode = pCurSedMode = &ModeNode0;

	DataNode0.next = &DataNode1;
	DataNode1.next = &DataNode2;
	DataNode2.next = &DataNode3;
	DataNode3.next = &DataNode4;
	DataNode4.next = &DataNode0;
	pCurCheckedNode = pCurSedNode = &DataNode0;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMp2KDlg::OnReset() 
{
	SerialCom.SetInBufferCount(0);
	SerialCom.SetOutBufferCount(0);

	m_MComSuccess = m_SCom1Success = m_SCom2Success = m_SCom3Success = m_SCom4Success = 0;
	m_MComFail = m_SCom1Fail = m_SCom2Fail = m_SCom3Fail = m_SCom4Fail = 0;
	m_MComLoopSuccess = m_SCom1LoopSuccess =
		m_SCom2LoopSuccess = m_SCom3LoopSuccess = m_SCom4LoopSuccess = 0;
	m_MComLoopFail = m_SCom1LoopFail =
		m_SCom2LoopFail = m_SCom3LoopFail = m_SCom4LoopFail = 0;
	m_VersionString = m_VersionString.Left(m_VersionString.ReverseFind(':') + 1);
	UpdateData(false);
}


void CMp2KDlg::On9600bps() 
{
	Data.SentData[0] = 0x01;
	Data.SentData[1] = 0x00;
	Data.SentData[2] = 0x06;
	Data.SentData[3] = 0x01;
	Data.SentData[4] = 0x00;
	Data.SentData[5] = 0x00;
	Data.SentData[6] = 0x03;
	Data.DataToWrite = 7;

	Packager(Data.SentData,Packaged_Data.PackagedData,Data.DataToWrite,
		&Packaged_Data.PackagedDataLen);
	TransferData(Packaged_Data.PackagedData,& Packaged_Data.PackagedDataLen);
	PreviousOperation = SETBAUD;

	// Set PC serial com
	SerialCom.SetPortOpen(false);
	ComSettings = "9600,N,8,1";
	SerialCom.SetSettings(ComSettings);
	SerialCom.SetPortOpen(true);

	// Make up the SetDelay dialog combobox
	SetDelay.m_ComboBox.DeleteString(4);
	SetDelay.m_ComboBox.DeleteString(3);
}

void CMp2KDlg::On19200bps() 
{
	Data.SentData[0] = 0x01;
	Data.SentData[1] = 0x00;
	Data.SentData[2] = 0x06;
	Data.SentData[3] = 0x01;
	Data.SentData[4] = 0x00;
	Data.SentData[5] = 0x00;
	Data.SentData[6] = 0x04;
	Data.DataToWrite = 7;

	Packager(Data.SentData,Packaged_Data.PackagedData,Data.DataToWrite,
		&Packaged_Data.PackagedDataLen);
	TransferData(Packaged_Data.PackagedData,& Packaged_Data.PackagedDataLen);
	PreviousOperation = SETBAUD;

	// Set PC serial com
	SerialCom.SetPortOpen(false);
	ComSettings = "19200,N,8,1";
	SerialCom.SetSettings(ComSettings);
	SerialCom.SetPortOpen(true);

	// Make up the SetDelay dialog combobox
	if(SetDelay.m_ComboBox.GetCount() == 5)
		SetDelay.m_ComboBox.DeleteString(4);
	else if(SetDelay.m_ComboBox.GetCount() == 3)
		SetDelay.m_ComboBox.AddString("1次/150毫秒");
}

void CMp2KDlg::On38400bps() 
{
	Data.SentData[0] = 0x01;
	Data.SentData[1] = 0x00;
	Data.SentData[2] = 0x06;
	Data.SentData[3] = 0x01;
	Data.SentData[4] = 0x00;
	Data.SentData[5] = 0x00;
	Data.SentData[6] = 0x05;
	Data.DataToWrite = 7;

	Packager(Data.SentData,Packaged_Data.PackagedData,Data.DataToWrite,
		&Packaged_Data.PackagedDataLen);
	TransferData(Packaged_Data.PackagedData,& Packaged_Data.PackagedDataLen);
	PreviousOperation = SETBAUD;

	// Set PC serial com
	SerialCom.SetPortOpen(false);
	ComSettings = "38400,N,8,1";
	SerialCom.SetSettings(ComSettings);
	SerialCom.SetPortOpen(true);

	// Make up the SetDelay dialog combobox
	if(SetDelay.m_ComboBox.GetCount() == 3)
	{
		SetDelay.m_ComboBox.AddString("1次/150毫秒");
		SetDelay.m_ComboBox.AddString("1次/100毫秒");
	}
	else if(SetDelay.m_ComboBox.GetCount() == 4)
		SetDelay.m_ComboBox.AddString("1次/100毫秒");
}

void CMp2KDlg::OnLoop() 
{
	LEDCounter = 0;
	LEDLoop = !LEDLoop;
	if(LEDLoop)
		SetTimer(ID_LEDTimer,1000,NULL);
	else
		KillTimer(ID_LEDTimer);
	KillTimer(ID_MComTimer);
	KillTimer(ID_SCom1Timer);
	KillTimer(ID_SCom2Timer);
	KillTimer(ID_SCom3Timer);
	KillTimer(ID_SCom4Timer);
}

bool CMp2KDlg::Packager(BYTE SourceData[250],BYTE DestData[1024],long DataToPackage,
						long *PackagedDataLen)
{
	long PackagerCount = 0,DataToCheck = 0;
	BYTE *pCheckedData;
	DestData[PackagerCount ++] = 0xff;
	for(int i = 0;i < DataToPackage;i ++)
	{
		switch(SourceData[i])
		{
		case 0xff:
			DestData[PackagerCount ++] = 0xfd;
			DestData[PackagerCount ++] = 0x00;
			DataToCheck += 2;
			break;
		case 0xfe:
			DestData[PackagerCount ++] = 0xfd;
			DestData[PackagerCount ++] = 0x01;
			DataToCheck += 2;
			break;
		case 0xfd:

⌨️ 快捷键说明

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