Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Qt to arduino Serial number

Qt to arduino Serial number

Scheduled Pinned Locked Moved 3rd Party Software
arduinoserialserial port
2 Posts 2 Posters 2.7k 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.
  • R Offline
    R Offline
    r31lly
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0

      • Login

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