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
Forum Updated to NodeBB v4.3 + New Features

read data from serial port

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 4 Posters 6.1k Views 1 Watching
  • 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.
  • mrjjM mrjj

    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 last edited by isan
    #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?

    mrjjM 1 Reply Last reply
    0
    • I isan

      @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?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on 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
      0
      • mrjjM mrjj

        @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 last edited by isan
        #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
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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
          2
          • mrjjM mrjj

            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 last edited by isan
            #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

            mrjjM 1 Reply Last reply
            0
            • I isan

              @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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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
              2
              • mrjjM mrjj

                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 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!

                mrjjM 1 Reply Last reply
                0
                • I isan

                  @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!

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • mrjjM mrjj

                    @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 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
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #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
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #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
                        1
                        • mrjjM mrjj

                          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 last edited by isan
                          #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

                          mrjjM 1 Reply Last reply
                          0
                          • I isan

                            @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

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 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
                            1
                            • mrjjM mrjj

                              @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 last edited by JonB
                              #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");
                              
                              
                              mrjjM 1 Reply Last reply
                              3
                              • JonBJ JonB

                                @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");
                                
                                
                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 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 last edited by JonB
                                  #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.

                                  mrjjM 1 Reply Last reply
                                  2
                                  • JonBJ JonB

                                    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.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 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
                                    • mrjjM mrjj

                                      @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 last edited by isan
                                      #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

                                      mrjjM 1 Reply Last reply
                                      0
                                      • I isan

                                        @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

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #21

                                        @isan
                                        ok so it must be on the fly.

                                        Then you need to use 2 buffers as not to parse already received data over and over.
                                        so in pseudo code

                                        Read char from serial
                                        tempbuffer += char
                                        if char is \n then
                                        split tempbuffer
                                        emit value
                                        clear tempbuffer

                                        //i assume u want to store all ?
                                        Mainbuffer +=char

                                        I 1 Reply Last reply
                                        1
                                        • mrjjM mrjj

                                          @isan
                                          ok so it must be on the fly.

                                          Then you need to use 2 buffers as not to parse already received data over and over.
                                          so in pseudo code

                                          Read char from serial
                                          tempbuffer += char
                                          if char is \n then
                                          split tempbuffer
                                          emit value
                                          clear tempbuffer

                                          //i assume u want to store all ?
                                          Mainbuffer +=char

                                          I Offline
                                          I Offline
                                          isan
                                          wrote on last edited by isan
                                          #22

                                          @mrjj so the code is like:

                                          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);
                                              //--------tempbuffer format is QString------//
                                             tempbuffer .append( vs);
                                             if (vs=="\n")
                                            //-------- values format is QStringList------//
                                               values = tempbuffer.split(",");
                                                x= values[0].toInt();
                                           msleep(1); 
                                                 emit signalValueUpdated(x);
                                                 tempbuffer.clear();
                                            
                                              }
                                              serialClose(fd); 
                                          }
                                          

                                          is it true?

                                          mrjjM 1 Reply Last reply
                                          0

                                          • Login

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