Cant read strings with Raspberry via QtSerial



  • Hello to all,
    I'am trying to read string coming from arduino to my raspberry. On raspberry pi2, I'm using qt4 with jessie and connecting raspberry with arduino via usb.

    I send data from arduino like;

    void setup(){
      Serial.begin(9600);
    }
    
    void loop(){
    Serial.println("123456789");
    delay(2000);
    }
    

    On the raspberry I'm trying to read like;

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        serial = new QSerialPort(this);
        serial->setPortName("ttyACM0");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::UnknownDataBits);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
        connect(serial,SIGNAL(readyRead()),this,SLOT(data_received()));
    
    }
    
    void MainWindow::data_received()
    {
        QByteArray incomingData;
        incomingData=serial->readAll();
        qDebug(incomingData);
    }
    

    With qt terminal example, I read the data perfect.
    But with my code above;

    12
    1
    2345
    6789
    
    
    123
    4567
    89
    
    12
    3456
    789
    
    
    1
    2345
    678
    9
    
    123
    4567
    89
    
    

    What must I do? Any suggests?


  • Lifetime Qt Champion

    Hi,

    qDebug by default will add a new line at destruction hence what you see.

    If you want to modify that behaviour, take a look at this.



  • @SGaist
    Thanks for your reply.
    But when I tried to change the text of a label, or check the received data, I couldnt success.
    When I update the text of a label, I got 6789, 23456789, 123456 etc.
    And after those tries I saw that with qdebug I couldnt read the data clearly.

    And as I sad before , I can read the data clearly with QtSerial terminal example.

    What I'm trying to do is, checking the received data and updating the gpio pins...


  • Lifetime Qt Champion

    The terminal example does only one thing: it appends whatever is received to the Console widget.

    The usual way to ensure good communication on a serial port is to have a protocol in place that helps you know when you have received a "full frame" of data.

    Then on the receiving part, you accumulate the data in a buffer until you have enough to recreate that frame and then process it.



  • @SGaist
    Can you suggest me an example ?



  • serial->setDataBits(QSerialPort::UnknownDataBits);

    It is best solution! ))


  • Lifetime Qt Champion

    What kind of example are you looking for ?


Log in to reply
 

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