'How to read COM Port output after entering input
I am using python and pyserial to control LED Lights (green, yellow and red)
That's how initialize my serial port:
def initComPort():
serialPort = serial.Serial(
port="COM3", baudrate=9600, bytesize=8, parity='N', stopbits=serial.STOPBITS_ONE, xonxoff=0
)
return serialPort
Whenever I want to set the first light to green I use this:
def gLight():
initComPort().write(str.encode('WR10000\r'))
Now I want to get the current status, like, what light is turned on by using the following information click here
Now, when using Putty and entering RD with a Return I get "10000"
But I am not able to recreate this in Python, I tried using this but the code always seems to wait at that readline() function:
def getState():
initComPort().write(str.encode('RD\r'))
print('Write command sent!')
out = ' '
time.sleep(1)
out += initComPort().readline()
print (">>" + out)
Using read(5) did not solve my problem either, what am I doing wrong?
Solution 1:[1]
Seems like whenever I use
initComPort().write(str.encode('RD\r'))
I am creating a new serial variable or something like that
so I tried using this instead while encoding the byte output into string, and now its working!
test = initComPort()
test.write(str.encode('RD\r'))
print('Write command sent!')
out = str(test.readline())
print (out)
kek.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hek |
