-1

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);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
sam
  • 1
  • 3
  • 1
    If `send_data` is a pointer then `sizeof` is almost certainly the wrong thing to use. – Mark Ransom Jul 05 '22 at 15:51
  • So, what can i do ?please – sam Jul 05 '22 at 15:55
  • Is `send_data` a pointer? If you [edit] this post to include a [mre], we won't have to guess or ask about the code that you are not showing us. – Drew Dormann Jul 05 '22 at 15:56
  • No, send_data it's not a pointer, i used only this command for check if my serial Port can receive data – sam Jul 05 '22 at 16:00
  • 1
    You will likely have to add some more code for anyone to be able to understand your problem. Also have you considered that the problem can be on the side that receives the serial data? – drescherjm Jul 05 '22 at 16:01
  • 2
    You may also need to clarify what you mean by "send data in hex". The phrase "in hex" describes how to _display_ data. – Drew Dormann Jul 05 '22 at 16:12

3 Answers3

0

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/

Alexis
  • 576
  • 1
  • 10
  • 29
  • Have you not rather over complicated that? What is wrong with using a `%02X` format specifier? – Clifford Jul 06 '22 at 19:38
  • Yeah, probably I have gone too far. But the focus should remain on what I have written at the beginning. – Alexis Jul 06 '22 at 19:55
0

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165