如何在Serialport上发送十六进制数据
如何在SerialPort上发送十六进制的数据?
我使用了此功能,收到“是的,我可以写入端口”,但我没有收到我输入的数据
QByteArray send_data;
if(serialPort->isWritable())
{
qDebug()<<"Yes, I can write to port!";
int size = sizeof(send_data);
serialPort->write(send_data,size);
}
send_data += static_cast<char>(0xAA);
serialPort->write(send_data);
How to send data in hex on SerialPort?
I used this function, I receive the "yes, I can write to port" but I do not receive the data I entered
QByteArray send_data;
if(serialPort->isWritable())
{
qDebug()<<"Yes, I can write to port!";
int size = sizeof(send_data);
serialPort->write(send_data,size);
}
send_data += static_cast<char>(0xAA);
serialPort->write(send_data);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数据以二进制传输(本质上是0和1的序列)。无论。在十六进制而不是字符的字符串中显示数据只是一个选择。
在下面的示例中,您可以看到Array String_c使用与代码中使用的字符串相同的字符串初始化。接下来,我将数据打印在两者和字符串中。您可以看到唯一的区别是我决定打印数据的方式。两者的源数据都是相同的。
您可以在此处查看上面的代码: https://onlinegdb.com/y7fwamtdoq
注:我得到了功能:我得到了功能printcharinhexadecimal从这里: https://helloacm.com/the -c-cunction-to-print-a-char-array-ray-in-hexadecimal/
Data are transmitted in binary (essentially a sequence of 0 and 1). No matter what. Showing data in hexadecimal rather than a string of characters is just a choice.
In the following example, you can see that the array string_c is initialized with the same string that you are using in your code. Next, I print the data in both, as hex and as a string. You can see that the only difference is in the way I decided to print the data. The source data is the same for both.
You can see the above code running here: https://onlinegdb.com/Y7fwaMTDoq
Note: I got the function printCharInHexadecimal from here: https://helloacm.com/the-c-function-to-print-a-char-array-string-in-hexadecimal/
如您所怀疑,您对
sizeof
的使用是错误的。它没有返回包含数据的大小,而是返回一个非零常数,该常数是qbyTearray
对象本身的大小。由于该对象是新鲜构造的,它应该是空的,并且您在第一个写入中使用的任何大小都会导致不确定的行为。使用:完全跳过第一个写作,然后将上述用作第二个写作。
As suspected, your use of
sizeof
is wrong. It is not returning the size of the contained data, it is returning a non-zero constant that is the size of aQByteArray
object itself. Since that object was freshly constructed it should be empty, and any size you use in the first write other than zero will lead to undefined behavior. Use:Skip the first write entirely, and use the above for your second write.
您需要清楚自己的期望。源代码中的0xaA只是使用HEX 表示形式的整数值。无论源代码表示如何:
0xaa == 170 == 0263
,它都符合完全相同的代码。如果您实际上打算在运行时间输出一串字符串,代表十六进制中的值,则需要将该值从整数转换为字符串。例如;
将输出ASCII字符
AA
,同时证明了170至0xAA的等效性。这就是源中的十六进制符号不会影响值或如何在编译机代码中存储或表示的值。You need to be clear about what you expect. 0xAA in your source code is simply an integer value using hex representation. It complies to exactly the same code regardless of the source code presentation:
0xAA == 170 == 0263
.If you actually intended to output a string of characters at run time representing a value in hexadecimal, you need to convert that value from an integer to a string. For example;
will output ASCII characters
AA
, whilst demonstrating the equivalence of 170 to 0xAA. That is the hex notation in the source does not affect the value or how it is stored or represented in the compiled machine code.