Sending 2 string through serial port



  • Hi, pros
    I'm new with Qt. My problem is i don't know how to send multiple strings to serial port like :
    QString data = "x" + "50" + " 60" ;
    Does anyone who knows the answer, please ?


  • Qt Champions 2016

    Hi
    I would take
    http://doc.qt.io/qt-5/qtserialport-terminal-example.html

    It can both send and read.

    To send the data its simply
    serial->write(data);



  • Yes, i read it but it can't get a right result, because i send a long string to arduino through serial. A string and convert to ascii
    Here's my code :
    int value_a=0, value_b=0;
    QString a = ui->lineEdit_1->text();
    QString b=ui->lineEdit_2->text();
    for (int i = 0; i< a.length(); i++)
    {
    value_a = a.at(i).toLatin1();
    }
    for (int j = 0; j < b.length(); j++)
    {
    value_b = b.at(j).toLatin1();
    }
    QString data = "x" + value_a + value_b;
    QByteArray buffer = data.toLatin1();
    serial->write(buffer);


  • Qt Champions 2016

    Hi
    I assume you check that data contains what you expect.
    You must explain what the issue is?

    • get a right result

    What is right result?
    Is it on the arduino side?
    Are you using ReadAll() ?



  • well, i will send "x" + "50" to serial , and in arduino will convert it , "x" is a character for starting and "50" is for data.
    example : result = (data[0] - 48)*100 + (data[1]-48)*10 + data[2]-48 - this is on arduino side.
    The right result is 50 will be displayed oon LCD, i've checked on visual studio , everything on arduino is ok.
    I'm not using ReadAll()


  • Qt Champions 2016

    well U can test with the Terminal sample.
    Try send from one to other and see what you get.



  • thanks for your help , my problem is solved


  • Qt Champions 2016

    @huythy
    Super :) Please mark as Solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.