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

📄 serialporttestdlg.cpp

📁 串口调试软件(附带源码)
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************************************/
/*                                             测温仪上位机程序                                     */
/*                                             光纤长度: 1.5米                                      */
/*                                             测温范围: 600℃~ 1800℃                              */
/*                                             公司名称: 陕西瑞光自动化仪表有限公司                 */
/*                                             职    务: 电子工程师                                 */
/*                                             作    者: 庄建超                                     */
/*                                             时    间: 2010年12月25日                             */
/*                                             版    本: V1.00                                      */
/****************************************************************************************************/
// SerialPortTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SerialPortTest.h"
#include "SerialPortTestDlg.h"
//#include   <stdlib.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()

/////////////////////////////////////////////////////////////////////////////
// CSerialPortTestDlg dialog

CSerialPortTestDlg::CSerialPortTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSerialPortTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSerialPortTestDlg)
	m_strEditReceiveMsg = _T("");
	m_strEditReceiveMsg2 = _T("");
	m_strEditReceiveMsg3 = _T("");
	strOpen=0;
	strCommData= _T("");
	m_strEditSendMsg = _T(" Hello ");//串口发送缓冲区初始化
	//m_strEditComBorte = _T("9600");
	m_strEditDAslope = _T("0.99");//DA斜率初始化
	m_strEditDATop = _T("0000");//DA零点加初始化
	m_strEditDABtom = _T("0000");//DA零点减初始化
	m_strEditAdd =_T("00");//仪器地址初始化
	m_strEditAddG =_T("00");//仪器地址初始化
	Comm_Add =_T("00");//仪器地址标志
	m_strEditTop =_T("1800");//4-20mA上限初始化
	m_strEditBtom =_T("600");//4-20mA下限初始化
	m_strEditHuanwen=_T("20.0");
	m_charLRC=0;//校验和初始化
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bSerialPortOpened=FALSE; //初始状态:串口没有打开
}

void CSerialPortTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerialPortTestDlg)
	DDX_Control(pDX, IDC_COMBO_COMPORT, m_ctrlComboComPort);
	DDX_Control(pDX, IDC_COMBO_COMPORT2, m_ctrlComboParty);
	DDX_Control(pDX, IDC_COMBO_COMPORT3, m_ctrlComBorte);
	DDX_Control(pDX, IDC_EDIT_RECEIVEMSG, RXD_EDIT);
	DDX_Control(pDX, IDC_EDIT_RECEIVEMSG2, RXD_EDIT2);	
	DDX_Text(pDX, IDC_EDIT5, m_strEditDAslope);
	DDX_Text(pDX, IDC_EDIT6, m_strEditDATop);
	DDX_Text(pDX, IDC_EDIT7, m_strEditDABtom);
	DDX_Text(pDX, IDC_EDIT3, m_strEditAdd);
	DDX_Text(pDX, IDC_EDIT_AddG, m_strEditAddG);
	DDX_Text(pDX, IDC_EDIT9, m_strEditTop);
	DDX_Text(pDX, IDC_EDIT10, m_strEditBtom);
	DDX_Text(pDX, IDC_EDIT8, m_strEditReceiveMsg3);
	DDX_Text(pDX, IDC_EDIT_RECEIVEMSG, m_strEditReceiveMsg);
	DDX_Text(pDX, IDC_EDIT_RECEIVEMSG2, m_strEditReceiveMsg2);	
	DDX_Text(pDX, IDC_EDIT_SENDMSG, m_strEditSendMsg);
	DDX_Text(pDX, IDC_EDIT_huanwen, m_strEditHuanwen);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSerialPortTestDlg, CDialog)
	//{{AFX_MSG_MAP(CSerialPortTestDlg)
	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_SEND, OnButtonSend)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CSerialPortTestDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CSerialPortTestDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON13, &CSerialPortTestDlg::OnBnClickedButton13)
	ON_BN_CLICKED(IDC_BUTTON7, &CSerialPortTestDlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON11, &CSerialPortTestDlg::OnBnClickedButton11)	
	ON_BN_CLICKED(IDC_BUTTON10, &CSerialPortTestDlg::OnBnClickedButton10)
	ON_BN_CLICKED(IDC_BUTTON14, &CSerialPortTestDlg::OnBnClickedButton14)
	ON_BN_CLICKED(IDC_BUTTON15, &CSerialPortTestDlg::OnBnClickedButton15)
	ON_BN_CLICKED(IDC_BUTTON12, &CSerialPortTestDlg::OnBnClickedButton12)	
	ON_BN_CLICKED(IDC_BUTTON17, &CSerialPortTestDlg::OnBnClickedButton17)
	ON_BN_CLICKED(IDC_BUTTON18, &CSerialPortTestDlg::OnBnClickedButton18)	
	ON_BN_CLICKED(IDC_BUTTON8, &CSerialPortTestDlg::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON9, &CSerialPortTestDlg::OnBnClickedButton9)	
	ON_BN_CLICKED(IDC_BUTTON_duqufashe, &CSerialPortTestDlg::OnBnClickedButtonduqufashe)	
	ON_BN_CLICKED(IDC_xielv, &CSerialPortTestDlg::OnBnClickedxielv)
	ON_BN_CLICKED(IDC_wendu, &CSerialPortTestDlg::OnBnClickedwendu)
	ON_BN_CLICKED(IDC_huanwen, &CSerialPortTestDlg::OnBnClickedhuanwen)
	ON_BN_CLICKED(IDC_biaoding, &CSerialPortTestDlg::OnBnClickedbiaoding)
	ON_BN_CLICKED(IDC_wendu2, &CSerialPortTestDlg::OnBnClickedwendu2)
	ON_BN_CLICKED(IDC_wendu3, &CSerialPortTestDlg::OnBnClickedwendu3)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerialPortTestDlg message handlers

BOOL CSerialPortTestDlg::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
	m_ctrlComboParty.SetCurSel(2);
	m_ctrlComBorte.SetCurSel(3); //初始选择波特率
	//以下两句分别设置“打开串口”、“关闭串口”两个按状态的使能状态
	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 CSerialPortTestDlg::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 CSerialPortTestDlg::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 CSerialPortTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

LONG CSerialPortTestDlg::OnComm(WPARAM ch, LPARAM port)
{
	m_strEditReceiveMsg=m_strEditReceiveMsg+(wchar_t)ch;
	CString str;	
	CString str1;	
		//str.Format("%02X %02X ",ch>>4&0x0f,ch&0x0f);
	str.Format("%02X ",ch);
	m_strEditReceiveMsg2=m_strEditReceiveMsg2+str;
	//m_strEditReceiveMsg2.Format("%02X ",ch);
	//RXD_EDIT2.ReplaceSel(m_strEditReceiveMsg);	

	if(str=="0D ") 
	{	
		str1=strCommData.Right(2);//提取数据包校验和
		str.Format("%02X",m_charLRC);		
		//str1.MakeLower(); 
		//str.MakeLower();
		
		if(str1==str)				//比较校验和
		{
			if((m_strSComm=="obj")||(m_strSComm=="amb"))//读取温度
			{
			
			m_strEditReceiveMsg3=strCommData;
			m_strEditReceiveMsg3=m_strEditReceiveMsg3.Left(10);//提取前10个字符
			m_strEditReceiveMsg3=m_strEditReceiveMsg3.Right(4);//提取后4个字符
			m_strEditReceiveMsg3=m_strEditReceiveMsg3.TrimLeft("0");//删除0
			if(m_strEditReceiveMsg3=="")m_strEditReceiveMsg3="0";			
			}
			if(m_strSComm=="emm")//读取发射率
			{	
			
			m_strEditDAslope=strCommData;
			m_strEditDAslope=m_strEditDAslope.Left(9);//提取前9个字符
			m_strEditDAslope=m_strEditDAslope.Right(3);//提取后3个字符
			m_strEditDAslope=m_strEditDAslope.TrimLeft("0");//删除0
			m_strEditDAslope.Format("%g",atof(m_strEditDAslope)/100);//除以100
			if(m_strEditDAslope=="")m_strEditDAslope="0";			
			}
			if(m_strSComm=="emm_G")//设置发射率
			{	
			
			m_strEditDAslope=strCommData;
			m_strEditDAslope=m_strEditDAslope.Left(8);//提取前8个字符
			m_strEditDAslope=m_strEditDAslope.Right(2);//提取后2个字符
			
			if(m_strEditDAslope=="OK")AfxMessageBox("设置发射率成功");	

			m_strEditDAslope="0";			
			}
			if(m_strSComm=="Add")//设置地址
			{
			m_strEditReceiveMsg3=strCommData;
			m_strEditReceiveMsg3=m_strEditReceiveMsg3.Left(8);//提取前8个字符
			m_strEditReceiveMsg3=m_strEditReceiveMsg3.Right(2);//提取后2个字符
			
			if(m_strEditReceiveMsg3=="OK")AfxMessageBox("设置地址成功");	
			
			m_strEditReceiveMsg3="0";			
			}
			if(m_strSComm=="Laser_Open")//打开激光
			{
			m_strEditReceiveMsg3=strCommData;
			m_strEditReceiveMsg3=m_strEditReceiveMsg3.Left(8);//提取前8个字符
			m_strEditReceiveMsg3=m_strEditReceiveMsg3.Right(2);//提取后2个字符
			

⌨️ 快捷键说明

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