Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Serial Port debugging Assistant with Qt

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve Qt serial debugging assistant, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. Preface

This work was done many years ago, after seven or eight years of improvement, of course, not wholeheartedly perfect, that is, continuous improvement according to the needs of the actual project, especially the function of analog equipment to recover data. This is very practical in many host computer development using Qt. After all, many hardware and software projects are started at the same time, it will not be said to wait until the hardware or software has been completed to do another part. So software engineers need to get the software up according to the communication protocol agreed at the beginning. What to do without hardware field testing? they can only write their own tools to simulate device response data. Basically, they are required to receive what data to reply to what data, so it is possible for general serial port devices to reply data.

In fact, after Qt5 began to integrate the QSerialPort class can be used for serial communication, I personally tested many times, found that there is always an inexplicable strange problem, or intend to directly use the third-party serial open source class to do, after all, mature and stable, and experienced a variety of projects for a long time test, or very reliable.

Basic functions:

Support hexadecimal data sending and receiving.

Support serial communication above COM9 under windows.

Real-time display of sending and receiving data byte size and serial port status.

Any qt version is supported, which can be tested in person.

Support serial port to network data sending and receiving.

Advanced features:

You are free to manage the data you need to send, and you only need to select the data from the drop-down box each time, without having to re-enter the data.

Can simulate the device response data, you need to open the simulation device response data in the main interface. When you receive the set instruction, reply to the set reply instruction immediately. For example, if you specify that you need to reply to 0x16 0x00 0xFE 0x01 when you receive a 0x16 0x00 0xFF 0x01, you only need to add a piece of data 1600 FF 01:16 00 FE 01 to the SendData.txt.

The data can be sent and saved to the text file regularly: the default interval is 5 seconds, and the interval time can be changed.

When you continue to receive a large amount of data, you can pause the display of the data to view the specific data, the background still receives the data but does not process it, and there is no need to close the serial port to view the received data.

Each time the data received is a complete piece of data, not disjointed, do delay processing.

A set of source code is compiled everywhere and does not need to change the serial communication class. It has been successfully compiled and run under the XP/WIN7/UBUNTU/ARMLINUX system.

Second, the first step of the code: introduce the third-party serial port class include ($PWD/qextserialport/qextserialport.pri) INCLUDEPATH + = $$PWD/qextserialport second step: instantiate the class void frmComTool::on_btnOpen_clicked () {if (ui- > btnOpen- > text () = = "Open serial port") {com = new QextSerialPort (ui- > cboxPortName- > currentText (), QextSerialPort::Polling); comOk = com- > open (QIODevice::ReadWrite) If (comOk) {/ / clear buffer com- > flush (); / / set baud rate com- > setBaudRate ((BaudRateType) ui- > cboxBaudRate- > currentText () .toInt ()) / / set the data bit com- > setDataBits ((DataBitsType) ui- > cboxDataBit- > currentText () .toInt ()); / / set the check bit com- > setParity ((ParityType) ui- > cboxParity- > currentIndex ()) / / set stop bit com- > setStopBits ((StopBitsType) ui- > cboxStopBit- > currentIndex ()); com- > setFlowControl (FLOW_OFF); com- > setTimeout (10); changeEnable (true); ui- > btnOpen- > setText ("close serial port") TimerRead- > start ();}} else {timerRead- > stop (); com- > close (); com- > deleteLater (); changeEnable (false); ui- > btnOpen- > setText ("open serial port"); on_btnClear_clicked (); comOk = false Step 3: read the data and process void frmComTool::readData () {if (com- > bytesAvailable () readAll (); int dataLen = data.length (); if (dataLen ckHexReceive- > isChecked ()) {buffer = QUIHelper::byteArrayToHexStr (data);} else {/ / buffer = QUIHelper::byteArrayToAsciiStr (data)) Buffer = QString::fromLocal8Bit (data);} / / enable debugging to simulate debugging data if (ui- > ckDebug- > isChecked ()) {int count = App::Keys.count (); for (debug I = 0; I

< count; i++) { if (buffer.startsWith(App::Keys.at(i))) { sendData(App::Values.at(i)); break; } } } append(1, buffer); receiveCount = receiveCount + data.size(); ui->

BtnReceiveCount- > setText (QString ("receive:% 1 byte") .arg (receiveCount)); / / enable network forwarding to call network send data if (tcpOk) {socket- > write (data); append (4, QString (buffer));}} 3. Effect diagram

These are all the contents of this article entitled "how to implement Serial Port debugging Assistant in Qt". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 269

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report