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. Help using QSerialPort

Help using QSerialPort

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 2.7k 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.
  • S Offline
    S Offline
    silviutp
    wrote on last edited by
    #1

    I am having trouble sending commands to a motor through a serial interface.
    Bellow I try to send a homing command (first byte is the device id, second byte is the command, and there are 4 more bytes of data which are not used for this command)
    @int main(){
    QSerialPortInfo info("COM1");
    qDebug()<<info.manufacturer()<<endl;
    qDebug()<<info.description()<<endl;
    qDebug()<<info.portName()<<endl;
    QSerialPort port(info);

    port.setBaudRate(QSerialPort::Baud9600);
    port.setDataBits(QSerialPort::Data8);
    port.setFlowControl(QSerialPort::NoFlowControl);
    port.setParity(QSerialPort::NoParity);
    port.setStopBits(QSerialPort::OneStop);
    port.open(QSerialPort::ReadWrite);
    
    char cmd_data[]={0,1,0,0,0,0};
    port.write(cmd_data);
    delay(3000); // delay 3s
    
    cout<<"Closing Serial Port"<<endl;
    port.close();
    return 0;
    

    }@
    Nothing happens.... The device works with an external program..
    What am I doing wrong ? Is this the right way to send the command ?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kqt-mr-nv6
      wrote on last edited by
      #2

      first you have to open the port
      and then do the settings

      and also
      consider the return status of all the settings and
      port open methods

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        Hi.
        Did you check is serialport opened and data realy sended? If yes try compare real data sended to a device from your programm and external programm. Are they identical?
        You can use "free serialport monitor":http://www.serial-port-monitor.com/ for that.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on last edited by
          #4

          Hi.

          Yes, please do all as guys says...

          Also this wrong:

          [code]
          ...
          port.write(cmd_data);
          delay(3000); // delay 3s
          ...
          [/code]

          need:
          [code]
          ...
          port.write(cmd_data);
          waitForBytesWritten(3000); // delay 3s
          ...
          [/code]

          1 Reply Last reply
          0
          • S Offline
            S Offline
            silviutp
            wrote on last edited by
            #5

            [quote author="kqt-mr-nv6" date="1383819968"]first you have to open the port
            and then do the settings
            [/quote]
            thank you, that was the problem

            1 Reply Last reply
            0
            • S Offline
              S Offline
              silviutp
              wrote on last edited by
              #6

              Hi
              I am having a problem communicating with my motor. When I give it some commands that take more time to realize (for example a long move or a home command) I am having problem when reading a response in the sense that I cannot always get the 6 bytes corresponding to a response. This thing also happens when I use a Sleep function (from "windows.h") with sufficient time for the command to complete.
              I have this functions for sending...
              @bool MotorAccess::sendCommand(const QByteArray& cmd){
              int bytes_sent = port_->write(cmd);
              if (bytes_sent != 6){
              return false;
              }
              port_->waitForBytesWritten(100);

              return true;
              }@
              and reading...
              @QByteArray MotorAccess::getResponse(){
              bool read_success = true;
              QByteArray response = QByteArray();// = port_->readAll();

              // discard data if buffers have more data then needed
              // this can happen when manual accessing the motor
              while (port_->bytesAvailable()>6) {
              #ifdef DEBUG_MODE
              qDebug()<<port_->bytesAvailable();
              #endif
              port_->read(1);
              }

              read_success = port_->waitForReadyRead(100);
              if (port_->bytesAvailable() == 0) {
              return response;
              }
              response.append(port_->read(1));

              while (read_success && response.count() < 6 ){
              read_success = port_->waitForReadyRead(100);
              response.append(port_->read(1));
              }
              if ((! read_success) || response.count() != 6) {
              #ifdef DEBUG_MODE

                 qDebug()<<"Read Problem. Bytes available: "<<port_->bytesAvailable();
              

              #endif
              }

              return response;
              }@
              I would really appreciate some help on this. Thanks

              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