Serial comunicacion using Qt and Visual Studio
-
Hello :
I would like to ask you very kindly about how to realize a serial communication using Qt and Visual Studio.
Here is the code I have up to now ://mytimer.hpp
@
#ifndef MYTIMER_HPP
#define MYTIMER_HPP#include <QtSerialPort/QtSerialPort>
#include <QTimer>
#include <QThread>
#include <QObject>class MyTimer : public QObject
{
Q_OBJECTpublic:
MyTimer(int,int);
QSerialPort serial;
QTimer timer;
int how_many_times;
int sampling;signals:
void done();public slots:
void MyTimerSlot();
};#endif // MYTIMER_H
@//mytimer.cpp
@
//#include <QtSerialPort/QtSerialPort>
#include "mytimer.hpp"
#include <QDebug>MyTimer::MyTimer(int a, int b)
{
how_many_times = a;
sampling = b;connect(&timer, SIGNAL(timeout()),
this, SLOT(MyTimerSlot()));
timer.setInterval(sampling);
timer.start();}
void MyTimer::MyTimerSlot()
{if (how_many_times == 1)
{
timer.stop();
}/==========================================================================/
serial.setPortName("COM1");
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
serial.open(QIODevice::ReadWrite);serial.write("hello");
serial.close();
/==========================================================================/
qDebug() << "hello" ;
how_many_times -= 1;
if (how_many_times == 0)
{
done();
}}
@//main.cpp
@
#include <QCoreApplication>
#include "mytimer.hpp"int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);MyTimer timer(20,500);
QObject::connect(&timer, SIGNAL(done()), &a, SLOT(quit()));
return a.exec();
}
@Thank you in advance for your response,
Have a nice day![edit: added missing coding tags @ SGaist]
-
Did you read the next article?
http://qt-project.org/doc/qt-5/qtserialport-terminal-example.html