Qt Forum

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

    Solved Sending 2 string through serial port

    General and Desktop
    2
    8
    1602
    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.
    • huythy
      huythy last edited by A Former User

      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 ?

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

        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);

        1 Reply Last reply Reply Quote 4
        • huythy
          huythy last edited by huythy

          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);

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

            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() ?

            1 Reply Last reply Reply Quote 2
            • huythy
              huythy last edited by huythy

              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()

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

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

                1 Reply Last reply Reply Quote 3
                • huythy
                  huythy last edited by

                  thanks for your help , my problem is solved

                  mrjj 1 Reply Last reply Reply Quote 1
                  • mrjj
                    mrjj Lifetime Qt Champion @huythy last edited by

                    @huythy
                    Super :) Please mark as Solved.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post