Validate serial readingNon-blocking read on a subprocess.PIPE in pythonHow to read/process command line arguments?How do you read from stdin?How to read a file line-by-line into a list?Processing / Reading Serial Port Data Streams Crashing ProgramHow to read a large file, line by line, in PythonHow to read a text file into a string variable and strip newlines?Why is reading lines from stdin much slower in C++ than Python?SerialPort.BaseStream.ReadAsync drops or scrambles bytes when reading from a USB Serial PortMissing bytes in io.Reader with Serial communication

Does a bank have to tell me if a check made out to me was cashed there?

How do we say "within a kilometer radius spherically"?

Getting UPS Power from One Room to Another

Electricity free spaceship

What is the color of artificial intelligence?

Amplitude of a crest and trough in a sound wave?

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

Do you need to let the DM know when you are multiclassing?

Why does this query, missing a FROM clause, not error out?

Was Self-modifying-code possible just using BASIC?

What should I discuss with my DM prior to my first game?

If there's something that implicates the president why is there then a national security issue? (John Dowd)

Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?

Live action TV show where High school Kids go into the virtual world and have to clear levels

Is it okay to have a sequel start immediately after the end of the first book?

What are the implications when matrix's lowest eigenvalue is equal to 0?

Why do radiation hardened IC packages often have long leads?

Can the removal of a duty-free sales trolley result in a measurable reduction in emissions?

The usage of kelvin in formulas

How can one's career as a reviewer be ended?

Derivative of a double integral over a circular region

Who is "He that flies" in Lord of the Rings?

What is the logic behind charging tax _in the form of money_ for owning property when the property does not produce money?

Rail-to-rail op-amp only reaches 90% of VCC, works sometimes, not everytime



Validate serial reading


Non-blocking read on a subprocess.PIPE in pythonHow to read/process command line arguments?How do you read from stdin?How to read a file line-by-line into a list?Processing / Reading Serial Port Data Streams Crashing ProgramHow to read a large file, line by line, in PythonHow to read a text file into a string variable and strip newlines?Why is reading lines from stdin much slower in C++ than Python?SerialPort.BaseStream.ReadAsync drops or scrambles bytes when reading from a USB Serial PortMissing bytes in io.Reader with Serial communication






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm currently trying to read a serialport and the data is sometimes "corrupted".



I am expecting data like this



['CPS', '0', 'CPM', '11', 'uSv/hr', '0.06', 'SLOW']



The first reading after opening the serial port is always



['x00']



then reading data is fine for a unknown time, until the serialport delivers somethin like this



['CPS', '0', 'CPM', '21', 'uSv/hr', 'x000.11', 'SLOW']



I already tried to make sure the reading is correct by
this, rather ugly, condition checking:



serialData = serialConnection.readline()
serialData = "".join(serialData.split())
serialList = serialData.split(",")
if (len(serialList) < 7 or serialList[5][0] == "\"
or serialList[0] != "CPS" or serialList[2] != "CPM"):
uSvH = 0.1
else:
uSvH = float(serialList[5])


So I'm checking whether the read data is the correct size(seven entrys), the first entry always has to be CPS and when a x* is recieved instead of a float it sets the reading to a default value.



But somehow this fails when a corrupt reading occurs.



Is there a better way to sanitize the reading or make sure the data that is received is fine?










share|improve this question




























    1















    I'm currently trying to read a serialport and the data is sometimes "corrupted".



    I am expecting data like this



    ['CPS', '0', 'CPM', '11', 'uSv/hr', '0.06', 'SLOW']



    The first reading after opening the serial port is always



    ['x00']



    then reading data is fine for a unknown time, until the serialport delivers somethin like this



    ['CPS', '0', 'CPM', '21', 'uSv/hr', 'x000.11', 'SLOW']



    I already tried to make sure the reading is correct by
    this, rather ugly, condition checking:



    serialData = serialConnection.readline()
    serialData = "".join(serialData.split())
    serialList = serialData.split(",")
    if (len(serialList) < 7 or serialList[5][0] == "\"
    or serialList[0] != "CPS" or serialList[2] != "CPM"):
    uSvH = 0.1
    else:
    uSvH = float(serialList[5])


    So I'm checking whether the read data is the correct size(seven entrys), the first entry always has to be CPS and when a x* is recieved instead of a float it sets the reading to a default value.



    But somehow this fails when a corrupt reading occurs.



    Is there a better way to sanitize the reading or make sure the data that is received is fine?










    share|improve this question
























      1












      1








      1








      I'm currently trying to read a serialport and the data is sometimes "corrupted".



      I am expecting data like this



      ['CPS', '0', 'CPM', '11', 'uSv/hr', '0.06', 'SLOW']



      The first reading after opening the serial port is always



      ['x00']



      then reading data is fine for a unknown time, until the serialport delivers somethin like this



      ['CPS', '0', 'CPM', '21', 'uSv/hr', 'x000.11', 'SLOW']



      I already tried to make sure the reading is correct by
      this, rather ugly, condition checking:



      serialData = serialConnection.readline()
      serialData = "".join(serialData.split())
      serialList = serialData.split(",")
      if (len(serialList) < 7 or serialList[5][0] == "\"
      or serialList[0] != "CPS" or serialList[2] != "CPM"):
      uSvH = 0.1
      else:
      uSvH = float(serialList[5])


      So I'm checking whether the read data is the correct size(seven entrys), the first entry always has to be CPS and when a x* is recieved instead of a float it sets the reading to a default value.



      But somehow this fails when a corrupt reading occurs.



      Is there a better way to sanitize the reading or make sure the data that is received is fine?










      share|improve this question














      I'm currently trying to read a serialport and the data is sometimes "corrupted".



      I am expecting data like this



      ['CPS', '0', 'CPM', '11', 'uSv/hr', '0.06', 'SLOW']



      The first reading after opening the serial port is always



      ['x00']



      then reading data is fine for a unknown time, until the serialport delivers somethin like this



      ['CPS', '0', 'CPM', '21', 'uSv/hr', 'x000.11', 'SLOW']



      I already tried to make sure the reading is correct by
      this, rather ugly, condition checking:



      serialData = serialConnection.readline()
      serialData = "".join(serialData.split())
      serialList = serialData.split(",")
      if (len(serialList) < 7 or serialList[5][0] == "\"
      or serialList[0] != "CPS" or serialList[2] != "CPM"):
      uSvH = 0.1
      else:
      uSvH = float(serialList[5])


      So I'm checking whether the read data is the correct size(seven entrys), the first entry always has to be CPS and when a x* is recieved instead of a float it sets the reading to a default value.



      But somehow this fails when a corrupt reading occurs.



      Is there a better way to sanitize the reading or make sure the data that is received is fine?







      python serial-port






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 20:49









      r4ptorr4ptor

      2315




      2315






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Looks like I was thinking too complicated. A simple try except statement finished the job.



           if(len(serialList) == 7):
          try:
          uSvH = float(serialList[5])
          except ValueError:
          uSvH = 0.1
          if (len(serialList) < 7 or serialList[0] != "CPS" or serialList[2] != "CPM"):
          uSvH = 0.1


          Now the script passes some random readings.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328438%2fvalidate-serial-reading%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Looks like I was thinking too complicated. A simple try except statement finished the job.



             if(len(serialList) == 7):
            try:
            uSvH = float(serialList[5])
            except ValueError:
            uSvH = 0.1
            if (len(serialList) < 7 or serialList[0] != "CPS" or serialList[2] != "CPM"):
            uSvH = 0.1


            Now the script passes some random readings.






            share|improve this answer



























              0














              Looks like I was thinking too complicated. A simple try except statement finished the job.



               if(len(serialList) == 7):
              try:
              uSvH = float(serialList[5])
              except ValueError:
              uSvH = 0.1
              if (len(serialList) < 7 or serialList[0] != "CPS" or serialList[2] != "CPM"):
              uSvH = 0.1


              Now the script passes some random readings.






              share|improve this answer

























                0












                0








                0







                Looks like I was thinking too complicated. A simple try except statement finished the job.



                 if(len(serialList) == 7):
                try:
                uSvH = float(serialList[5])
                except ValueError:
                uSvH = 0.1
                if (len(serialList) < 7 or serialList[0] != "CPS" or serialList[2] != "CPM"):
                uSvH = 0.1


                Now the script passes some random readings.






                share|improve this answer













                Looks like I was thinking too complicated. A simple try except statement finished the job.



                 if(len(serialList) == 7):
                try:
                uSvH = float(serialList[5])
                except ValueError:
                uSvH = 0.1
                if (len(serialList) < 7 or serialList[0] != "CPS" or serialList[2] != "CPM"):
                uSvH = 0.1


                Now the script passes some random readings.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 14:22









                r4ptorr4ptor

                2315




                2315





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328438%2fvalidate-serial-reading%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript