Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. read data from serial port

read data from serial port

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 4 Posters 5.8k Views
  • 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.
  • I Offline
    I Offline
    isan
    wrote on 3 Aug 2018, 15:18 last edited by isan 8 May 2018, 14:18
    #1

    I ask this question before and I could not make the problem clear
    because it's not a good question
    so I delete the last topic and post new one with new details
    I want to read data from USB port of raspberry pi and convert it to string for save it to excel file
    also get the first number in string line and emit it for use it to an other function

    #include "MyThread.h"
    #include <wiringSerial.h>
    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    void MyThread::run()
    {
    
        qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
    
        int fd ,x;
        
          
        if ((fd = serialOpen ("/dev/ttyACM0",230400)) < 0)
        {
            fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
        }
    
        while (serialDataAvail(fd)>-2)
        {
             //-------value format is int-------
            value=serialGetchar (fd) ; 
         //--------NewValue format is string------
          NewValue.push_back(value);
          qDebug()<<NewValue.c_str();
       //   nm=NewValue.back();
       //   cout<<nm<<endl;
             if (!WriteNewRecord(NewValue)) {
               fstream FileStream;
                  FileStream << NewValue <<endl;
                            }
                      
            msleep(1); 
           emit signalValueUpdated(x);
      
        }
        serialClose(fd);
    
    
    }
    
    
    

    the data that receive is number between -1 to 255,currentDate,currentTime
    like 85,2018/8/3,17:21:3
    9,2018/8/27,1:40:34
    -the length is not fixed!
    -for 1 sec I receive 314 samples so they have same time stamp
    I want to get just number,before first " , "( that between -1,255,not date and time ) with int format to call in emit signalValueUpdated(x); for use in other place;
    with stoi() and .toInt() ,the output is zero
    NewValue.split(", ")[0].toInt(); just get the first byte of NewValue
    How can I do this?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 3 Aug 2018, 15:46 last edited by
      #2

      hi
      In not sure why split and toInt dont work for u ?

      QString input{"85,2018/8/3,17:21:3"};
      QStringList values = input.split(",");
      qDebug() << values[0].toInt();
      

      gives 85.

      I 1 Reply Last reply 3 Aug 2018, 16:09
      0
      • M mrjj
        3 Aug 2018, 15:46

        hi
        In not sure why split and toInt dont work for u ?

        QString input{"85,2018/8/3,17:21:3"};
        QStringList values = input.split(",");
        qDebug() << values[0].toInt();
        

        gives 85.

        I Offline
        I Offline
        isan
        wrote on 3 Aug 2018, 16:09 last edited by isan 8 Mar 2018, 16:12
        #3

        @mrjj I read from serial in while
        and get 314 sps per second ,
        and push_back them in string ,when I use the split it do it from the beginning of string NewValue , it doesn't split just new line that receive
        and .toInt() return zero
        how can do it just for new line data that receive?

        M 1 Reply Last reply 3 Aug 2018, 16:16
        0
        • I isan
          3 Aug 2018, 16:09

          @mrjj I read from serial in while
          and get 314 sps per second ,
          and push_back them in string ,when I use the split it do it from the beginning of string NewValue , it doesn't split just new line that receive
          and .toInt() return zero
          how can do it just for new line data that receive?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 3 Aug 2018, 16:16 last edited by
          #4

          @isan
          You mean data comes like
          85,2018/8/3,17:21:3
          85,2018/8/3,17:21:3
          85,2018/8/3,17:21:3
          85,2018/8/3,17:21:3
          with newline in between ?

          I 1 Reply Last reply 3 Aug 2018, 16:25
          0
          • M mrjj
            3 Aug 2018, 16:16

            @isan
            You mean data comes like
            85,2018/8/3,17:21:3
            85,2018/8/3,17:21:3
            85,2018/8/3,17:21:3
            85,2018/8/3,17:21:3
            with newline in between ?

            I Offline
            I Offline
            isan
            wrote on 3 Aug 2018, 16:25 last edited by isan 8 Mar 2018, 17:36
            #5

            @mrjj the data that receive is like:
            when print with qDebug<<NewValue.c_str();
            85,2018/8/3,17:21:3
            98,2018/8/3,17:21:3
            102,2018/8/3,17:21:3
            9,2018/8/3,17:21:3
            .......(until 314 samples )
            167,2018/8/3,17:21:4
            34,2018/8/3,17:21:4
            28,2018/8/3,17:21:4
            85,2018/8/3,17:21:4
            .......(until 314 samples )

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 3 Aug 2018, 16:35 last edited by
              #6

              so every time value variable is \n
              you have a full line in NewValue and can split and take value.

              I 1 Reply Last reply 3 Aug 2018, 16:52
              2
              • M mrjj
                3 Aug 2018, 16:35

                so every time value variable is \n
                you have a full line in NewValue and can split and take value.

                I Offline
                I Offline
                isan
                wrote on 3 Aug 2018, 16:52 last edited by isan 8 Jul 2018, 15:26
                #7

                @mrjj with this:

                QStringList values = input.split(",");
                qDebug() << values[0].toInt();
                

                that you said?
                I change my code:

                void MyThread::run()
                {
                
                    qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
                
                    int fd ,x;
                    
                      
                    if ((fd = serialOpen ("/dev/ttyACM0",230400)) < 0)
                    {
                        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
                    }
                
                    while (serialDataAvail(fd)>-2)
                    {
                         //-------value format is int-------
                        value=serialGetchar (fd) ;
                    //--------vs format is QString------
                    vs.push_back(value);
                     //--------values format is QStringList------
                     values = input.split(",");
                      x= values[0].toInt();
                 msleep(1); 
                       emit signalValueUpdated(x);
                  
                    }
                    serialClose(fd); 
                }
                

                the first line that receive is 96,2018/8/3,21:35:40
                the emit signalValueUpdated(x); is 96 for ever

                M 1 Reply Last reply 3 Aug 2018, 18:23
                0
                • I isan
                  3 Aug 2018, 16:52

                  @mrjj with this:

                  QStringList values = input.split(",");
                  qDebug() << values[0].toInt();
                  

                  that you said?
                  I change my code:

                  void MyThread::run()
                  {
                  
                      qDebug("Thread id inside run %d",(int)QThread::currentThreadId());
                  
                      int fd ,x;
                      
                        
                      if ((fd = serialOpen ("/dev/ttyACM0",230400)) < 0)
                      {
                          fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
                      }
                  
                      while (serialDataAvail(fd)>-2)
                      {
                           //-------value format is int-------
                          value=serialGetchar (fd) ;
                      //--------vs format is QString------
                      vs.push_back(value);
                       //--------values format is QStringList------
                       values = input.split(",");
                        x= values[0].toInt();
                   msleep(1); 
                         emit signalValueUpdated(x);
                    
                      }
                      serialClose(fd); 
                  }
                  

                  the first line that receive is 96,2018/8/3,21:35:40
                  the emit signalValueUpdated(x); is 96 for ever

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 3 Aug 2018, 18:23 last edited by
                  #8

                  hi
                  well you only ask for first value!
                  values = input.split(","); // makes long list (potentially )
                  x= values[0].toInt(); // take only first in list

                  so it would be more like

                  for ( const QString& valline : values ) {
                        qDebug() << "val =" << valline.toInt();
                      }
                  

                  and since list also contains other data, it looks like
                  96
                  2018/8/3
                  21:35:40
                  80
                  2018/8/3
                  21:35:40
                  90
                  2018/8/3
                  21:35:40
                  so value every 3 index

                  I 1 Reply Last reply 3 Aug 2018, 18:36
                  2
                  • M mrjj
                    3 Aug 2018, 18:23

                    hi
                    well you only ask for first value!
                    values = input.split(","); // makes long list (potentially )
                    x= values[0].toInt(); // take only first in list

                    so it would be more like

                    for ( const QString& valline : values ) {
                          qDebug() << "val =" << valline.toInt();
                        }
                    

                    and since list also contains other data, it looks like
                    96
                    2018/8/3
                    21:35:40
                    80
                    2018/8/3
                    21:35:40
                    90
                    2018/8/3
                    21:35:40
                    so value every 3 index

                    I Offline
                    I Offline
                    isan
                    wrote on 3 Aug 2018, 18:36 last edited by
                    #9

                    @mrjj I should use this :

                    for ( const QString& valline : values ) {
                        qDebug() << "val =" << valline.toInt();
                      }
                    

                    instead of this :

                    x= values[0].toInt();
                    

                    am I right?

                    qDebug() << "val =" << valline.toInt();
                    

                    always returns zero to me!

                    M 1 Reply Last reply 3 Aug 2018, 18:38
                    0
                    • I isan
                      3 Aug 2018, 18:36

                      @mrjj I should use this :

                      for ( const QString& valline : values ) {
                          qDebug() << "val =" << valline.toInt();
                        }
                      

                      instead of this :

                      x= values[0].toInt();
                      

                      am I right?

                      qDebug() << "val =" << valline.toInt();
                      

                      always returns zero to me!

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 3 Aug 2018, 18:38 last edited by
                      #10

                      @isan
                      hmm
                      maybe input is not as you expect then.
                      try qDebug() << values;
                      to see what it has

                      I 1 Reply Last reply 3 Aug 2018, 19:01
                      0
                      • M mrjj
                        3 Aug 2018, 18:38

                        @isan
                        hmm
                        maybe input is not as you expect then.
                        try qDebug() << values;
                        to see what it has

                        I Offline
                        I Offline
                        isan
                        wrote on 3 Aug 2018, 19:01 last edited by
                        #11

                        @mrjj said in read data from serial port:

                        qDebug() << values;

                        return :
                        \n191", "2018/8/3", "23:28:55 \r\n187", "2018/8/3", "23:28:55 \r\n185", "2018/8/3", "23:28:55 \r\n164", "2018/8/3", "23:28:55 \r\n131", "2018/8/3", "23:28:55 \r\n103", "2018/8/3", "23:28:55 \r\n....................................

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 3 Aug 2018, 19:13 last edited by mrjj 8 Mar 2018, 19:17
                          #12

                          Ok so when we only split at ","
                          We get \n\r on some/all of the values.
                          Using something Like ReadLine might be easier.
                          but you seem to use native serial comm and not Qt version?
                          but you can try
                          for ( const QString& valline : values ) {
                          qDebug() << "val =" << valline.trimmed().toInt();
                          }
                          Trimmed() should remove the \n\r
                          http://doc.qt.io/qt-5/qstring.html#trimmed

                          ah. wait.
                          it looks like some of the values are in same index as timestamp
                          "23:28:55 \r\n187"

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 3 Aug 2018, 19:25 last edited by mrjj 8 Mar 2018, 19:29
                            #13

                            Hi
                            Im not really sure what is happening.
                            Seems to not to be structured as we think/needs more splitting

                            QString input{"85,2018/8/3,17:21:3\\n\\r85,2018/8/3,17:21:3\\n\\r95,2018/8/3,17:21:3"};
                              QStringList lines = input.split("\\n\\r");
                              for ( const QString& line : lines) {
                                QStringList values = line.split(",");
                                for ( const QString& valline : values ) {
                                  qDebug() << "val =" << valline.trimmed().toInt();
                                }
                              }
                            

                            val = 85
                            val = 0
                            val = 0
                            val = 85
                            val = 0
                            val = 0
                            val = 95
                            val = 0
                            val = 0

                            when do you start processing the input ?
                            After it all have been read or how do u know that input is ready ?

                            I 1 Reply Last reply 3 Aug 2018, 20:00
                            1
                            • M mrjj
                              3 Aug 2018, 19:25

                              Hi
                              Im not really sure what is happening.
                              Seems to not to be structured as we think/needs more splitting

                              QString input{"85,2018/8/3,17:21:3\\n\\r85,2018/8/3,17:21:3\\n\\r95,2018/8/3,17:21:3"};
                                QStringList lines = input.split("\\n\\r");
                                for ( const QString& line : lines) {
                                  QStringList values = line.split(",");
                                  for ( const QString& valline : values ) {
                                    qDebug() << "val =" << valline.trimmed().toInt();
                                  }
                                }
                              

                              val = 85
                              val = 0
                              val = 0
                              val = 85
                              val = 0
                              val = 0
                              val = 95
                              val = 0
                              val = 0

                              when do you start processing the input ?
                              After it all have been read or how do u know that input is ready ?

                              I Offline
                              I Offline
                              isan
                              wrote on 3 Aug 2018, 20:00 last edited by isan 8 May 2018, 14:20
                              #14

                              @mrjj it's return zero and sometimes 2018

                              when do you start processing the input ?
                              After it all have been read or how do u know that input is ready ?

                              I'm not sure what you mean?
                              read from serial port in thread and thread start in other class constructor.
                              so it's start when program run!
                              data send from arduino that connected to raspberry pi with USB port

                              M 1 Reply Last reply 3 Aug 2018, 21:00
                              0
                              • I isan
                                3 Aug 2018, 20:00

                                @mrjj it's return zero and sometimes 2018

                                when do you start processing the input ?
                                After it all have been read or how do u know that input is ready ?

                                I'm not sure what you mean?
                                read from serial port in thread and thread start in other class constructor.
                                so it's start when program run!
                                data send from arduino that connected to raspberry pi with USB port

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 3 Aug 2018, 21:00 last edited by
                                #15

                                @isan
                                Looking at the code, it seems to me you
                                send al data and thread will read it all. and then close serial.
                                so

                                while (serialDataAvail(fd) > -2) {
                                  //-------value format is int-------
                                  value = serialGetchar (fd) ;
                                  //--------vs format is QString------
                                  vs.push_back(value);  
                                 }
                                 serialClose(fd);
                                 
                                 // here u should have complete data and can use 
                                to get the values if data is complete
                                input would be vs
                                
                                  QStringList lines = input.split("\\n\\r");
                                  for ( const QString& line : lines) {
                                    QStringList values = line.split(",");
                                    for ( const QString& valline : values ) {
                                      qDebug() << "val =" << valline.trimmed().toInt();
                                    }
                                  }
                                }
                                
                                JonBJ I 2 Replies Last reply 3 Aug 2018, 21:17
                                1
                                • M mrjj
                                  3 Aug 2018, 21:00

                                  @isan
                                  Looking at the code, it seems to me you
                                  send al data and thread will read it all. and then close serial.
                                  so

                                  while (serialDataAvail(fd) > -2) {
                                    //-------value format is int-------
                                    value = serialGetchar (fd) ;
                                    //--------vs format is QString------
                                    vs.push_back(value);  
                                   }
                                   serialClose(fd);
                                   
                                   // here u should have complete data and can use 
                                  to get the values if data is complete
                                  input would be vs
                                  
                                    QStringList lines = input.split("\\n\\r");
                                    for ( const QString& line : lines) {
                                      QStringList values = line.split(",");
                                      for ( const QString& valline : values ) {
                                        qDebug() << "val =" << valline.trimmed().toInt();
                                      }
                                    }
                                  }
                                  
                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on 3 Aug 2018, 21:17 last edited by JonB 8 Mar 2018, 21:21
                                  #16

                                  @mrjj
                                  I have no idea of the implications, but you have pasted code for OP as:

                                    QStringList lines = input.split("\\n\\r");
                                  

                                  If he is supposed to be copying this, shouldn't he be using

                                    QStringList lines = input.split("\r\n");
                                  

                                  Or, from his qDebug(), does it mean that the input literally has the 4-character sequence \r\n in it, and not CR-LF? In which case he would want

                                  QStringList lines = input.split("\\r\\n");
                                  
                                  
                                  M 1 Reply Last reply 3 Aug 2018, 21:39
                                  3
                                  • JonBJ JonB
                                    3 Aug 2018, 21:17

                                    @mrjj
                                    I have no idea of the implications, but you have pasted code for OP as:

                                      QStringList lines = input.split("\\n\\r");
                                    

                                    If he is supposed to be copying this, shouldn't he be using

                                      QStringList lines = input.split("\r\n");
                                    

                                    Or, from his qDebug(), does it mean that the input literally has the 4-character sequence \r\n in it, and not CR-LF? In which case he would want

                                    QStringList lines = input.split("\\r\\n");
                                    
                                    
                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 3 Aug 2018, 21:39 last edited by
                                    #17

                                    @JonB
                                    Thank you , yes u are right its \r\n :) (of cause)
                                    or \r\n when escaped.

                                    1 Reply Last reply
                                    2
                                    • JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on 3 Aug 2018, 22:21 last edited by JonB 8 Mar 2018, 22:21
                                      #18

                                      Now, going back to where @isan wrote:

                                      return :
                                       \n191", "2018/8/3", "23:28:55 \r\n187", "2018/8/3", "23:28:55 \r\n185", "2018/8/3", "23:28:55 \r\n164", "2018/8/3", "23:28:55 \r\n131", "2018/8/3", "23:28:55 \r\n103", "2018/8/3", "23:28:55 \r\n....................................
                                      

                                      So if that's really right, after first splitting on "\r\n" (not even certain about that if the input really starts as shown with just \n and not \r\n, I'll just assume it's really \r\n), he then needs to split on "\",\"", not just plain ,. Then at the end of that you have 3 clean tokens per line.

                                      I have to say the input looks a bit oddly tokenized/quoted, but that's what corresponds to the input he shows. If you're not careful and leave " characters in, you'll get toInt() returning 0 where you don't expect.

                                      M 1 Reply Last reply 3 Aug 2018, 22:49
                                      2
                                      • JonBJ JonB
                                        3 Aug 2018, 22:21

                                        Now, going back to where @isan wrote:

                                        return :
                                         \n191", "2018/8/3", "23:28:55 \r\n187", "2018/8/3", "23:28:55 \r\n185", "2018/8/3", "23:28:55 \r\n164", "2018/8/3", "23:28:55 \r\n131", "2018/8/3", "23:28:55 \r\n103", "2018/8/3", "23:28:55 \r\n....................................
                                        

                                        So if that's really right, after first splitting on "\r\n" (not even certain about that if the input really starts as shown with just \n and not \r\n, I'll just assume it's really \r\n), he then needs to split on "\",\"", not just plain ,. Then at the end of that you have 3 clean tokens per line.

                                        I have to say the input looks a bit oddly tokenized/quoted, but that's what corresponds to the input he shows. If you're not careful and leave " characters in, you'll get toInt() returning 0 where you don't expect.

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 3 Aug 2018, 22:49 last edited by
                                        #19

                                        @JonB
                                        Hi
                                        I think the "s comes from qDebug and is not in the input. (looking at the sending code higher up)
                                        It seems he reads the entire load in one go and closes port so that means he should be able to
                                        read complete string and then split it. ( i hope )

                                        1 Reply Last reply
                                        2
                                        • M mrjj
                                          3 Aug 2018, 21:00

                                          @isan
                                          Looking at the code, it seems to me you
                                          send al data and thread will read it all. and then close serial.
                                          so

                                          while (serialDataAvail(fd) > -2) {
                                            //-------value format is int-------
                                            value = serialGetchar (fd) ;
                                            //--------vs format is QString------
                                            vs.push_back(value);  
                                           }
                                           serialClose(fd);
                                           
                                           // here u should have complete data and can use 
                                          to get the values if data is complete
                                          input would be vs
                                          
                                            QStringList lines = input.split("\\n\\r");
                                            for ( const QString& line : lines) {
                                              QStringList values = line.split(",");
                                              for ( const QString& valline : values ) {
                                                qDebug() << "val =" << valline.trimmed().toInt();
                                              }
                                            }
                                          }
                                          
                                          I Offline
                                          I Offline
                                          isan
                                          wrote on 4 Aug 2018, 03:49 last edited by isan 8 Apr 2018, 06:01
                                          #20

                                          @mrjj said in read data from serial port:

                                          @isan
                                          Looking at the code, it seems to me you
                                          send al data and thread will read it all. and then close serial.
                                          so

                                          while (serialDataAvail(fd) > -2) {
                                            //-------value format is int-------
                                            value = serialGetchar (fd) ;
                                            //--------vs format is QString------
                                            vs.push_back(value);  
                                           }
                                           serialClose(fd);
                                           
                                           // here u should have complete data and can use 
                                          to get the values if data is complete
                                          input would be vs
                                          
                                            QStringList lines = input.split("\\n\\r");
                                            for ( const QString& line : lines) {
                                              QStringList values = line.split(",");
                                              for ( const QString& valline : values ) {
                                                qDebug() << "val =" << valline.trimmed().toInt();
                                              }
                                            }
                                          }
                                          

                                          Data is always sent and It does not go out of while() and I can not wait for complete data
                                          Upon receive, I must use the data in other classes
                                          I should not miss any data, if I close the port, the data will be lost

                                          M 1 Reply Last reply 4 Aug 2018, 06:55
                                          0

                                          1/29

                                          3 Aug 2018, 15:18

                                          • Login

                                          • Login or register to search.
                                          1 out of 29
                                          • First post
                                            1/29
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved