py9b/py9b/transport/base.py

59 lines
1.2 KiB
Python
Raw Normal View History

2018-10-15 12:10:00 +00:00
"""Transport abstract class"""
2019-10-20 06:49:22 +00:00
def checksum(data):
s = 0
for c in data:
s += c
return (s & 0xFFFF) ^ 0xFFFF
2018-10-15 12:10:00 +00:00
2018-12-01 22:24:21 +00:00
2018-10-15 12:10:00 +00:00
class BaseTransport(object):
2019-10-20 06:49:22 +00:00
MOTOR = 0x01
ESC = 0x20
BLE = 0x21
BMS = 0x22
EXTBMS = 0x23
HOST = 0x3E
DeviceNames = {
MOTOR: "MOTOR",
ESC: "ESC",
BLE: "BLE",
BMS: "BMS",
EXTBMS: "EXTBMS",
HOST: "HOST",
}
def __init__(self, link):
self.link = link
def recv(self):
raise NotImplementedError()
def send(self, src, dst, cmd, arg, data=""):
raise NotImplementedError()
def execute(self, command):
self.send(command.request)
if not command.has_response:
return True
# TODO: retry ?
exc = None
for n in range(1):
try:
rsp = self.recv()
return command.handle_response(rsp)
except Exception as e:
print("retry")
exc = e
pass
raise exc
@staticmethod
def GetDeviceName(dev):
return BaseTransport.DeviceNames.get(dev, "%02X" % (dev))
2018-12-01 22:24:21 +00:00
2018-10-15 12:10:00 +00:00
__all__ = ["checksum", "BaseTransport"]