Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort Writing issue

QSerialPort Writing issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 813 Views 1 Watching
  • 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.
  • V Offline
    V Offline
    Vishu1236
    wrote on last edited by SGaist
    #1

    Hi,
    I have written a simple serial port writing code as follows :```
    //your code here

    **#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSerialPort/QSerialPort>
    QSerialPort serial;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        serial.setPortName("com1");
        serial.open(QIODevice::ReadWrite);
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::OneStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);
    
        serial.write("OK");
    
    }
    

    my COM1 is connected to a hardware.
    Now the issue is i am not able to recieve this data at hardware end, but if i am sending same data with different app like docklite , i am getting correct data at hardware end.
    Also i am able to see this data in serial port monitor,but not at hardware end.
    What can be the issue:

    0_1542000483733_1111.JPG

    [Edit: fixed coding tags SGaist]

    J.HilkJ 1 Reply Last reply
    0
    • V Vishu1236

      Hi,
      I have written a simple serial port writing code as follows :```
      //your code here

      **#include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QtSerialPort/QSerialPort>
      QSerialPort serial;
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          serial.setPortName("com1");
          serial.open(QIODevice::ReadWrite);
          serial.setBaudRate(QSerialPort::Baud9600);
          serial.setDataBits(QSerialPort::Data8);
          serial.setParity(QSerialPort::NoParity);
          serial.setStopBits(QSerialPort::OneStop);
          serial.setFlowControl(QSerialPort::NoFlowControl);
      
          serial.write("OK");
      
      }
      

      my COM1 is connected to a hardware.
      Now the issue is i am not able to recieve this data at hardware end, but if i am sending same data with different app like docklite , i am getting correct data at hardware end.
      Also i am able to see this data in serial port monitor,but not at hardware end.
      What can be the issue:

      0_1542000483733_1111.JPG

      [Edit: fixed coding tags SGaist]

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Vishu1236
      two points.

      first, set the settings of the serial port before you call open().
      second open returns a bool if opening was successful or not. You should check that.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      V 1 Reply Last reply
      1
      • V Offline
        V Offline
        Vishu1236
        wrote on last edited by
        #3

        Hi,
        thanx for your reply
        i have changed my code to :

        //your code here
        

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QtSerialPort/QSerialPort>
        QSerialPort *serial;

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        serial = new QSerialPort(this);
        serial->setPortName("com1");
        connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::WriteOnly);

        serial->setDataTerminalReady(false);
        if (serial->isOpen() && serial->isWritable())
        {
            serial->write("OK");
        }
        

        }

        still able to see data at serial port monitor but not at my harware.
        serial port monitor is showing :

        Port opened by process "serialport.exe" (PID: 8120)

        Request: 11/12/2018 11:30:41 AM.24564 (+314.5910 seconds)

        4F 4B OK

        Port closed

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @Vishu1236
          two points.

          first, set the settings of the serial port before you call open().
          second open returns a bool if opening was successful or not. You should check that.

          V Offline
          V Offline
          Vishu1236
          wrote on last edited by
          #4

          @J.Hilk
          Hi
          i tried printing errors and found "unknown error" errorno. 22 in setStopBits() ,setFlowControl.

          any sugesstions??

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            22 ? It's not part of the QSerialPort:: SerialPortError enum values.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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