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. I need to transmit data in 8-bit format via rs232. I am getting errors . please kindly help me.

I need to transmit data in 8-bit format via rs232. I am getting errors . please kindly help me.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 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.
  • AnilReddyA Offline
    AnilReddyA Offline
    AnilReddy
    wrote on last edited by AnilReddy
    #1
    void SerialConnect::write(const QString data) {
        QBitArray dataArray1(8);
      
        dataArray1[1]=0;dataArray1[2]=0;dataArray1[3]=1;dataArray1[4]=0;dataArray1[5]=0;dataArray1[6]=1;dataArray1[7]=1;dataArray1[8]=0;
        
        qint64 bytesWritten = serial->write(dataArray1);
    
        if (bytesWritten == (-1)) {
            qDebug() << "Error writing data to serial port: " << serial->errorString();
        }
        
        qDebug() << "SerialConnect::write(QString) Text sent as QByteArray: " << dataArray1;
        qDebug() << "Number of bytes written: " << bytesWritten;
    
    }
    
    
    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, arrays start with [0] not [1]. Also I don't think Qt's serialport class supports writing a QBitArray, try using a QByteArray :-)

      AnilReddyA 1 Reply Last reply
      0
      • hskoglundH hskoglund

        Hi, arrays start with [0] not [1]. Also I don't think Qt's serialport class supports writing a QBitArray, try using a QByteArray :-)

        AnilReddyA Offline
        AnilReddyA Offline
        AnilReddy
        wrote on last edited by
        #3

        @hskoglund
        thanks for the reply , But i need to transmit data as 8-bit . Is it possible to send in bytes. By converting bits as hexa decimals or decimals

        1 Reply Last reply
        0
        • M Offline
          M Offline
          michelson
          wrote on last edited by
          #4

          As you see in the docs http://doc.qt.io/qt-5/qserialport.html#writeData you can send char buffer through serial port.
          Probably you want to have something like:

          //0-indexed bit position
          void setBit(uint bit, bool value, char& buffer)
          {
              if(bit < 8)
                  buffer |= (value << bit);
          }
          

          Since char is one byte you can imagine how this function works:

          char data = 0b00000000;
          setBit(3, true, data);
          //now data is 0b00001000;
          

          Now to the sending part:

          //1st option
          QByteArray byteToSend(1, data); //data from previous part
          serial->write(byteToSend);
          serial->waitForBytesWritten(1000);
          
          //2nd option
          char byteToSend[1] = {data};
          serial->write(byteToSend, 1);
          serial->waitForByteWritten(1000);
          

          Of course you can define byte you wanna write in the beginning assuming your example you can have something like:

          char dataArray = 0b00100110;
          

          and send this one somehow.
          For more bitwise operators you can check here: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=163 or just google it :)

          1 Reply Last reply
          2
          • AnilReddyA Offline
            AnilReddyA Offline
            AnilReddy
            wrote on last edited by
            #5

            thank you very much mr michelson . Its is working , i am able to transmit the bit data. And could please tell me how to send the data on after another, Because rs232 serialport can not transmit all at a time. Could you please write me a sample code.
            thanks in advance.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              michelson
              wrote on last edited by
              #6

              You can just pass larger QByteArray or char* with all the data you need.
              For user controlled flow you can try sending new data as above after eg. button click (so somewhere in apropriate slot probably).
              Or just

              QByteArray data = ...//fill
              serial->write(data);
              serial->waitForBytesWritten(1000);
              
              data = ...//again fill
              
              serial->write(data);
              serial->waitForBytesWritten(1000);
              

              and so on (mayby in loop?)

              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