How to read ? QSerialPort
-
Arduino send me "degreevalue,distancevalue." .constantly sending different values. I have to write two different places.as the submitted values change, the displayed value must change as well.
for example:
place1 = degreevalue
place2 = distancevalue
how can i write -
Actually i want code like this. i tried different types but i couldn't write
void serialEvent (Serial myPort) { // starts reading data from the Serial Port // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". data = myPort.readStringUntil('.'); data = data.substring(0,data.length()-1); index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance // converts the String variables into Integer iAngle = int(angle); iDistance = int(distance); }
-
Hi @lordumuzca
Maybe you can get inspired from Qt Serial Port Examples
-
Hi,
To add to @mostefa, type casting a string to an it like that is wrong. QString provides dedicated methods for conversion from string to int and other numeric types.
-
#include "dialog.h" #include "ui_dialog.h" #include <QSerialPort> #include <QSerialPortInfo> #include <QDebug> #include <QtWidgets> #include <QString> #include <string> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); ui->degree_lcdNumber->display("---"); ui->distance_lcdNumber->display("---"); arduino_is_available = false; arduino_port_name = ""; arduino =new QSerialPort; serialBuffer = ""; foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) { if(serialPortInfo.hasVendorIdentifier() &&serialPortInfo.hasProductIdentifier()){ if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){ if(serialPortInfo.productIdentifier()== arduino_uno_product_id){ arduino_port_name = serialPortInfo.portName(); arduino_is_available = true; } } } } if(arduino_is_available){ //open and configure the port 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); QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(serialReceived())); }else{ //give error message QMessageBox::warning(this,"Port Error","Couldn't find the Arduino!"); } } Dialog::~Dialog() { if(arduino->isOpen()){ arduino->close(); } delete ui; } void Dialog::serialReceived(){ QStringList bufferSplit = serialBuffer.split("."); serialData = arduino->readAll(); serialBuffer += QString::fromStdString(serialData.toStdString()); // serialBuffer = ","; qDebug()<<bufferSplit; } void Dialog::updateLCD(const QString sensor_reading){ // ui->degree_lcdNumber->display(sensor_reading); }
my output
i want to display before comma in LCD1, after comman is in LCD2. i tried different things but i cant.
maybe each double is new line like this.maybet better
"xx,xx"
"xx,xx" -
You can use a regexp to retrieve both numbers from your string.
Also note that your conversion to std string are useless, QString already provides handling fro QByteArray.