-
Notifications
You must be signed in to change notification settings - Fork 4
/
sdoplg.py
362 lines (313 loc) · 11.5 KB
/
sdoplg.py
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/python
"""
Utility for read/write Service Data Objects
usage:
./sdoplg.py <nodeID:index:subindex> [-wfsb] [<filename>]
-w ... write
-f ... file
-s ... string
-b ... byte
-h ... hex
"""
from can import CAN
import sys
def printAbort( errClass, errCode, addCode ):
"Print About message for given class, code and additional code"
errClassMsg = {
5:"Service Error",
6:"Access Error",
8:"Other Error"
}
errCodeMsg = {
1:"Object access unsupported",
2:"Object non-existent",
3:"Parameter Inconsistent",
6:"Hardware fault",
7:"Type conflict",
}
errTri = {
(5,3,0):"Toggle bit not alternated",
(6,2,0):"Object does not exist",
(6,6,0):"Acceess failed due to hardware",
(6,7,0x12):"Object length too high",
(6,7,0x13):"Object length too low"
}
try:
print "Abort:", errTri[ (errClass, errCode, addCode ) ] + " (" + errClassMsg[ errClass ] + ", " + errCodeMsg[ errCode ] + ")"
except:
print "Unknown abort code:", (errClass, errCode, addCode )
class SDOPlg:
def __init__(self, can = None, verbose = 1):
if can is None:
can = CAN()
# can.resetModules() # event this sometimes does not help :(
self.can = can
self.verbose = verbose
def readSDO( self, nodeID, objectIndex, subIndex ):
"Read Dictionary Object via CANopen Service Data Objects (SDO)"
self.can.sendData( 0x600 + nodeID, [0x40, objectIndex&0xFF, objectIndex>>8, subIndex, 0,0,0,0] )
toggleBit = 0x00
wholeData = []
while True:
id,data = self.can.readPacket()
self.can.printPacket( id, data )
if id == 0x580 + nodeID:
assert( len(data) == 8 )
if data[0] == 0x80: # Abort
printAbort( data[7], data[6], (data[5]<<8)|data[4] )
return [] # throw exception?
if (data[0] & 0xF3) == 0x43: # expedited transfer
assert( ((data[2]<<8)|data[1]) == objectIndex )
assert( data[3] == subIndex )
num = 4 - ((data[0]>>2) & 0x3)
return data[4:4+num]
# multi packet transfer (segmented)
if data[0] == 0x41 : # init
assert( ((data[2]<<8)|data[1]) == objectIndex )
assert( data[3] == subIndex )
totalLength = data[4] + (data[5]<<8) + (data[6]<<16) + (data[7]<<24)
print "TotalLength", totalLength
else:
assert( (data[0] & 0xF0) == 0x00 | toggleBit )
toggleBit = 0x10 - toggleBit
if data[0] & 0x01: # last segment
num = 7 - ((data[0]>>1) & 0x7 )
wholeData.extend( data[1:1+num] )
return wholeData
else:
wholeData.extend( data[1:] )
self.can.sendData( 0x600 + nodeID, [0x60|toggleBit, objectIndex&0xFF, objectIndex>>8, subIndex, 0,0,0,0] )
def writeSDO( self, nodeID, objectIndex, subIndex, data ):
"Write Dictionary Object via CANopen Service Data Objects (SDO)"
remainingData = []
if len(data) <= 4:
# expedited transfer
cmd = 0x23 | ((4-len(data))<<2)
d = data
d.extend( [0,0,0,0] )
self.can.sendData( 0x600 + nodeID, [cmd, objectIndex&0xFF, objectIndex>>8, subIndex, d[0],d[1],d[2],d[3]] )
else:
cmd = 0x21
l = len(data)
self.can.sendData( 0x600 + nodeID, [cmd, objectIndex&0xFF, objectIndex>>8, subIndex, l&0xFF, (l>>8)&0xFF, (l>>16)&0xFF, (l>>24)&0xFF] )
remainingData = data
toggleBit = 0x00
while True:
id,data=self.can.readPacket()
self.can.printPacket( id, data )
if id == 0x580 + nodeID:
assert( len(data) == 8 )
if data[0] == 0x80: # Abort
printAbort( data[7], data[6], (data[5]<<8)|data[4] )
return # throw exception?
sendNextData = False
if data[0] == 0x60: # both expedited & segmented transfer
assert( ((data[2]<<8)|data[1]) == objectIndex )
assert( data[3] == subIndex )
sendNextData = True
if data[0] == 0x20 or data[0] == 0x30:
assert( data[0] & 0x10 == toggleBit )
toggleBit = 0x10 - toggleBit
sendNextData = True
if sendNextData:
if len(remainingData) == 0:
return
# next 7 bytes
if len(remainingData) > 7:
cmd = 0x00 | toggleBit
self.can.sendData( 0x600 + nodeID, [cmd] + remainingData[0:7] )
remainingData = remainingData[7:]
else:
cmd = 0x01 | toggleBit | ((7-len(remainingData))<<1) # set last segment
d = remainingData
d.extend( [0,0,0,0,0,0,0] )
self.can.sendData( 0x600 + nodeID, [cmd] + d[0:7] )
remainingData = []
#------------------------------------------------------------------------------
class ReadSDO:
"Read Dictionary Object via CANopen Service Data Objects (SDO) - iteraction implemenation"
def __init__( self, nodeID, objectIndex, subIndex ):
self.nodeID, self.objectIndex, self.subIndex = nodeID, objectIndex, subIndex
self.result = None
self.packet = None
def update( self, packet ):
"update data from can - buffer for 1 packet only!"
self.packet = packet
def generator( self ):
yield ( 0x600 + self.nodeID, [0x40, self.objectIndex&0xFF, self.objectIndex>>8, self.subIndex, 0,0,0,0] )
toggleBit = 0x00
wholeData = []
while True:
while self.packet is None:
yield None
id,data = self.packet
self.packet = None
if id == 0x580 + self.nodeID:
assert( len(data) == 8 )
if data[0] == 0x80: # Abort
printAbort( data[7], data[6], (data[5]<<8)|data[4] )
self.result = [] # throw exception?
return
if (data[0] & 0xF3) == 0x43: # expedited transfer
assert( ((data[2]<<8)|data[1]) == self.objectIndex )
assert( data[3] == self.subIndex )
num = 4 - ((data[0]>>2) & 0x3)
self.result = data[4:4+num]
return
# multi packet transfer (segmented)
if data[0] == 0x41 : # init
assert( ((data[2]<<8)|data[1]) == self.objectIndex )
assert( data[3] == self.subIndex )
totalLength = data[4] + (data[5]<<8) + (data[6]<<16) + (data[7]<<24)
else:
assert( (data[0] & 0xF0) == 0x00 | toggleBit )
toggleBit = 0x10 - toggleBit
if data[0] & 0x01: # last segment
num = 7 - ((data[0]>>1) & 0x7 )
wholeData.extend( data[1:1+num] )
self.result = wholeData
return
else:
wholeData.extend( data[1:] )
yield ( 0x600 + self.nodeID, [0x60|toggleBit, self.objectIndex&0xFF, self.objectIndex>>8, self.subIndex, 0,0,0,0] )
class WriteSDO:
"Write Dictionary Object via CANopen Service Data Objects (SDO)"
def __init__( self, nodeID, objectIndex, subIndex, data ):
self.nodeID, self.objectIndex, self.subIndex = nodeID, objectIndex, subIndex
self.data = data
def update( self, packet ):
"update data from can - buffer for 1 packet only!"
self.packet = packet
def generator( self ):
remainingData = []
if len(self.data) <= 4:
# expedited transfer
cmd = 0x23 | ((4-len(self.data))<<2)
d = self.data
d.extend( [0,0,0,0] )
yield ( 0x600 + self.nodeID, [cmd, self.objectIndex&0xFF, self.objectIndex>>8, self.subIndex, d[0],d[1],d[2],d[3]] )
else:
cmd = 0x21
l = len(self.data)
yield ( 0x600 + self.nodeID, [cmd, self.objectIndex&0xFF, self.objectIndex>>8, self.subIndex, l&0xFF, (l>>8)&0xFF, (l>>16)&0xFF, (l>>24)&0xFF] )
remainingData = self.data
toggleBit = 0x00
while True:
while self.packet is None:
yield None
id,data = self.packet
self.packet = None
# self.can.printPacket( id, data )
if id == 0x580 + self.nodeID:
assert( len(data) == 8 )
if data[0] == 0x80: # Abort
printAbort( data[7], data[6], (data[5]<<8)|data[4] )
return # throw exception?
sendNextData = False
if data[0] == 0x60: # both expedited & segmented transfer
assert( ((data[2]<<8)|data[1]) == self.objectIndex )
assert( data[3] == self.subIndex )
sendNextData = True
if data[0] == 0x20 or data[0] == 0x30:
assert( data[0] & 0x10 == toggleBit )
toggleBit = 0x10 - toggleBit
sendNextData = True
if sendNextData:
if len(remainingData) == 0:
return
# next 7 bytes
if len(remainingData) > 7:
cmd = 0x00 | toggleBit
yield ( 0x600 + self.nodeID, [cmd] + remainingData[0:7] )
remainingData = remainingData[7:]
else:
cmd = 0x01 | toggleBit | ((7-len(remainingData))<<1) # set last segment
d = remainingData
d.extend( [0,0,0,0,0,0,0] )
yield ( 0x600 + self.nodeID, [cmd] + d[0:7] )
remainingData = []
def testNewSDO_0( filename ):
from can import ReplyLog
can=CAN(ReplyLog(filename))
can.resetModules()
sdoplg = SDOPlg( can )
( nodeID, dataIndex, subIndex ) = 10, 16*256+10, 0
data = sdoplg.readSDO( nodeID, dataIndex, subIndex )
print "RESUT DATA:", data
def testNewSDO( filename ):
from can import ReplyLog
can=CAN(ReplyLog(filename))
can.resetModules()
( nodeID, dataIndex, subIndex ) = 10, 16*256+10, 0
reader = ReadSDO( nodeID, dataIndex, subIndex )
for packet in reader.generator():
if packet != None:
can.sendData( *packet )
reader.update( can.readPacket() )
print "RESUT DATA:", reader.result
#------------------------------------------------------------------------------
def detectI2CModules( com, nodeID ):
modules = []
for addr in range(128):
writeSDO( com, nodeID, 0x2101, 0, [addr] )
data = readSDO( com, nodeID, 0x2100, 0 )
if data:
modules.append( addr )
return modules
def processSDO( tripple, operation, dataToSend ):
print tripple, operation, len(dataToSend)
sdoplg = SDOPlg()
# print "I2C Modules:", detectI2CModules( com, 7 )
print "TestSDO", tripple
nodeID, dataIndex, subIndex = tripple
if operation == "read":
data = sdoplg.readSDO( nodeID, dataIndex, subIndex )
return data
assert( operation == "write" )
sdoplg.writeSDO( nodeID, dataIndex, subIndex, dataToSend )
def usage():
print __doc__
if __name__ == "__main__":
if len(sys.argv) < 2:
usage()
sys.exit(2)
assert( len( sys.argv[1].split(":") ) == 3 )
tripple = tuple( [int(x,0) for x in sys.argv[1].split(":")] )
options = []
if len(sys.argv) > 2 and sys.argv[2][0] == "-":
options = [ x for x in sys.argv[2][1:] ]
operation = "read"
if "w" in options:
operation = "write"
filename = ""
if "f" in options:
assert( len(sys.argv) > 2 and sys.argv[-1][0] != "-" )
filename = sys.argv[-1]
data = []
if operation == "write":
if filename:
file = open( filename, "rb" )
str = file.read()
data = [ord(x) for x in str]
file.close()
if "s" in options:
assert( len(sys.argv) > 3 )
data = [ ord(x) for x in sys.argv[3] ]
if "b" in options:
assert( len(sys.argv) > 3 )
assert( filename == "" )
data = [ int(x,0) for x in sys.argv[3:] ]
assert( operation == "read" or len(data) > 0 )
# print sys.argv, tripple, operation, filename, options, data
data = processSDO( tripple, operation, data )
if operation == "read":
if filename:
file = open( filename, "wb" )
file.write( "".join([chr(x) for x in data]) )
file.close()
if "s" in options:
print "".join([chr(x) for x in data])
elif "h" in options:
print " ".join([hex(x) for x in data])
else:
print data