-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial4wire.py
More file actions
143 lines (105 loc) · 3.92 KB
/
Copy pathserial4wire.py
File metadata and controls
143 lines (105 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import code
import serial
from enum import Enum
# Change port here.
PORT = "/dev/ttyACM0"
# Command characters.
class Command(Enum):
SEND = "S"
READ = "R"
STB_HIGH = "H"
STB_LOW = "L"
class SerialDevice:
def __init__(self, port):
self.ser = serial.Serial()
self.ser.port = port
self.ser.baudrate = 9600
print("[4WireSerial] Waiting for device at port:", self.ser.name)
while True:
try:
self.ser.open()
except serial.SerialException:
pass
else:
print("[4WireSerial] Port opened at:", self.ser.name)
print("[4WireSerial] Waiting for ready signal...")
break
self.ser.read() # wait for ready signal
print("[4WireSerial] Device ready!")
# Low level methods:
def send(self, *data: int):
self.ser.write(Command.SEND.value.encode())
self.ser.write(len(data).to_bytes(1))
self.ser.write(bytes(data))
sent_bytes = self.ser.read()
return int.from_bytes(sent_bytes)
def recv(self, count: int = 1):
self.ser.write(Command.READ.value.encode())
self.ser.write(count.to_bytes(1))
received_bytes = list(self.ser.read(count))
return received_bytes
def stb(self, val: bool):
cmd = Command.STB_HIGH if val else Command.STB_LOW
self.ser.write(cmd.value.encode())
self.ser.read() # ACK
# Other convenient methods:
def send_str(self, string: str):
sent_bytes = self.send(*tuple(map(ord, string)))
return sent_bytes
def send_cmd(self, cmd: int, *data: int):
self.stb(False)
sent_bytes = self.send(cmd)
if data != ():
sent_bytes += self.send(*data)
self.stb(True)
return sent_bytes
def recv_data(self, cmd: int, count: int = 1):
self.stb(False)
self.send(cmd)
received_bytes = self.recv(count)
self.stb(True)
return received_bytes
if __name__ == "__main__":
dbg = SerialDevice(PORT)
# Wrapper functions for verbosity and convenience in interactive mode:
def send(*data: int):
print("\n HEX BIN DEC")
for n in range(len(data)):
print("[{0:02d}] {1:02X} : {1:08b} : {1:03d}".format(n + 1, data[n]))
sent = dbg.send(*data)
print(f"\n{sent} Byte(s) sent.\n")
def recv(count: int = 1):
print("\n HEX BIN DEC")
received = dbg.recv(count)
for n in range(count):
print("[{0:02d}] {1:02X} : {1:08b} : {1:03d}".format(n + 1, received[n]))
print(f"\n{count} Byte(s) received.\n")
def stb(val: bool):
dbg.stb(val)
print("\nStrobe pin held {}.\n".format("HIGH" if val else "LOW"))
def send_str(string: str):
print("\n HEX BIN DEC ASCII")
for n in range(len(string)):
print(
"[{0:02d}] {1:02X} : {1:08b} : {1:03d} : {1:c}".format(
n + 1, ord(string[n])
)
)
sent = dbg.send_str(string)
print(f"\n{sent} Character(s) sent.\n")
def send_cmd(cmd: int, *data: int):
print("\n HEX BIN DEC")
print("[CMD] {0:02X} : {0:08b} : {0:03d}".format(cmd))
for n in range(len(data)):
print("[{0:03d}] {1:02X} : {1:08b} : {1:03d}".format(n + 1, data[n]))
sent = dbg.send_cmd(cmd, *data)
print(f"\n{sent} Byte(s) sent.\n")
def recv_data(cmd: int, count: int = 1):
print("\n HEX BIN DEC")
received = dbg.recv_data(cmd, count)
for n in range(count):
print("[{0:02d}] {1:02X} : {1:08b} : {1:03d}".format(n + 1, received[n]))
print(f"\n{count} Byte(s) received.\n")
# Start interactive console.
print("[4WireSerial] Starting interactive console:\n")
code.interact(local=locals(), banner="")