read() blocks with 9 bytes waiting
Brought to you by:
cliechti
I have the following code:
#!/bin/env python3 # -*- coding: utf-8 -*- import serial class _main(object): def __init__(self, port='/dev/tty706'): self.openPort(port) while True: msg = self.waitForStart() msg += self.readUntilEOM() print("This is the message: %s" % repr(msg)) def openPort(self, port): self.port = serial.Serial(port=port, baudrate=4800, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=None) print("%s opened with %i baud. Timeout is %s seconds." % (self.port.name, self.port.baudrate, self.port.timeout)) def waitForStart(self): print("Waiting for start indicator (%i): " % self.port.inWaiting(), end='', flush=True) while True: byte = self.port.read(size=1) print(".", end='', flush=True) if byte == b'\x01': print("Found.") return byte def readUntilEOM(self): print("Reading data until EOM.", end='', flush=True) msg = b'' while True: print("%i>" % self.port.inWaiting(), end='', flush=True) byte = self.port.read(size=1) print(".", end='', flush=True) if byte == b'\x01': print("PROBLEM: Got start indicator, but message not yet finished.") break msg += byte if byte == b'\x04': break print("Got %i bytes." % len(msg)) return msg if __name__=='__main__': _main()
As you can see, I output the value of inWaiting() right before the read(size=1). However, it seems as if read() is blocking with 9 bytes still waiting in the buffer. The output looks like this:
/dev/tty706 opened with 4800 baud. Timeout is None seconds. Waiting for start indicator (0): ...........................Found. Reading data until EOM.127>.126>.125>.124>.123>.122>.121>.120>.119>.118>.117>.116>.115>.114>.113>.112>.111>.110>.109>.108>.107>.106>.105>.104>.103>.102>.101>.100>.99>.98>.97>.96>.95>.94>.93>.92>.91>.90>.89>.88>.87>.86>.85>.84>.83>.82>.81>.80>.79>.78>.77>.76>.75>.74>.73>.72>.71>.70>.69>.68>.67>.66>.65>.64>.63>.62>.61>.60>.59>.58>.57>.56>.55>.54>.53>.52>.51>.50>.49>.48>.47>.46>.45>.44>.43>.42>.41>.40>.39>.38>.37>.36>.35>.34>.33>.32>.31>.30>.29>.28>.27>.26>.25>.24>.23>.22>.21>.20>.19>.18>.17>.16>.15>.14>.13>.12>.11>.10>.9>.136>.135>.134>.133>.132>.131>.130>.129>.128>.127>.126>.125>.124>.123>.122>.121>.120>.119>.118>.117>.116>.115>.114>.113>.112>.111>.110>.109>.108>.107>.106>.105>.104>.103>.102>.101>.100>.99>.98>.97>.96>.95>.94>.93>.92>.91>.90>.89>.88>.87>.86>.85>.84>.83>.82>.81>.80>.79>.78>.77>.76>.75>.74>.73>.72>.71>.70>.69>.68>.67>.66>.65>.64>.63>.62>.61>.60>.59>.58>.57>.56>.55>.54>.53>.52>.51>.50>.49>.48>.47>.46>.45>.44>.43>.42>.41>.40>.39>.38>.37>.36>.35>.34>.33>.32>.31>.30>.29>.28>.27>.26>.25>.24>.23>.22>.21>.20>.19>.18>.17>.16>.15>.14>.13>.12>.11>.10>.9>.136>.135>.134>.133>.132>.131>.130>.129>.128>.127>.126>.125>.124>.123>.122>.121>.120>.119>.118>.117>.116>.115>.114>.113>.112>.111>.110>.109>.108>.107>.106>.105>.104>.103>.102>.101>.100>.99>.98>.97>.96>.95>.94>.93>.92>.91>.90>.89>.88>.87>.86>.85>.84>.83>.82>.81>.80>.79>.78>.77>.76>.75>.74>.73>.72>.71>.70>.69>.68>.67>.66>.65>.64>.63>.62>.61>.60>.59>.58>.57>.56>.55>.54>.53>.52>.51>.50>.49>.48>.47>.46>.45>.44>.43>.42>.41>.40>.39>.38>.37>.36>.35>.34>.33>.32>.31>.30>.29>.28>.27>.26>.25>.24>.23>.22>.21>.20>.19>.18>.17>.16>.15>.14>.13>.12>.11>.10>.9>.167>.166>.165>.164>.163>.162>.161>.160>.159>.158>.157>.156>.155>.154>.153>.152>.151>.150>.149>.148>.147>.146>.145>.144>.143>.142>.141>.140>.139>.138>.137>.136>.135>.134>.133>.132>.131>.130>.129>.128>.127>.126>.125>.124>.123>.122>.121>.120>.119>.118>.117>.116>.115>.114>.113>.112>.111>.110>.109>.108>.107>.106>.105>.104>.103>.102>.101>.100>.99>.98>.97>.96>.95>.94>.93>.92>.91>.90>.89>.88>.87>.86>.85>.84>.83>.82>.81>.80>.79>.78>.77>.76>.75>.74>.73>.72>.71>.70>.69>.68>.67>.66>.65>.64>.63>.62>.61>.60>.59>.58>.57>.56>.55>.54>.53>.52>.51>.50>.49>.48>.47>.46>.45>.44>.43>.42>.41>.40>.39>.38>.37>.36>.35>.34>.33>.32>.31>.30>.29>.28>.27>.26>.25>.24>.23>.22>.21>.20>.19>.18>.17>.16>.15>.14>.13>.12>.11>.10>.9>
And there it hangs until more data is received. The Waiting count never went below 9 during receiving. Why is that?