当前位置:网站首页>Data conversion problem in Qt development serial communication software: when reading_ Qbytearray to string; When sending_ Data format; Int to hexadecimal format string; Intercept characters in string
Data conversion problem in Qt development serial communication software: when reading_ Qbytearray to string; When sending_ Data format; Int to hexadecimal format string; Intercept characters in string
2022-07-24 02:25:00 【Old city in October】
Using Qt In the process of serial port program development , In addition to the function of serial communication , Data conversion is also a troublesome thing , This article mainly introduces the problem of data conversion in serial communication , About the function realization, you can find relevant cases by searching online .
After completing the initialization of serial port and other functions , Realize data reading and conversion .
Refer to the post :Qt Summary 15 :QByteArray Detailed explanation ;Qt Simple application of hexadecimal , Hexadecimal addition ;QString And QByteArray The method of mutual conversion ;
1. Read serial port command -QByteArray turn QString,QString turn float
The data received from the serial port looks like :“0010101821....” Of 16 Hexadecimal number , Serial port returns QByteArray Data format , Convert to QString, Then by intercepting some bits in the data , according to 16 Convert hexadecimal format to 10 Use .
- QByteArray turn QString
static QString serialBuffer;
QByteArray serialBufferTmp=m_serialPort->readAll();
serialBuffer=serialBuffer.append(serialBufferTmp);
- Cut off the 8 Behind you 4 Bit 16 Hexadecimal number , and QString turn float
QString strValue=serialBuffer.mid(8,4);
float floatValue=strValue.toInt(nullptr,16)
2. Write a command to the serial port
- Decimal system int turn 16 Base format string
Usually we go from Qt Used in the developed interface QLineEdit The input is decimal , For serial port processing, it needs to be converted to 16 Base format , Then it can be processed into a format that can be parsed by the serial port , For example, the decimal system 50 Convert to 16 It's binary “32”, The code is as follows :
int Num=dec2HexInputEdit->text().toInt();
QString hexString=QString::number(Num,16)
- 16 Hexadecimal number addition
For reference here Qt Simple application of hexadecimal , Hexadecimal addition , It first converts numbers into 10 Base number , Add and convert to QString
QString hexAri(QString str, int addend)
{
bool ok;
int dec = str.toUInt(&ok, 16);
QString tmp = QString("%1").arg(dec + addend, 5, 16, QLatin1Char('0');
return tmp;
}
- string turn ByteAr
Reference resources QString And QByteArray The method of mutual conversion ;
QByteArray SettingArray=Setting.toLatin1();
边栏推荐
- Crop leaf disease identification system
- Canvas drawing (mouse click to draw and lift to end)
- Opensmile introduction and problems encountered during installation
- 关于 SAP Fiori 应用的离线使用
- 我国科学家在高安全量子密钥分发网络方面取得新进展
- Leetcode exercise -- two questions about the nearest common ancestor of binary trees
- [C language] preprocessing details
- Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
- 奔走相告,行情与量化页面功能优化!股票量化分析工具QTYX-V2.4.5
- 营员招募|心怀世界的AI青年们,联合国需要你为可持续发展助力!
猜你喜欢

Responsive layout a web page displays different effects on different devices) meta:vp

Reconnaître le Protocole de couche de transport - TCP / UDP

Pbootcms template calls the tag ordinal number from 2 or automatic number

Diablo king, analysis of low illumination image enhancement technology

组件el-scrollbar的使用
![[untitled]](/img/36/8dc8aa76fbcd7fdb86cd0b7b4338c7.jpg)
[untitled]

JDBC tool class

Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
In depth understanding of the underlying framework of wechat applet (II) component system, exprser

Sharing a case of controller restart caused by a CIFS bug in NetApp Fas series
随机推荐
Deliver temperature with science and technology, vivo protects the beauty of biodiversity
Idea's gradle project Chinese garbled
Crud operation of mongodb (2)
Jar package used by jsonarray in main function provided by leetcode
分享一个基于Abp 和Yarp 开发的API网关项目
Running around, market and quantitative page function optimization! Stock quantitative analysis tool qtyx-v2.4.5
BPG notes (III)
STM32 installation tutorial and j-link burning driver installation tutorial [the next day]
Halide::Generator生成器使用说明
wallys/WiFi6 MiniPCIe Module 2T2R2 × 2.4GHz 2x5GHz MT7915 MT7975
[untitled]
Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
Loadrunner12 installation, recording the first script and the proxy server did not respond to the solution
Build a CPU Simulator
网络协议详解:TCP Part1
因果学习开源项目:从预测到决策!
毕业设计校园信息发布平台网站源码
[important notice] the third phase of planet online training is coming! Explain how to build your own quantitative strategy on qtyx
【FPGA教程案例39】通信案例9——基于FPGA的交织-解交织数据传输
【补题日记】[2022牛客暑期多校2]K-Link with Bracket Sequence I