python - PySerial: how to understand that the timeout occured while reading from serial port? -
i'm using pyserial
read serial port in code below. checkreaduntil()
read output of command send serial port until sequence of symbols readuntil
in serial output.
... self.ser = serial.serial(comdev, 115200, timeout=10) ... #function continue read serial port until 'readuntil' #sequence of symbols appears def checkreaduntil(self, readuntil): outputcharacters = [] while 1: ch = self.ser.read() outputcharacters += ch if outputcharacters[-len(readuntil):]==readuntil: break outputlines = ''.join(outputcharacters) return outputlines
however, if there no sequence readuntil
(for reason), i'm stuck in function checkreaduntil()
forever. setting timeout=10
sets timeout i'm stuck in loop iterates every 10 seconds , nothing, waiting.
how possible understand there timeout event may exit infinite loop? output length may different.
update (previous answer not correct, working code @konstantin):
... self.ser = serial.serial(comdev, 115200, timeout=10) ... #function continue read serial port until 'readuntil' #sequence of symbols appears def checkreaduntil(self, readuntil): outputcharacters = [] while 1: ch = self.ser.read() if len(ch) == 0: break outputcharacters += ch if outputcharacters[-len(readuntil):]==readuntil: break outputlines = ''.join(outputcharacters) return outputlines
Comments
Post a Comment