Qt to arduino Serial number
-
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);
} -
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.