Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Qt to arduino Serial number

    3rd Party Software
    arduino serial serial port
    2
    2
    2377
    Loading More Posts
    • 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.
    • R
      r31lly last edited by

      I am trying to send an integer from qt to the arduino through the serial port. All the number does is change the delay time for the blinking led. I am able to turn the led on and off by sending a char, but something gets messed up when trying to send an integer. Below is the code that is relevent.

      Qt Code:
      void PlayingAround::on_verticalSlider_valueChanged(int slideposition)
      {
      ui->lcdNumber->display(slideposition);
      PlayingAround::sendArduino(QString("%1").arg(slideposition));
      // qDebug() << slideposition;
      }

      void PlayingAround::sendArduino(QString command){

      if(arduino->isWritable()){
          arduino->write(command.toStdString().c_str());
          qDebug() << command.toStdString().c_str();
      }else{
          qDebug() << "Couldn't write to serial";
      }
      

      }

      Serial Config in Qt:
      if(arduino_is_available){
      //Open and Config serialPort
      arduino->setPortName(arduino_port_name);
      arduino->open(QSerialPort::ReadWrite);
      arduino->setBaudRate(QSerialPort::Baud9600);
      arduino->setDataBits(QSerialPort::Data8);
      arduino->setParity(QSerialPort::NoParity);
      arduino->setStopBits(QSerialPort::OneStop);
      arduino->setFlowControl(QSerialPort::NoFlowControl);
      }else{
      QMessageBox::warning(this, "Port Error", "Couldn't find the arduino!");
      }

      Arduino Code
      int led = 13;
      int pause = 100;

      void setup() {
      pinMode(led, OUTPUT);
      Serial.begin(9600);
      }

      void loop() {
      while(Serial.available()){
      int pause = Serial.parseInt();
      }

      digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
      delay(pause); // wait for a second
      digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
      delay(pause); // wait for a second
      Serial.println(pause);
      }

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

        Why the double conversion to stdString and c_str ? You can use something like toLatin1 if you want a QByteArray to write to your serial port.

        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 Reply Quote 0
        • First post
          Last post