CSerialPort Tutorial 4.3.x (3) - The use of CSerialPort in MFC

CSerialPort Tutorial 4.3.x (3) - The use of CSerialPort in MFC

environment:

系统:windows 10 64位
编译器:Visual Studio 2008

foreword

The CSerialPort project is a lightweight open source cross-platform serial port class library based on C/C++, which can easily realize serial port reading and writing across platforms and multiple operating systems, and also supports C#, Java, Python, Node.js, etc.

The open source protocol of the CSerialPort project has adopted the GNU Lesser General Public License v3.0 since version V3.0.0.171216

In order to allow developers to better use CSerialPort for development, a CSerialPort tutorial series based on version 4.3.x is specially written.

CSerialPort project address:

MFC complete sample program address:

  • https://github.com/itas109/CSerialPort/tree/master/examples/CommMFC
  • https://gitee.com/itas109/CSerialPort/tree/master/examples/CommMFC

1. Create a new dialog-based MFC project

Create a new dialog-based MFC project, the solution name is CommMFC

Download the CSerialPort source code in the CommMFC solution directory

$ cd CommMFC
$ git clone https://github.com/itas109/CSerialPort

The directory structure is as follows:

D:/CommMFC $ tree
.
+--- CommMFC
|   +--- CommMFC.aps
|   +--- CommMFC.cpp
|   +--- CommMFC.h
|   +--- CommMFC.rc
|   +--- CommMFC.vcproj
|   +--- CommMFCDlg.cpp
|   +--- CommMFCDlg.h
|   +--- ReadMe.txt
|   +--- res
|   |   +--- CommMFC.ico
|   |   +--- CommMFC.rc2
|   +--- Resource.h
|   +--- stdafx.cpp
|   +--- stdafx.h
|   +--- targetver.h
+--- CommMFC.sln
+--- CSerialPort
|   +--- include
|   |   +--- CSerialPort
|   |   |   +--- SerialPort.h
|   |   |   +--- SerialPortInfo.h
|   +--- src
|   |   +--- SerialPort.cpp
|   |   +--- SerialPortBase.cpp
|   |   +--- SerialPortInfo.cpp
|   |   +--- SerialPortInfoBase.cpp
|   |   +--- SerialPortInfoWinBase.cpp
|   |   +--- SerialPortWinBase.cpp

2. Set CSerialPort header file

Right-click [CommMFC Root Namespace] - [Properties] - [C/C++] - [General] - [Additional Include Directory] - Add the header file directory of CSerialPort

D:\CommMFC\CSerialPort\include

or

$(ProjectDir)\..\CSerialPort\include

3. Add CSerialPort source file

Right-click [CommMFC Root Namespace] - [Add] - [New Filter (named CSerialPort)]

Right-click [CSerialPort Filter] - [Add] - [Existing Item] - add the required files of the src directory of CSerialPort ()

The list of required documents is as follows:

  • SerialPort.cpp
  • SerialPortBase.cpp
  • SerialPortWinBase.cpp
  • SerialPortInfo.cpp
  • SerialPortInfoBase.cpp
  • SerialPortInfoWinBase.cpp

Notice:

It is necessary to set the precompiled header of the added cpp file to "Do not use precompiled header", such as right click [serialport.cpp] - [Properties] - [C/C++] - [Precompiled header] - [Precompiled header: No Using precompiled headers]

If not set, an error will be reported:

serialport.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"?
SerialPortBase.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"?
SerialPortWinBase.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"?
SerialPortInfo.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"?
SerialPortInfoBase.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"?
SerialPortInfoWinBase.cpp: fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"?

4. Add the necessary dependency library of CSerialPort

The required dependency library for CSerialPort under windows issetupapi.lib

Right-click [CommMFC Root Namespace] - [Properties] - [Linker] - [Input] - [Additional Dependencies] - Addsetupapi.lib

5. Add CSerialPort code in MFC

5.1 Add CSerialPort's header file, inheritance class, receiving function and CSerialPort instance object

in CommMFCDlg.hthe file

  • Add header file of CSerialPort
  • CCommMFCDlg class inheritanceCSerialPortListener
  • Add receiver functiononReadEvent(const char *portName, unsigned int readBufferLen)
  • Increase the instance object of CSerialPort

code show as below:

// CommMFCDlg.h : 头文件
//

#pragma once

// add by itas109
#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"
using namespace itas109;
// end by itas109


// CCommMFCDlg 对话框
class CCommMFCDlg : public CDialog, public CSerialPortListener // add by itas109
{
	...

	// add by itas109
private:
	void onReadEvent(const char *portName, unsigned int readBufferLen);
    // end by itas109
    
    // add by itas109
private:
	CSerialPort m_serialPort;
	// end by itas109
};

Note:
If CCommMFCDlg does not inherit CSerialPortListener, an error will be reported when calling the connectReadEvent function

CSerialPort::connectReadEvent: 不能将参数 1 从CCommMFCDlg *const 转换为itas109::CSerialPortListener *

5.2 Add related implementation code of serial port

CommMFCDlg.cppAdded in the file

  • CCommMFCDlg::OnInitDialog()Add the test code of CSerialPort in
  • Increase the implementation of the OnReceive function
// CommMFCDlg.cpp: 实现文件
...

BOOL CCommMFCDlg::OnInitDialog()
{
	...

	// TODO: 在此添加额外的初始化代码
	// add by itas109
	m_serialPort.connectReadEvent(this);

	m_serialPort.init("COM1");
	m_serialPort.open();

	if (m_serialPort.isOpen())
	{
		m_serialPort.writeData("itas109", 7);
	}
	else
	{
		MessageBox(_T("open failed"));
	}
	// end by itas109
	
	...
}

// add by itas109
void CCommMFCDlg::onReadEvent(const char *portName, unsigned int readBufferLen)
{
	if(readBufferLen > 0)
	{
		char data[1024];
		int recLen = m_serialPort.readData(data,readBufferLen > 1023 ? 1023 : readBufferLen);

		if (recLen > 0)
		{
			data[recLen] = '\0';

			CString cstr;
			cstr.Format(_T("OnReceive - data: %s, size: %d"), CString(data), recLen);
			MessageBox(LPCTSTR(cstr));
		}
	}
}
// end by itas109

6. Results

The serial port corresponding to COM2 in the code is the RS232 loopback test hardware, so the corresponding result is that after the program starts, initialize and open the serial port COM1, send data, and then pop up a box to prompt the received data (such as itas09OnReceive - data: itas109, size: 7 )


License

License under CC BY-NC-ND 4.0: Attribution-Noncommercial Use-No Derivatives


Reference:

  1. https://github.com/itas109/CSerialPort
  2. https://gitee.com/itas109/CSerialPort
  3. https://blog.csdn.net/itas109

Guess you like

Origin blog.csdn.net/itas109/article/details/132389533
Recommended