Python Forum
Read serial BR=9600 . print me 2 lines? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Read serial BR=9600 . print me 2 lines? (/thread-23464.html)



Read serial BR=9600 . print me 2 lines? - korenron - Dec-31-2019

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 ?
Time: 17:11:59
23.89!95.1223.88!99.60
23.89
no
Time: 17:12:01
23.88!102.2023.88!100.20
23.88
no
Time: 17:12:03
23.89!93.9223.88!98.52
23.89
no
Time: 17:12:05
23.88!104.5223.89!91.72
23.88
no
this is the code :

import serial
import time
import I2C_LCD_driver

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)



RE: Read serial BR=9600 . print me 2 lines? - DeaD_EyE - Dec-31-2019

You could use read_until('!')

You're still using Python 2.7. If you swap over to Python 3, then the read methods return bytes and not a str.