#!/usr/bin/python3 |
importserial, time, sys, threading |
fromcoloramaimportFore, Style, initascolorama_init |
colorama_init() |
# lock to serialize console output |
lock=threading.Lock() |
classHighlight: |
def__init__(self, clazz, color): |
self.color=color |
self.clazz=clazz |
def__enter__(self): |
print(self.color, end=') |
def__exit__(self, type, value, traceback): |
ifself.clazzFore: |
print(Fore.RESET, end=') |
else: |
assertself.clazzStyle |
print(Style.RESET_ALL, end=') |
sys.stdout.flush() |
iflen(sys.argv) !=3andlen(sys.argv) !=4: |
sys.stderr.write('Usage: %s <baud> <port1> [<port2>]n'% (sys.argv[0])) |
exit(1) |
defread_serial(port, baud, color): |
ser=serial.Serial() |
ser.port=port |
ser.baudrate=baud |
ser.bytesize=serial.EIGHTBITS#number of bits per bytes |
ser.parity=serial.PARITY_NONE#set parity check: no parity |
ser.stopbits=serial.STOPBITS_ONE#number of stop bits |
#ser.timeout = None #block read |
ser.timeout=0# non blocking read |
ser.xonxoff=False#disable software flow control |
ser.rtscts=False#disable hardware (RTS/CTS) flow control |
ser.dsrdtr=False#disable hardware (DSR/DTR) flow control |
ser.writeTimeout=2#timeout for write |
try: |
ser.open() |
exceptExceptionase: |
print('error open serial port: '+str(e)) |
exit() |
ifser.isOpen(): |
try: |
whileTrue: |
c=ser.read(size=1024) |
withlock: |
iflen(c) >0: |
print(color) |
sys.stdout.buffer.write(c) |
ser.close() |
exceptExceptionase1: |
print ('error communicating..: '+str(e1)) |
else: |
print('cannot open serial port ') |
exit() |
# Create two threads as follows |
try: |
t=threading.Thread(target=read_serial, args=(sys.argv[2], sys.argv[1], Fore.GREEN )) |
t.daemon=True# thread dies when main thread (only non-daemon thread) exits. |
t.start() |
iflen(sys.argv) 4: |
t=threading.Thread(target=read_serial, args=(sys.argv[3], sys.argv[1], Fore.RED )) |
t.daemon=True# thread dies when main thread (only non-daemon thread) exits. |
t.start() |
except: |
print('Error: unable to start thread') |
try: |
whileTrue: |
pass |
exceptKeyboardInterrupt: |
exit() |
To read single byte from serial device. Data = ser.read to read given number of bytes from the serial device. Data = ser.read(size=5) to read one line from serial device. Data = ser.readline to read the data from serial device while something is being written over it. #for python2.7 data = ser.read(ser.inWaiting) #for python3 ser.read(ser. Open named port at “19200,8,N,1”, 1s timeout: with serial.Serial('/dev/ttyS1', 19200, timeout=1) as ser. X = ser.read # read one byte. S = ser.read(10) # read up to ten bytes (timeout). Line = ser.readline # read a ' ' terminated line. Open port at “38400,8,E,1”, non blocking HW handshaking. CGI example and TinyWeb weight together only 375 kb with Python 2.2!). Hint 3: When serving files (not CGI), TinyWeb uses Windows file extension Return type: bytes Read size bytes from the serial port. If a timeout is set it may return less characters as requested. Now I am trying to achieve the same thing using Python serial connector. If there is other stuff to be done on the Arduino (perhaps if you are doing some filtering of the temperature data using the Arduino so as to reduce noise) then it might make more sense for it to push data to the serial stream at the end of each loop, and for the python script to only read the data every 5 seconds.
commented Jun 5, 2018
Python Serial Read Time Out Examples
commented Jun 25, 2018
when I run this code on spyder IDE . i got this error: runfile('C:/Users/iit/readserial.py', wdir='C:/Users/iit') Optimus 7.0 3. Gallery is full of eye-candyThe Pictures hub is the photo gallery of Windows Phone 7. When you start it, it shows the most recent photos (including those from the camera), What’s new (which shows photos from online services) and a menu, which lets you view all photos, photos sorted by date and your favorite photos.The Pictures hub. |