Serial Communication using Qt Visual Studio and Arduino

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 :

//mytimer.hpp

#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

//mytimer.cpp

//#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(); }

}

//main.cpp

#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!

I don't know anything about Qt and not much more about Visual Studio. However the general approach in this Python demo applies to any language.

It may help you to write your Qt program to take the place of my Python program working with my Arduino program (with no changes). The Arduino program will also work with the Arduino Serial Monitor.

...R

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();

What is this nonsense? There is NO excuse for cramming all this stuff on so few lines.

Opening the serial port resets the Arduino. Closing the serial port resets the Arduino. There is not much point in having you application even bother to open the serial port if it's going to send data before the Arduino finishes resetting, and is then going to reset the Arduino immediately.

Thank you for your response,
I have tested the serial communication using Matlab/Simulink - Arduino, and there is no problem. Everything is working fine I can send and get data. But when I'm trying to use Qt5.3.1 and Visual Studio 2013 to do the same so it is not working, I can't send data, I can't do nothing :frowning:

Even if I test the following Qt example :

http://qt-project.org/doc/qt-5/qtserialport-cwritersync-main-cpp.html

So there is also a problem, it is like the console application is going to an infinity cycle but no data is sent.
I will be very glad if somebody can test that Qt example and tell me what I'm doing wrong.
Thank you in advance for your response guys.
Have a nice day!

Falcon2050:
tell me what I'm doing wrong.

Not reading the advice you have already been given which almost certainly contains the answer.

Both my advice and @PaulS's are consistent with each other.

...R