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 via serial communication
QtWS25 Last Chance

Sending hex data via serial communication

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 1.3k Views
  • 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.
  • B Offline
    B Offline
    Bean_Noodle
    wrote on last edited by
    #1

    Hi. I am finding way to send hex data via serial communication
    i searched it several times and followed some ways but it didn't work.
    i checked that protocol is working with using other software that sending hex data to device
    below is my code

    @
    const char data[]={0xAA,0xAA,0x01,0x00,0x00,0x0E,0x00,0x01,0x00,0x00,0x00,0x2D,0x37,0x1D,0xAA,0xAA,0x01,0x00,0x00,0x0E,0x00,0x0C,0x10,0x00,0x00,0x01,0x76,0x13};

    serial->setPortName(("COM8"));
    initSerialPort(); // baud rate and etc

    if(serial->open(QIODevice::ReadWrite))
    {
        qDebug()<<"Port is open!";
        if(serial->isWritable())
        {
            qDebug()<<"Yes, i can write to port!";
           int size = sizeof(data);
           serial->write(data,size);
        }
    }
    

    @

    and when i use other declare like uint16_t, uchar, write function cannot convert argument 1 from uint16_t (or uchar) to const char *

    i did try also this form
    @

    QByteArray hex("AAAA0100000E00010000002D371DAAAA0100000E000C100000017613");
    QByteArray data = QByteArray::fromHex(hex);
    and it also didnt work

    JonBJ 1 Reply Last reply
    0
    • B Bean_Noodle

      Hi. I am finding way to send hex data via serial communication
      i searched it several times and followed some ways but it didn't work.
      i checked that protocol is working with using other software that sending hex data to device
      below is my code

      @
      const char data[]={0xAA,0xAA,0x01,0x00,0x00,0x0E,0x00,0x01,0x00,0x00,0x00,0x2D,0x37,0x1D,0xAA,0xAA,0x01,0x00,0x00,0x0E,0x00,0x0C,0x10,0x00,0x00,0x01,0x76,0x13};

      serial->setPortName(("COM8"));
      initSerialPort(); // baud rate and etc

      if(serial->open(QIODevice::ReadWrite))
      {
          qDebug()<<"Port is open!";
          if(serial->isWritable())
          {
              qDebug()<<"Yes, i can write to port!";
             int size = sizeof(data);
             serial->write(data,size);
          }
      }
      

      @

      and when i use other declare like uint16_t, uchar, write function cannot convert argument 1 from uint16_t (or uchar) to const char *

      i did try also this form
      @

      QByteArray hex("AAAA0100000E00010000002D371DAAAA0100000E000C100000017613");
      QByteArray data = QByteArray::fromHex(hex);
      and it also didnt work

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Bean_Noodle

      but it didn't work.
      and it also didnt work

      Do you get a compiler error on trying to write/send the data? In which case, copy & paste the error message and show the line number it occurs on. Or, do you get an error at the receiver side from the data you send, in which case state the error and clearly you are not sending what the receiver expects.

      We are not mind-readers, you have to supply the exact, correct information and what errors you get. "Sending hex data" is not in itself any problem, and you probably don't actually send hex anything, just bytes/words.

      B 1 Reply Last reply
      0
      • JonBJ JonB

        @Bean_Noodle

        but it didn't work.
        and it also didnt work

        Do you get a compiler error on trying to write/send the data? In which case, copy & paste the error message and show the line number it occurs on. Or, do you get an error at the receiver side from the data you send, in which case state the error and clearly you are not sending what the receiver expects.

        We are not mind-readers, you have to supply the exact, correct information and what errors you get. "Sending hex data" is not in itself any problem, and you probably don't actually send hex anything, just bytes/words.

        B Offline
        B Offline
        Bean_Noodle
        wrote on last edited by
        #3

        @JonB thank you for answering.
        there was no error message when i try to build with that code but device don't work at all.
        and when i declare data with form of uchar or uint16_t,

        cannot convert argument 1 from uint16_t (or uchar) to const char *

        this error message is come.

        JonBJ 1 Reply Last reply
        0
        • B Bean_Noodle

          @JonB thank you for answering.
          there was no error message when i try to build with that code but device don't work at all.
          and when i declare data with form of uchar or uint16_t,

          cannot convert argument 1 from uint16_t (or uchar) to const char *

          this error message is come.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Bean_Noodle said in Sending hex data via serial communication:

          there was no error message when i try to build with that code but device don't work at all.

          Then maybe what you are sending is not what the device expects, nobody here can know.

          and when i declare data with form of uchar or uint16_t,
          cannot convert argument 1 from uint16_t (or uchar) to const char *
          this error message is come.

          That is not showing the lines of code you try, both for the declarations and the call it fails on. How can we say what's wrong if you won't show what you are writing? I shan't ask again. In any case, yes, neither uint16_t nor uchar, nor pointers to them, are the same as char *, so you would need the correct cast.....

          Ketan__Patel__0011K 1 Reply Last reply
          1
          • JonBJ JonB

            @Bean_Noodle said in Sending hex data via serial communication:

            there was no error message when i try to build with that code but device don't work at all.

            Then maybe what you are sending is not what the device expects, nobody here can know.

            and when i declare data with form of uchar or uint16_t,
            cannot convert argument 1 from uint16_t (or uchar) to const char *
            this error message is come.

            That is not showing the lines of code you try, both for the declarations and the call it fails on. How can we say what's wrong if you won't show what you are writing? I shan't ask again. In any case, yes, neither uint16_t nor uchar, nor pointers to them, are the same as char *, so you would need the correct cast.....

            Ketan__Patel__0011K Offline
            Ketan__Patel__0011K Offline
            Ketan__Patel__0011
            wrote on last edited by
            #5

            @JonB

            Try This

            Convert Your String into Latin1

            And Simply send the data using write()

            serial->write(SERIAL_STRING.toLatin1());

            A 1 Reply Last reply
            0
            • Ketan__Patel__0011K Ketan__Patel__0011

              @JonB

              Try This

              Convert Your String into Latin1

              And Simply send the data using write()

              serial->write(SERIAL_STRING.toLatin1());

              A Offline
              A Offline
              Anonymous_Banned275
              wrote on last edited by
              #6

              @Ketan__Patel__0011
              I do not want to engage in semantic , but
              are you sending RAW hex - 8 bits data or "serial communication " 8 bit code?
              By "serial communication " I mean formatted bit stream including
              start bit - data - stop bits plus optional parity bit(s) ?

              'In other words - since you have stated "it does not work " - is your receiver expecting plain 8 bits raw data or properly formatted "serial communication "?

              And I won't mention " transmission rate" etc.

              Ketan__Patel__0011K 1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #7

                All we have to go on is, "it doesn't work." Could mean almost anything is wrong.

                What output does your compiler produce when compiling this file?
                What output does your code actually produce at run time on the debug console?
                Do not paraphrase it, copy and paste it; even if you think it compiles or runs cleanly.

                In initSerialPort() check the return value from every call to set baud rate, bit, parity, flow control etc. Are they all true (and correct)?

                Check the return value for the call to QSerialPort::write(). Is it the value you expect, something smaller, or -1?

                Understand that bytes may be queued but not actually put on the wire by QSerialPort (QIODevice) until the code returns to the Qt event loop, i.e. sending is not synchronous/blocking. Has your code returned to the Qt event loop before you declare it "does not work" ?

                1 Reply Last reply
                3
                • A Anonymous_Banned275

                  @Ketan__Patel__0011
                  I do not want to engage in semantic , but
                  are you sending RAW hex - 8 bits data or "serial communication " 8 bit code?
                  By "serial communication " I mean formatted bit stream including
                  start bit - data - stop bits plus optional parity bit(s) ?

                  'In other words - since you have stated "it does not work " - is your receiver expecting plain 8 bits raw data or properly formatted "serial communication "?

                  And I won't mention " transmission rate" etc.

                  Ketan__Patel__0011K Offline
                  Ketan__Patel__0011K Offline
                  Ketan__Patel__0011
                  wrote on last edited by
                  #8

                  @AnneRanch

                  QString SERIAL_STRING= "%01#XXXX00000001 + <MY_HEX_VALUE>**";

                  This kind of Data I am sending.

                  Using this method

                  @Ketan__Patel__0011 said in Sending hex data via serial communication:

                  serial->write(SERIAL_STRING.toLatin1());

                  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