Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending Hex data to a serial device
Forum Updated to NodeBB v4.3 + New Features

Sending Hex data to a serial device

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 11.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alfredo Palhares
    wrote on last edited by
    #1

    Hello everyone,

    I am researching on ways to send hex data to a serial device. Currently using QSerialPort to connect to the device.

    As an example I need to send the following data through serial:

     0000 0000 0000 0000 0000 0000 0000 00aa
     0000 0202 cc00 0000 0000 0000 0000 0000
     0a                                       
    

    Would QByteArray be a good option ? Can anyone give me a pratical example ?

    ad1170A 1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Hexadecimal is a human readable text representation of binary data,
      Do you want to send the hex string or the actual binary values it represents?

      If you have this string in a QByteArray:

      QByteArray b("0000 0000 0000 0000 0000 0000 0000 00aa")
      

      and your want to send the hex string then you simply use this with QIODevice::write(). If you want to send the binary values that string represents then you have to pre-process it to convert from the human-readable string to the equivalent bytes. That might be as trivial as:

      QByteArray t = b.replace(" ", ""); // remove the spaces
      QByteArray bytes = b.fromHex(); // convert from hex to binary
      device.write(bytes);
      

      but it may be more complicated if the pairs of bytes represent 16-bit integers because you may have to consider byte order.

      A 1 Reply Last reply
      1
      • C ChrisW67

        Hexadecimal is a human readable text representation of binary data,
        Do you want to send the hex string or the actual binary values it represents?

        If you have this string in a QByteArray:

        QByteArray b("0000 0000 0000 0000 0000 0000 0000 00aa")
        

        and your want to send the hex string then you simply use this with QIODevice::write(). If you want to send the binary values that string represents then you have to pre-process it to convert from the human-readable string to the equivalent bytes. That might be as trivial as:

        QByteArray t = b.replace(" ", ""); // remove the spaces
        QByteArray bytes = b.fromHex(); // convert from hex to binary
        device.write(bytes);
        

        but it may be more complicated if the pairs of bytes represent 16-bit integers because you may have to consider byte order.

        A Offline
        A Offline
        Alfredo Palhares
        wrote on last edited by
        #3

        @ChrisW67 says:

        QByteArray t = b.replace(" ", ""); // remove the spaces
        QByteArray bytes = b.fromHex(); // convert from hex to binary
        device.write(bytes);
        

        I've tried this but fromHex is a static method. Gonna give some feedback on data later on.

        J C 2 Replies Last reply
        0
        • A Alfredo Palhares

          @ChrisW67 says:

          QByteArray t = b.replace(" ", ""); // remove the spaces
          QByteArray bytes = b.fromHex(); // convert from hex to binary
          device.write(bytes);
          

          I've tried this but fromHex is a static method. Gonna give some feedback on data later on.

          J Offline
          J Offline
          JohnYork
          wrote on last edited by JohnYork
          #4

          @Alfredo-Palhares
          Hi, QByteArray should work properly on binary datas, even if there are many bytes which are not ASCII charactors. For QByteArray represent and save it's content length with it's own member variable, instead of working as a null-terminated string.
          For initializing a QByteArray instance with a binary sequence like your example, you can write code like this:

          QByteArray foo("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xaa\
          \x00\x00\x02\x02\xcc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
          \x0a");
          

          For sending these datas through a QSerialPort object, you can simply do it like this:

          serialport.write(foo);
          
          1 Reply Last reply
          0
          • A Alfredo Palhares

            @ChrisW67 says:

            QByteArray t = b.replace(" ", ""); // remove the spaces
            QByteArray bytes = b.fromHex(); // convert from hex to binary
            device.write(bytes);
            

            I've tried this but fromHex is a static method. Gonna give some feedback on data later on.

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by ChrisW67
            #5

            @Alfredo-Palhares
            Indeed, QByteArray::fromHex() is static, my bad.

            QByteArray t = b.replace(" ", ""); // remove the spaces
            QByteArray bytes = QByteArray::fromHex(t); // convert from hex to binary
            device.write(bytes);
            
            1 Reply Last reply
            0
            • A Alfredo Palhares

              Hello everyone,

              I am researching on ways to send hex data to a serial device. Currently using QSerialPort to connect to the device.

              As an example I need to send the following data through serial:

               0000 0000 0000 0000 0000 0000 0000 00aa
               0000 0202 cc00 0000 0000 0000 0000 0000
               0a                                       
              

              Would QByteArray be a good option ? Can anyone give me a pratical example ?

              ad1170A Offline
              ad1170A Offline
              ad1170
              wrote on last edited by
              #6

              @Alfredo-Palhares

              you can also use a uin16_t array.

              uint16_t data[] = {0x0000, 0x0000, 0x0000, ... 0x00AA, 0x0000, 0x0202 ...};

              serial->write(data, sizeof(data));

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved