PDA

View Full Version : serial programming



pooch
27th January 2014, 06:28
I am trying to write to serial port and read as well. Below is my code for it.
//mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qextserialport.h>
#include <QDebug>

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

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

void MainWindow::on_pushButton_clicked()
{
PortSettings portSettings;
portSettings.BaudRate = BAUD9600;
portSettings.DataBits = DATA_8;
portSettings.Parity = PAR_EVEN;
portSettings.StopBits = STOP_1;
portSettings.FlowControl = FLOW_OFF;
//portSettings.Timeout_Millisec=1000;

port= new QextSerialPort("COM3",portSettings,QextSerialPort::EventDriven);
QObject::connect(port,SIGNAL(readyRead()),this,SLO T(readData()));
if(port->isOpen())
{
port->flush();
port->close();
}
port->open(QextSerialPort::ReadWrite);
if (port->isOpen()) {
qDebug("connected!!");
//delay->setInterval(2000);

}
else qDebug("not connected");
writeData(":0B1000408000102FA00DC\r\n");//this is the data that i need to send
}
void MainWindow::readData()
{
port->flush();
QByteArray response=port->readAll();
ui->textBrowser->append(response);
}
void MainWindow::writeData(char* msg)
{
port->write(msg);
ui->textBrowser->append("writing data");
ui->textBrowser->append(msg);
port->flush();
}


this code writes to virtual serial(realterm) and even reads the response. But when i use hardware to write serial data to an atmega, no response is obtained. But i received response when i sent this data to atmega using python. Is there any fault in here?

kuzulis
27th January 2014, 07:33
You ca use QtSerialPort instead of QextSerialPort, because the QtSerialPort now is an part of Qt:

http://qt-project.org/wiki/QtSerialPort
http://qt-project.org/doc/qt-5.1/qtserialport/qtserialport-index.html

anda_skoa
27th January 2014, 08:19
You ca use QtSerialPort instead of QextSerialPort, because the QtSerialPort now is an part of Qt:

http://qt-project.org/wiki/QtSerialPort
http://qt-project.org/doc/qt-5.1/qtserialport/qtserialport-index.html

Isn't that only in Qt5?

According to the profile, pooch is using Qt4

Cheers,
_

kuzulis
27th January 2014, 08:30
Isn't that only in Qt5?

No, not only. QtSerialPort support Qt4 and/or Qt5. In case of Qt4 - user need to build it manually. Please read WIKI (the url above).

pooch
1st February 2014, 11:11
I installed qt5 from its site. But when i built the project, it gave me error like this:
:-1: error: Qt Creator needs a compiler set up to build. Configure a compiler in the kit options.
What do i have to do?

pooch
2nd February 2014, 06:05
I installed qt5 from its site. But when i built the project, it gave me error like this:
:-1: error: Qt Creator needs a compiler set up to build. Configure a compiler in the kit options.
What do i have to do?
I solves this problem myself.

But according to QtSerialPort documentation, Configuring timeouts and delays while reading is not supported. What does that mean?
I actually need it to implement modbusmaster protocol and to timeout when no data is recived within 1 second as soon as i complete my write to serial port.
Also when i use qextserialport with qt5, when i try to open port, the application output says:
Starting E:\qt stuffs try only\qt5\build-untitled-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\untitled.exe...
The program has unexpectedly finished.
E:\qt stuffs try only\qt5\build-untitled-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\untitled.exe crashed.
what does this mean?
What do i do with it

kuzulis
3rd February 2014, 14:13
But according to QtSerialPort documentation, Configuring timeouts and delays while reading is not supported. What does that mean?

This means that I/O is non-blocking and all of read/write operations returns immediately, without waiting.
It is necessary in order that not to lock of Qt event-loop, because it lead to freeze.



I actually need it to implement modbusmaster protocol and to timeout when no data is recived within 1 second as soon as i complete my write to serial port.


No problem:



...
...
QTimer *timer = new QTimer(this);
QSerialPort *port = new QSerialPort(this);
...
timer->setSingleShot(true);
...
connect(timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
connect(port, SIGNAL(readyRead()), this, SLOT(handleReadyRead()));
...
...
Foo::handleTimeout()
{
// process of response timeout from the slave device
}
...
Foo::handleReadyRead()
{
if (port->bytesAvailable() < expectedPacketLength)
return;

timer->stop();
QByteArray receivedPacket = port->readAll();

// do parse response from the slave device
}

Foo::write()
{
port->write(packetToSend);
timer->start(1000); // run timer for the 1 sec timeout
}