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. dataarray in QT sent to QSerialPort
QtWS25 Last Chance

dataarray in QT sent to QSerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
serialportwritedataqbytearray
7 Posts 3 Posters 3.0k 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.
  • kteleK Offline
    kteleK Offline
    ktele
    wrote on last edited by ktele
    #1

    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
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • kteleK Offline
        kteleK Offline
        ktele
        wrote on last edited by
        #3

        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
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • kteleK Offline
            kteleK Offline
            ktele
            wrote on last edited by
            #5

            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
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on last edited by
              #6
              serial.write(bytes);
              serial.waitForBytesWritten(-1); // << add this 
              serial.close();
              
              1 Reply Last reply
              2
              • kteleK Offline
                kteleK Offline
                ktele
                wrote on last edited by
                #7

                Thanks! It worked!

                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