dataarray in QT sent to QSerialPort



  • 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.


  • Moderators



  • 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.


  • Moderators

    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);
    }
    


  • 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);
    


  • serial.write(bytes);
    serial.waitForBytesWritten(-1); // << add this 
    serial.close();
    


  • Thanks! It worked!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.