Simple serial port not outputting data



  • Apologies to all if this is not the correct place to post this...

    I'm a very newbie to the Qt environment but I am excited about the possibilities. I've looked through most of the example serial port apps related to this and tried to make a very small app that just writes a test message to a serial port. I've just created a simple main window which has a combobox populated with the available serial ports and a send command button which starts the action of sending the data to the port. I tried to make this very small and streamlined (maybe I made it TOO small and streamlined??). I'm including the mainwindow.h here:

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtSerialPort/QtSerialPort>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pbSendCmd_clicked();

    private:
    Ui::MainWindow *ui;
    QSerialPort *serial;
    };

    #endif // MAINWINDOW_H
    @

    The mainwindow.cpp is included here:

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QComboBox>
    #include <QString>
    #include <QMessageBox>
    #include <QtSerialPort/QSerialPortInfo>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    // change window title
    setWindowTitle("Widget Testbed");
    
    // do serial port combobox population
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
        ui->serialPortComboBox->addItem(info.portName());
    
    // create serial port
    serial = new QSerialPort(this);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pbSendCmd_clicked()
    {
    // set parameters for com port
    serial->setPortName(ui->serialPortComboBox->currentText());
    serial->setBaudRate(QSerialPort::Baud38400);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);

    // open the selected com port
    if(!serial->open(QIODevice::ReadWrite)){
        QMessageBox::warning(this, "COM Error", "Can't open port");
        return;
    }
    
    // write data to port
    QByteArray data = "test message";
    serial->write(data);
    
    // close the selected com port
    serial->close();
    

    }
    @

    In addition, I've changed the .pro file line to greaterThan(QT_MAHOR_VERSION, 4) : QT += widgets serialport.
    The combobox populates correctly and there are no compilation errors. The port appears to open correctly but no data is written. What am I doing wrong?

    Thanks for any help.


  • Lifetime Qt Champion

    Hi,

    How do you know there's nothing sent ?



  • I hooked up the output to an oscilloscope and looked for any data transitions... Nothing showed up.


  • Lifetime Qt Champion

    Does it also happen if you use a standard Windows utility ?



  • Hi again -

    I have confirmed that the serial port works correctly using a Tera Term session to an external device connected to the same serial port I'm trying to get the Qt app to work with.

    Thanks.



  • [quote]
    @
    // write data to port
    QByteArray data = "test message";
    serial->write(data);

    // close the selected com port
    serial->close();
    

    @
    [/quote]

    This is wrong sequence. The serial port nothing to write in this case because the close() immediately does closing of device.

    You should or:

    @
    serial->write(data);
    serial->flush();
    serial->close();
    @

    or:

    @
    serial->write(data);
    serial->waitForBytesWritten();
    serial->close();
    @

    or to close the device after the bytesWritten(qint64) signal received.

    NOTE: All I/O in serialport is asynchronous!



  • Thanks so much for responding! I tried the serial->flush() but still didn't see anything on the scope. I tried the serial->waitForBytesWritten(strlen(data)) and it worked like a charm!

    I'm still learning about how the timing of everything works but this is a grat start.

    Thanks, again!


Log in to reply
 

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