如何在Serialport上发送十六进制数据

发布于 2025-02-13 01:01:57 字数 357 浏览 1 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贪了杯 2025-02-20 01:01:57

数据以二进制传输(本质上是0和1的序列)。无论。在十六进制而不是字符的字符串中显示数据只是一个选择。

在下面的示例中,您可以看到Array String_c使用与代码中使用的字符串相同的字符串初始化。接下来,我将数据打印在两者和字符串中。您可以看到唯一的区别是我决定打印数据的方式。两者的源数据都是相同的。

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
void printCharInHexadecimal(const char* str, int len) 
{
   for (int i = 0; i < len; ++ i) {
     uint8_t val = str[i];
     char tbl[] = "0123456789ABCDEF";    
     printf("0x");
     printf("%c", tbl[val / 16]);
     printf("%c", tbl[val % 16]);
     printf(" ");
   }
   printf("\n");
}

int main()
{
  char string_c[] = "Yes, i can write to port";
  // string printed in hex
  printCharInHexadecimal(string_c, 24);
  // same string printed as "text"
  printf("%s\n",string_c);
  return 0;
}

您可以在此处查看上面的代码: 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.

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
void printCharInHexadecimal(const char* str, int len) 
{
   for (int i = 0; i < len; ++ i) {
     uint8_t val = str[i];
     char tbl[] = "0123456789ABCDEF";    
     printf("0x");
     printf("%c", tbl[val / 16]);
     printf("%c", tbl[val % 16]);
     printf(" ");
   }
   printf("\n");
}

int main()
{
  char string_c[] = "Yes, i can write to port";
  // string printed in hex
  printCharInHexadecimal(string_c, 24);
  // same string printed as "text"
  printf("%s\n",string_c);
  return 0;
}

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/

罪歌 2025-02-20 01:01:57

如您所怀疑,您对sizeof的使用是错误的。它没有返回包含数据的大小,而是返回一个非零常数,该常数是qbyTearray对象本身的大小。由于该对象是新鲜构造的,它应该是空的,并且您在第一个写入中使用的任何大小都会导致不确定的行为。使用:

int size = (int)send_data.size();

完全跳过第一个写作,然后将上述用作第二个写作。

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 a QByteArray 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:

int size = (int)send_data.size();

Skip the first write entirely, and use the above for your second write.

温柔戏命师 2025-02-20 01:01:57

您需要清楚自己的期望。源代码中的0xaA只是使用HEX 表示形式的整数值。无论源代码表示如何:0xaa == 170 == 0263,它都符合完全相同的代码。

如果您实际上打算在运行时间输出一串字符串,代表十六进制中的值,则需要将该值从整数转换为字符串。例如;

char hexbyte[3] ;
sprintf( hexbyte, "%02X", 170 ) ;
serialPort->write(send_data) ;

将输出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;

char hexbyte[3] ;
sprintf( hexbyte, "%02X", 170 ) ;
serialPort->write(send_data) ;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文