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
QtWS25 Last Chance

Mimic simple python script with QSerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 331 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 last edited by Mark81
    #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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      0
      • SGaistS SGaist

        Hi,

        Did you check the return value of setDataTerminalReady ?

        M Offline
        M Offline
        Mark81
        wrote on 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.HilkJ 1 Reply Last reply
        1
        • M Mark81

          @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.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on 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
          0
          • J.HilkJ J.Hilk

            @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 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

            • Login

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