david_1234
Posts: 330
Joined: Mon Jan 01, 2018 2:14 pm

Read serial show me 2 reading at the same time

Tue Dec 31, 2019 2:53 pm

Hello ,
I have a script that read data from serial sensor
the data is sent in BR 9600
i'm waiting 500ms from each reading - so I'm getting 2 reading at one time
how can I avoid this ?

this is the code :

Code: Select all

import serial
import time

ser = serial.Serial(
               port='/dev/serial0',
               baudrate = 9600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )

def ReadFromSerial():
            x=ser.readline()
           print x
           split = x.split("!")
           print (split[0])
           if (float(split[0]) >100):
              problem ="yes"
           else:
              problem = "no"
           return problem

while True:
     print ("Time: %s" %time.strftime("%H:%M:%S"))
     answer = ReadFromSerial()
     print(answer)
     time.sleep(0.5)

this is the output:

Code: Select all

Time: 17:04:27
23.88!104.5223.89!86.20
23.88
yes
Time: 17:04:29
23.88!106.1223.89!94.20
23.88
yes
Time: 17:04:32
23.89!94.5223.88!100.00
23.89
yes

Thanks ,

Return to “Python”