Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved dataarray in QT sent to QSerialPort

    General and Desktop
    serialport writedata qbytearray
    3
    7
    2470
    Loading More Posts
    • 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.
    • ktele
      ktele last edited by ktele

      Hello.

      I have a code in MATLAB

      fwrite(scom, hex2dec(['AA';'AA';'AA';'20';'01';'00';'00';'8F';'83']));
      

      This is piece of data to be send to serial port device. How can I implement this code in QT as well.

      Thanks in advance.

      1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion last edited by

        Hi!
        Did you read the documentation for QSerialPort? http://doc.qt.io/qt-5/qserialport.html
        http://doc.qt.io/qt-5/qserialport.html#writeData
        Example: http://doc.qt.io/qt-5/qtserialport-terminal-example.html

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • ktele
          ktele last edited by

          Thanks. However, I am very new to Qt. Can't deal with

          QByteArray
          

          properly. Can you write, how can send this data properly. I tried

          write and writeData
          

          however, couldn't get response. The port is opened and configured fine.

          1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion last edited by

            What's the problem with QByteArray?
            You should first learn the basics.
            Did you verify that your request actually arrived?
            In the example I posted you can find code to read from the serial port:

            connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
            ...
            void MainWindow::readData()
            {
                QByteArray data = serial->readAll();
                console->putData(data);
            }
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • ktele
              ktele last edited by

              Here is my code to send data to port, I can't send, the LED is not blinking on the sensor.

              #include <QCoreApplication>
              #include <QSerialPort>
              #include <QSerialPortInfo>
              #include <QDebug>
              #include <QByteArray>
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
              
                  foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
                      qDebug() << serialPortInfo.portName() << "=" << serialPortInfo.description();
                  }
              
                  QSerialPort serial;
                  serial.setPortName("COM3");
                  serial.setBaudRate(QSerialPort::Baud115200);
                  serial.setDataBits(QSerialPort::Data8);
                  serial.setParity(QSerialPort::NoParity);
                  serial.setStopBits(QSerialPort::OneStop);
              
                  serial.open(QSerialPort::ReadWrite);
              
                  if (serial.isOpen() && serial.isWritable())
                  {
                      qDebug() << "Serial is open and writable";
                  }
              
                  QByteArray bytes;
                  bytes.resize(9);
                  bytes[0] = 0xaa;
                  bytes[1] = 0xaa;
                  bytes[2] = 0xaa;
                  bytes[3] = 0x20;
                  bytes[4] = 0x01;
                  bytes[5] = 0x00;
                  bytes[6] = 0x00;
                  bytes[7] = 0x8f;
                  bytes[8] = 0x83;
              
                  serial.write(bytes);
              
                  serial.close();
              
                  return a.exec();
              }
              

              Here I attach my MATLAB code, which is working

              %Initialiazing COM port
              scom=serial('COM3','BaudRate',115200,'Parity','none','DataBits',8,'StopBits',1);
              fopen(scom);
              
              for j=1:100
                      %Start acquisition by sending command to port
                      fwrite(scom,hex2dec(['AA';'AA';'AA';'20';'01';'00';'00';'8F';'83']));
                      myData = fread(scom,79); %read RAW data
                      showData=ones(32,1);
                      for i=1:32
                          showData(i,1)=myData(13+i*2)*16*16+myData(12+i*2); %Convert
                      end
                      showWin=(reshape(showData',4,8))';
                      showWinStr=num2str(showWin); 
                      [Xq,Yq]=meshgrid((1:0.5:4),(1:0.5:8));
                      Vq = interp2(showWin,Xq,Yq);
                      imagesc(Vq) %Plot as a image
                      pause(0.1) 
              end
              %Stop acquisition by sending stop command to port
              fwrite(scom,hex2dec(['AA';'AA';'AA';'22';'00';'00';'0E';'76']));
              fclose(scom);
              
              1 Reply Last reply Reply Quote 0
              • K
                kuzulis Qt Champions 2020 last edited by

                serial.write(bytes);
                serial.waitForBytesWritten(-1); // << add this 
                serial.close();
                
                1 Reply Last reply Reply Quote 2
                • ktele
                  ktele last edited by

                  Thanks! It worked!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post