Trying to read from a serial barcode scanner, run by a person.
Two things have popped up that I’m not sure how to solve:
1: Possibility of scans when I’m not looking for them, generating junk data in the buffer. I can’t figure out how to dump incoming buffer, except by using a timer that continuously reads the port at a 1 sec interval with a short timeout. This only runs if my PLC request to scan is idle.
Is there a better approach?
2: readline and readuntil don’t seem to handle timeout well, for example just return a null string. Given that, it might be useful to amend the example to be this:
with system.serial.port(comPort, bitRate=system.serial.BIT_RATE_9600) as port: try: line = port.readLine(15000) #readline reads until it gets \n, \r, or \r\n.``` except: line=''
Did I miss a simple way of detecting the timeout condition?