I’m trying to read a serial port in Ruby using the serial port gem. The problem is that sometimes there is not data to be read and when I tell the program to read the serial port, the program hangs… I’ve tried using sp.read, sp.readline and sp.readlines; they all cause the program to block (under Windows).
Does anybody know if there is a way that will not cause blocking? Basically, I want that if there be no data to read, the
read result be nil.
Thanks.
You could use IO#read_nonblock: