当前位置:网站首页>Qt开发串口通讯软件中的数据转换问题:读取时_QByteArray转string;发送时_数据格式;int转16进制格式string;string中截取字符;16进制数加法;string转ByteAr
Qt开发串口通讯软件中的数据转换问题:读取时_QByteArray转string;发送时_数据格式;int转16进制格式string;string中截取字符;16进制数加法;string转ByteAr
2022-07-24 02:22:00 【十月旧城】
在利用Qt进行串口程序开发过程中,除了与串口通讯的功能实现以外,数据的转换也是比较麻烦的一件事,本篇主要介绍串口通讯中数据转换的问题,关于功能实现大家在网上搜索就能找到相关案例。
在完成串口的初始化等功能之后,实现数据的读取转换。
参考博文:Qt总结之十五:QByteArray详解;Qt 十六进制的简单应用, 十六进制的加法;QString与QByteArray互相转换的方法;
1.读取串口命令-QByteArray转QString,QString转float
从串口接收到的数据形如:“0010101821....”的16进制数,串口返回QByteArray的数据格式,通过以下方式转换为QString,再通过截取数据中某几位,按照16进制格式转换为10进制使用。
- QByteArray转QString
static QString serialBuffer;
QByteArray serialBufferTmp=m_serialPort->readAll();
serialBuffer=serialBuffer.append(serialBufferTmp);
- 截取第8位后4位的16进制数,并QString转float
QString strValue=serialBuffer.mid(8,4);
float floatValue=strValue.toInt(nullptr,16)
2.向串口写入命令
- 十进制int转16进制格式string
通常我们从Qt开发的界面中使用QLineEdit输入的是十进制类型的,而为了串口处理需要将其转换为16进制格式,再通过处理成串口可以解析的格式,例如十进制50转换为16进制的“32”,代码如下:
int Num=dec2HexInputEdit->text().toInt();
QString hexString=QString::number(Num,16)
- 16进制数加法
此处参考Qt 十六进制的简单应用, 十六进制的加法,它是先将数转换为10进制,相加后再转换为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转ByteAr
参考QString与QByteArray互相转换的方法;
QByteArray SettingArray=Setting.toLatin1();
边栏推荐
- 【MySQL】字符集utf8mb4无法存储表情踩坑记录
- Use the hiflow scene connector to view the epidemic situation in the region every day
- 原生组件、小程序与客户端通信原理、video、map、canvas、picker等运行原理
- In depth understanding of the underlying framework of wechat applet (II) component system, exprser
- POP3客户端代码的实现
- Draw pictures with canvas
- 通过Arduino IDE向闪存文件系统上传文件
- regular expression
- Deliver temperature with science and technology, vivo protects the beauty of biodiversity
- Redis 6.0 source code learning simple dynamic string
猜你喜欢

Sharing a case of controller restart caused by a CIFS bug in NetApp Fas series

ASP.NET CORE写一个缓存Attribute工具

ASP. Net core write a cache attribute tool

Deliver temperature with science and technology, vivo protects the beauty of biodiversity

【MySQL】字符集utf8mb4无法存储表情踩坑记录

Upload files to flash file system through Arduino IDE

通过Arduino IDE向闪存文件系统上传文件

餐饮连锁门店重塑增长背后的数字化转型

Wallys/PD-60 802.3AT Input Output802.3AT/AT 85% Efficiency 10/100/1000M GE Surge Protection

The new red envelope cover platform can build the source code of the independent background of the sub station
随机推荐
Halide:: generator instructions
毕业设计校园信息发布平台网站源码
微信小程序之性能优化(分包、运行流程细节、精简结构、原生组件通信)
wallys/WiFi6 MiniPCIe Module 2T2R2 × 2.4GHz 2x5GHz MT7915 MT7975
[untitled]
1000 okaleido tiger launched binance NFT, triggering a rush to buy
Understand the transport layer protocol - tcp/udp
C -- bit operation
Magazine feature: the metauniverse will reshape our lives, and we need to make sure it gets better
[C language operation of linked list (initialization, establishment, length calculation, addition, deletion, and output of linked list)]
College degree want to 0 basic programming after looking for a job feasible?
Responsive layout a web page displays different effects on different devices) meta:vp
Halide::Generator生成器使用说明
Cinq ans de contact avec près d'une centaine de patrons, en tant que chasseur de têtes, j'a i découvert que le secret de la promotion n'est que quatre mots
LeetCode 70爬楼梯、199二叉树的右视图、232用栈实现队列、143重排链表
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
【FPGA教程案例39】通信案例9——基于FPGA的交织-解交织数据传输
Qml- use listview to build a three-level treeview architecture
浅谈领域驱动设计
145-keep-alive的初步使用