Python Forum
Read serial BR=9600 . print me 2 lines?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read serial BR=9600 . print me 2 lines?
#1
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)
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,292 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 781 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  failing to print not matched lines from second file tester_V 14 5,943 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,635 Jul-17-2021, 04:19 PM
Last Post: bill_z
  Why it does not print(file.read()) Rejaul84 1 2,307 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  [Solved] Trying to read specific lines from a file Laplace12 7 3,472 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  Stop the serial read until video is recorded vj01 1 1,713 Jun-10-2021, 09:56 AM
Last Post: Larz60+
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,465 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Need print out lines before and after the match tester_V 6 6,781 Aug-11-2020, 02:50 AM
Last Post: deanhystad
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,705 Aug-10-2020, 11:01 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020