forked from bobjacobsen/python-openlcb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_cdi_access.py
More file actions
301 lines (244 loc) · 9.23 KB
/
Copy pathexample_cdi_access.py
File metadata and controls
301 lines (244 loc) · 9.23 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
'''
Demo of using the memory service to read the CDI from memory, then an
example of parsing
Usage:
python3 example_memory_transfer.py [host|host:port]
Options:
host|host:port (optional) Set the address (or using a colon,
the address and port). Defaults to a hard-coded test
address and port.
'''
# region same code as other examples
import copy
import sys
import time
from typing import Union
import xml.sax
import xml.sax.handler
import xml.sax.xmlreader # for static type hints, autocomplete in this case
from logging import getLogger
from examples_settings import Settings # do 1st to fix path if no pip install
from openlcb import precise_sleep
from openlcb.dataprocessor import DataFormat
from openlcb.dataprocessormemo import DataProcessorMemo
from openlcb.memoryreadjob import MemoryReadJob
from openlcb.convert import Convert
from openlcb.memoryspace import MemorySpace
from openlcb.xmldataprocessor import attrs_to_dict
from openlcb.tcplink.tcpsocket import TcpSocket
settings = Settings()
if __name__ == "__main__":
settings.load_cli_args(docstring=__doc__)
logger = getLogger(__file__)
else:
logger = getLogger(__name__)
# endregion same code as other examples
from openlcb.canbus.canphysicallayergridconnect import ( # noqa:E402
CanPhysicalLayerGridConnect,
)
from openlcb.canbus.canlink import CanLink # noqa:E402
from openlcb.nodeid import NodeID # noqa:E402
from openlcb.datagramservice import ( # noqa:E402
DatagramService,
)
from openlcb.memoryservice import ( # noqa:E402
MemoryReadMemo,
MemoryService,
)
# specify connection information
# region moved to settings
# host = "192.168.16.212"
# port = 12021
# localNodeID = "05.01.01.01.03.01"
# # farNodeID = "09.00.99.03.00.35"
# farNodeID = "02.01.57.00.04.9C"
# endregion moved to settings
sock = TcpSocket()
# s.settimeout(30)
sock.connect(settings['host'], settings['port'])
# print("RR, SR are raw socket interface receive and send;"
# " RL, SL are link interface; RM, SM are message interface")
# def sendToSocket(frame: CanFrame):
# string = frame.encodeAsString()
# # print(" SR: {}".format(string.strip()))
# sock.sendString(string)
# physicalLayer.onFrameSent(frame)
def printFrame(frame):
# print(" RL: {}".format(frame))
pass
def printMessage(message):
# print("RM: {} from {}".format(message, message.source))
pass
def printDatagram(memo):
"""A call-back for when datagrams received
Args:
DatagramReadMemo: The datagram object
Returns:
bool: Always False (True would mean we sent a reply to the datagram,
but let the MemoryService do that).
"""
# print("Datagram receive call back: {}".format(memo.data))
return False
physicalLayer = CanPhysicalLayerGridConnect()
physicalLayer.registerFrameReceivedListener(printFrame)
canLink = CanLink(physicalLayer, NodeID(settings['localNodeID']))
canLink.registerMessageReceivedListener(printMessage)
datagramService = DatagramService(canLink)
canLink.registerMessageReceivedListener(datagramService.process)
datagramService.registerDatagramReceivedListener(printDatagram)
memoryService = MemoryService(datagramService)
#######################
# The XML parsing section.
#
# This creates a handler object that just prints
# information as it's presented.
#
# Since `characters` can be called multiple times
# in a row, we buffer up the characters until the `endElement`
# call is invoked to indicate the text is complete
class MyHandler(xml.sax.handler.ContentHandler):
"""XML SAX callbacks in a handler object
Attributes:
_chunks (list[str]): Collects chunks of data.
This is implementation-specific, and not
required if streaming (parser.feed).
_tmp_address (int|None): For sanity check, not actual address.
See replicatedTree docstring.
"""
def __init__(self):
self._chunks = []
self.stack = []
self.cursorCol = 0
self._tmp_space = None # type: int|None
self._tmp_address = None # type: int|None
def startElement(self, name: str, attrs: xml.sax.xmlreader.AttributesImpl):
"""See xml.sax.handler.ContentHandler documentation."""
self.stack.append(name)
if self.cursorCol != 0:
self.print()
self.write(name)
if attrs is not None and attrs:
self.print(" {}".format(attrs_to_dict(attrs)))
def endElement(self, name: str):
"""See xml.sax.handler.ContentHandler documentation."""
content = self._flushCharBuffer().strip()
if self.cursorCol != 0:
self.print()
if content:
self.print('/{} "{}"'.format(name, content))
else:
self.print('/{}'.format(name))
self.stack.pop()
# self.print("/", name)
pass
def write(self, *args, **kwargs):
args = list(args)
if self.cursorCol == 0:
tab = len(self.stack)*" "
self.cursorCol += len(tab)
args.insert(0, tab) # prepend indent
for arg in args:
sys.stdout.write(arg)
self.cursorCol += len(arg)
sys.stdout.flush()
def print(self, *args, **kwargs):
if self.cursorCol == 0: # No indent yet, so use write.
self.write(*args, **kwargs)
print()
else:
print(*args, **kwargs)
self.cursorCol = 0
def _flushCharBuffer(self):
"""Decode the buffer, clear it, and return all content.
See xml.sax.handler.ContentHandler documentation.
Returns:
str: The content of the bytes buffer decoded as utf-8.
"""
s = ''.join(self._chunks)
self._chunks.clear()
return s
def characters(self, content: str):
"""Received characters handler.
See xml.sax.handler.ContentHandler documentation.
Args:
data (Union[bytearray, bytes, list[int]]): any
data (any type accepted by bytearray extend).
"""
if not isinstance(content, str):
raise TypeError("Expected str, got {}"
.format(type(content).__name__))
self._chunks.append(content)
handler = MyHandler()
#######################
# have the socket layer report up to bring the link layer up and get an alias
print(" QUEUE frames : link up...")
physicalLayer.physicalLayerUp()
print(" QUEUED frames : link up...waiting...")
while canLink.pollState() != CanLink.State.Permitted:
# provides incoming data to physicalLayer & sends queued:
physicalLayer.receiveAll(sock, verbose=True)
physicalLayer.sendAll(sock)
if canLink.getState() == CanLink.State.WaitForAliases:
# physicalLayer.receiveAll(sock, verbose=True)
physicalLayer.sendAll(sock)
# ^ prevent assertion error below, proceed to send.
if canLink.pollState() == CanLink.State.Permitted:
break
assert canLink.getWaitForAliasResponseStart() is not None, \
("openlcb didn't send the 7,6,5,4 CID frames (state={})"
.format(canLink.getState()))
precise_sleep(.02)
print(" SENT frames : link up")
job = MemoryReadJob(memoryService)
def statusCallback(memo: DataProcessorMemo):
if memo.status:
print(memo.status)
farNodeID = NodeID(settings['farNodeID'])
time.sleep(.21)
# ^ 200ms: See section 6.2.1 of CAN Frame Transfer Standard
# (CanLink.State.Permitted will only occur after that, but waiting
# now will reduce output & delays below in this example).
while canLink.getState() != CanLink.State.Permitted:
print("Waiting for connection sequence to complete...")
# This delay could be .2 (per alias collision), but longer to
# reduce console messages:
time.sleep(.5)
print(f"CanLink state={canLink.getState()}")
waited = 0
delaySec = 1
while farNodeID not in canLink.nodeIdToAlias:
# canLink.pollState()
physicalLayer.receiveAll(sock)
physicalLayer.sendAll(sock)
time.sleep(delaySec)
waited += delaySec
print(f"Connected nodes: {canLink.nodeIdToAlias}")
print(f"Waiting for {farNodeID} ({waited}s)...")
job.readMemory(canLink, farNodeID, MemorySpace.CDI,
handler=handler,
callback=statusCallback)
previous_nodes = copy.deepcopy(canLink.nodeIdToAlias)
# process resulting activity
print()
print("This example will exit on failure or complete data.")
while not job.completeData and not job.failed:
# In this example, requests are initiate by the
# memoryRead thread, and receiveAll actually
# receives the data from the requested memory space (CDI in this
# case) and offset (incremental position in the file/data,
# incremented by this example's memoryReadSuccess handler).
count = 0
count += physicalLayer.receiveAll(sock)
count += physicalLayer.sendAll(sock)
if canLink.nodeIdToAlias != previous_nodes:
print("nodeIdToAlias updated: {}".format(canLink.nodeIdToAlias))
if count < 1:
precise_sleep(.01)
# else skip sleep to avoid latency (port already delayed)
if canLink.nodeIdToAlias != previous_nodes:
previous_nodes = copy.deepcopy(canLink.nodeIdToAlias)
physicalLayer.physicalLayerDown()
if job.failed:
print("Read complete (FAILED)")
else:
print("Read complete (OK)")