Im newbie for python and serial port. I want to monitor serial port continuously. If port not opened or access denied, need to run the python script without stop. I had done something, But that script has stopped when the PORT not opened or access denied. kindly, help someone to close this issue.
import serial
z1baudrate = 9600
z1port = 'COM4'
z1serial = serial.Serial(port=z1port, baudrate=z1baudrate,timeout=1)
try:
if z1serial.is_open:
while True:
size = z1serial.inWaiting()
if size:
data = z1serial.read(size)
res= data.decode("utf-8")
print(res)
else:
print("Data not reading")
time.sleep(1)
else:
z1serial.close()
print('z1serial not open or Already in use')
except serial.SerialException as e:
z1serial.close()
print('COM4 not open')