meller92
meller92

Reputation: 455

Qt 5.1 Serial Comunication using QSerialPort

I'm trying to configure communication with serial-pot using Qt, It's my first application in Qt and according to many threads I wrote this piece of code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPort>

QSerialPort serial;

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

    serial.open(QSerialPort::ReadWrite);
    serial.setPortName("ttyACM0");
    serial.setBaudRate(QSerialPort::Baud9600);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    serial.write("hello");
}

MainWindow::~MainWindow()
{
    delete ui;
    serial.close();
}

And compiler tells me that device is not open when I build project.

Upvotes: 1

Views: 1084

Answers (2)

meller92
meller92

Reputation: 455

I've opended device using serial.open(QIODevice::ReadWrite) and it runs. Thanks for help.

Upvotes: 0

JohnDough
JohnDough

Reputation: 11

Have you made sure that you have that device available to open? If this is a *nix system try:

ls -l  /sys/class/tty/ttyUSB* ### if USB device or 
ls -l /sys/class/tty/ttyACM* ### if ACM, whatever that is

You should get a symbolic link to the devices directory, at least you do on linux.

Also, your code builds fine, however I image it doesn't actually run .

In addition, it looks like QSerialPort also has a method (inherited from QioDevice) called "isOpen" that returns a boolean if the device is already open which you should probably be using before both opening and closing to know what the actual state of the device is.

Upvotes: 1

Related Questions