How do I read data from a serial port? python
Hi please my main question as I am new to python.
I am trying to read data from a serial port. Basically a serial port is a USB port converted to a serial port practically. I am using arduino.
I tried this code first:
while(True):
ser=serial.Serial('COM6',9600)
bytoread=ser.inWaiting()
val=ser.read(bytoread)
But it gave me an error.
Permission Error(13,Access is denied, none 5)
But when I changed my code to
while(True):
ser=serial.Serial()
ser.baudrate=19600
ser.port='COM6'
ser
ser.open()
bytoread=ser.inWaiting()
val=ser.read(bytoread)
No permission error occurred, but the program is always busy connecting the port. I waited for many minutes, but he never moved forward. What am I doing wrong here?
+3
Rafay zia mir
source
to share
1 answer
you can do something like:
import serial
ser = serial.Serial('COM6', 9600, timeout=None)
while True:
data = ser.readline()
you cannot put ser = serial.Serial('COM5', 9600, timeout=None)
in a loop while
because it will constantly (re) create the connection ...
+3
Dadep
source
to share