Hello :
I would like to ask you very kindly about how to realize a serial communication using Qt, Visual Studio and Arduino, I am trying to send some data first but this is not working.
Here is the code I have up to now :
#ifndef MYTIMER_HPP
#define MYTIMER_HPP
#include <QtSerialPort/QtSerialPort>
#include
#include
#include
class MyTimer : public QObject
{ Q_OBJECT
public: MyTimer(int,int); QSerialPort serial; QTimer timer; int how_many_times; int sampling;
signals: void done();
public slots: void MyTimerSlot();
};
#endif // MYTIMER_H
//#include <QtSerialPort/QtSerialPort>
#include “mytimer.hpp”
#include
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(); }
}
#include
#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!