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. Mimic simple python script with QSerialPort
Forum Update on Monday, May 27th 2025

Mimic simple python script with QSerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 338 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.
  • M Offline
    M Offline
    Mark81
    wrote on 1 Apr 2022, 10:01 last edited by Mark81 4 Jan 2022, 10:05
    #1

    Given this simple (and working) python script:

    #! /usr/bin/python
    
    import sys
    import serial
    
    com = serial.Serial(sys.argv[1], 1200)
    com.dtr=False
    com.close()
    

    I tried to write it in Qt 6.2.0 under Ubuntu 21.10:

    QSerialPort serial("/dev/ttyACM0");
    serial.setBaudRate(QSerialPort::BaudRate::Baud1200);
    serial.open(QIODevice::ReadWrite); // checked the return value, the port is opened
    serial.setDataTerminalReady(false);
    serial.close();
    

    The expected behavior is the reset of the Arduino board connected to /dev/ttyACM0.
    The Python script achieve the goal, the Qt code does nothing (i.e. does not trigger the reset).

    What am I missing in the Qt code?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 Apr 2022, 19:31 last edited by
      #2

      Hi,

      Did you check the return value of setDataTerminalReady ?

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

      M 1 Reply Last reply 4 Apr 2022, 06:15
      0
      • S SGaist
        1 Apr 2022, 19:31

        Hi,

        Did you check the return value of setDataTerminalReady ?

        M Offline
        M Offline
        Mark81
        wrote on 4 Apr 2022, 06:15 last edited by
        #3

        @SGaist yes, but the problem is I had to add a delay after the opening of the port before setting the DTR. With this delay (at least 500 ms) the Qt code works like the Python one (without any delay, though).

        J 1 Reply Last reply 4 Apr 2022, 11:14
        1
        • M Mark81
          4 Apr 2022, 06:15

          @SGaist yes, but the problem is I had to add a delay after the opening of the port before setting the DTR. With this delay (at least 500 ms) the Qt code works like the Python one (without any delay, though).

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 4 Apr 2022, 11:14 last edited by
          #4

          @Mark81

          try:

          QSerialPort *serial(new QSerialPort("/dev/ttyACM0"));
          serial->setBaudRate(QSerialPort::BaudRate::Baud1200);
          if(serial->open(QIODevice::ReadWrite)) {
              connect(serial, &QSerialPort::dataTerminalReadyChanged, serial, [=]()->void{
                  serial->close();
                  serial->deleteLater();
          }, Qt::QueuedConnection);
              serial->setDataTerminalReady(false);
          } else {
              // Could not open port
          }
          

          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.

          M 1 Reply Last reply 4 Apr 2022, 13:18
          0
          • J J.Hilk
            4 Apr 2022, 11:14

            @Mark81

            try:

            QSerialPort *serial(new QSerialPort("/dev/ttyACM0"));
            serial->setBaudRate(QSerialPort::BaudRate::Baud1200);
            if(serial->open(QIODevice::ReadWrite)) {
                connect(serial, &QSerialPort::dataTerminalReadyChanged, serial, [=]()->void{
                    serial->close();
                    serial->deleteLater();
            }, Qt::QueuedConnection);
                serial->setDataTerminalReady(false);
            } else {
                // Could not open port
            }
            
            M Offline
            M Offline
            Mark81
            wrote on 4 Apr 2022, 13:18 last edited by
            #5

            @J-Hilk thanks for the proposal but it still does not work, nothing happens on the Arduino board.

            1 Reply Last reply
            0

            1/5

            1 Apr 2022, 10:01

            • Login

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