25 lines
708 B
Python
25 lines
708 B
Python
|
|
from pybricks.iodevices import UARTDevice as _UARTDevice
|
||
|
|
from pybricks.tools import wait
|
||
|
|
from uerrno import ETIMEDOUT
|
||
|
|
|
||
|
|
class FakeUART:
|
||
|
|
def __init__(self, port, baudrate, timeout):
|
||
|
|
self.timeout = timeout
|
||
|
|
print("Warning: No physical UART detected. Using simulator.")
|
||
|
|
|
||
|
|
def read(self, length=1):
|
||
|
|
if self.timeout is not None:
|
||
|
|
wait(self.timeout)
|
||
|
|
raise OSError(ETIMEDOUT)
|
||
|
|
else:
|
||
|
|
while True:
|
||
|
|
wait(1000)
|
||
|
|
|
||
|
|
def write(self, data):
|
||
|
|
pass
|
||
|
|
|
||
|
|
def UARTDevice(port, baudrate=9600, timeout=None):
|
||
|
|
try:
|
||
|
|
return _UARTDevice(port, baudrate, timeout)
|
||
|
|
except OSError:
|
||
|
|
return FakeUART(port, baudrate, timeout)
|