Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. India
  4. Serial comunicacion using Qt and Visual Studio

Serial comunicacion using Qt and Visual Studio

Scheduled Pinned Locked Moved India
2 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Falcon2050
    wrote on last edited by
    #1

    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_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 <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]

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tarod.net
      wrote on last edited by
      #2

      Did you read the next article?

      http://qt-project.org/doc/qt-5/qtserialport-terminal-example.html

      "Individually, we are one drop. Together, we are an ocean."

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved