1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-16 03:18:09 +00:00
trezor-firmware/trezorlib/transport_serial.py

39 lines
1.2 KiB
Python
Raw Normal View History

2012-11-13 14:09:39 +00:00
'''SerialTransport implements wire transport over serial port.'''
# Local serial port loopback: socat PTY,link=COM8 PTY,link=COM9
import serial
2012-11-15 20:08:02 +00:00
from select import select
2012-11-13 14:09:39 +00:00
from transport import Transport
class SerialTransport(Transport):
def __init__(self, device, *args, **kwargs):
self.serial = None
super(SerialTransport, self).__init__(device, *args, **kwargs)
def _open(self):
self.serial = serial.Serial(self.device, 115200, timeout=10, writeTimeout=10)
def _close(self):
self.serial.close()
self.serial = None
2012-11-15 20:08:02 +00:00
def ready_to_read(self):
rlist, _, _ = select([self.serial], [], [], 0)
return len(rlist) > 0
2012-11-13 14:09:39 +00:00
def _write(self, msg):
try:
self.serial.write(msg)
self.serial.flush()
except serial.SerialException:
print "Error while writing to socket"
raise
def _read(self):
try:
(msg_type, datalen) = self._read_headers(self.serial)
return (msg_type, self.serial.read(datalen))
except serial.SerialException:
print "Failed to read from device"
raise