使用qt5编写Serial Port

涉及信号-槽、多线程

RS-232 Testing Guide

thread

线程相关

asynchronously

connect方法已实现异步。

synchronously

qmutexlocker

创建信号锁

m_mutex.lock();

信号

为便于调用者获得线程的内部信息,我们需要提供信号。

signals:
void error(const QString &s);
void timeout(const QString &s);

program ui

Worker method

Init Port

设置私有成员变量

部分代码

m_waitTimeout = waitTimeout;

Receive data

打开串口

if (!m_serialPort->open(QIODevice::ReadWrite)) {
		qDebug() << tr("Can't open %1, error code %2")
			.arg(m_portName).arg(m_serialPort->error());
		m_serialPort->close();
		return false;
	}

接收数据

while (m_serialPort->waitForReadyRead(500))
            responseData += m_serialPort->read(1024);

Deploy for windows

copy exe to target directory.

copy C:\Users\tdtc\Documents\Projects\serialCommQt\Release\serialCommQt.exe C:\Users\tdtc\Documents\serialCommQt_qmake\

exec windeployqt:

  • for QML
C:\Qt\Qt5.12.11\5.12.11\msvc2017\bin\windeployqt --qmldir C:\Users\tdtc\Documents\SerialCom\SerialCom\QtRaspberry C:\Users\tdtc\Documents\SerialCom_qml\serialCom.exe
  • for Other
C:\Qt\Qt5.12.11\5.12.11\msvc2017\bin\windeployqt --dir C:\Users\tdtc\Documents\Projects\serialCommQt C:\Users\tdtc\Documents\serialCommQt_qmake\serialCommQt.exe

qt deploy files

About Qt Plugins

The following files are copied from “C:\Qt\Qt5.12.11\5.12.11\msvc2017\plugins\platforms”.

The “d” at the end of the file name means the debug version.

./platforms/qwindowsd.dll

All Qt GUI applications require a plugin that implements the Qt Platform Abstraction (QPA) layer in Qt 5.
For Windows, the name of the platform plugin is qwindows.dll.

Reference