~ubuntu-branches/ubuntu/raring/pymodbus/raring-proposed

« back to all changes in this revision

Viewing changes to pymodbus/other_message.py

  • Committer: Package Import Robot
  • Author(s): W. Martin Borgert
  • Date: 2011-10-26 07:26:28 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111026072628-fvzyi6tnb8iipomp
Tags: 0.9.0+r175-1
* Update from trunk to get a number of upstream fixes.
* Removed examples/tools/ (not present in previous version
  anyway) from source because there are different licenses
  involved. Needs clarification.
* Dont't install unit tests.
* Debian patches not necessary anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from pymodbus.pdu import ModbusRequest
9
9
from pymodbus.pdu import ModbusResponse
10
10
from pymodbus.device import ModbusControlBlock
11
 
from pymodbus.exceptions import *
12
11
 
13
12
_MCB = ModbusControlBlock()
14
13
 
 
14
 
15
15
#---------------------------------------------------------------------------#
16
16
# TODO Make these only work on serial
17
17
#---------------------------------------------------------------------------#
57
57
        '''
58
58
        return "ReadExceptionStatusRequest(%d)" % (self.function_code)
59
59
 
 
60
 
60
61
class ReadExceptionStatusResponse(ModbusResponse):
61
62
    '''
62
63
    The normal response contains the status of the eight Exception Status
68
69
    function_code = 0x07
69
70
    _rtu_frame_size = 5
70
71
 
71
 
    def __init__(self, status):
 
72
    def __init__(self, status=0x00):
72
73
        ''' Initializes a new instance
73
74
 
74
75
        :param status: The status response to report
101
102
# Encapsulate interface transport 43, 14
102
103
# CANopen general reference 43, 13
103
104
 
 
105
 
104
106
#---------------------------------------------------------------------------#
105
107
# TODO Make these only work on serial
106
108
#---------------------------------------------------------------------------#
107
109
class GetCommEventCounterRequest(ModbusRequest):
108
110
    '''
109
 
    This function code is used to get a status word and an event count from the
110
 
    remote device's communication event counter. 
111
 
 
112
 
    By fetching the current count before and after a series of messages, a client
113
 
    can determine whether the messages were handled normally by the remote device. 
114
 
 
115
 
    The device's event counter is incremented once  for each successful message
116
 
    completion. It is not incremented for exception responses, poll commands,
117
 
    or fetch event counter commands. 
118
 
 
119
 
    The event counter can be reset by means of the Diagnostics function (code 08),
120
 
    with a subfunction of Restart Communications Option (code 00 01) or
121
 
    Clear Counters and Diagnostic Register (code 00 0A).
 
111
    This function code is used to get a status word and an event count from
 
112
    the remote device's communication event counter.
 
113
 
 
114
    By fetching the current count before and after a series of messages, a
 
115
    client can determine whether the messages were handled normally by the
 
116
    remote device.
 
117
 
 
118
    The device's event counter is incremented once  for each successful
 
119
    message completion. It is not incremented for exception responses,
 
120
    poll commands, or fetch event counter commands.
 
121
 
 
122
    The event counter can be reset by means of the Diagnostics function
 
123
    (code 08), with a subfunction of Restart Communications Option
 
124
    (code 00 01) or Clear Counters and Diagnostic Register (code 00 0A).
122
125
    '''
123
126
    function_code = 0x0b
124
127
    _rtu_frame_size = 4
155
158
        '''
156
159
        return "GetCommEventCounterRequest(%d)" % (self.function_code)
157
160
 
 
161
 
158
162
class GetCommEventCounterResponse(ModbusResponse):
159
163
    '''
160
164
    The normal response contains a two-byte status word, and a two-byte
161
165
    event count. The status word will be all ones (FF FF hex) if a
162
 
    previously-issued program command is still being processed by the remote
163
 
    device (a busy condition exists). Otherwise, the status word will be 
164
 
    all zeros.
 
166
    previously-issued program command is still being processed by the
 
167
    remote device (a busy condition exists). Otherwise, the status word
 
168
    will be all zeros.
165
169
    '''
166
170
    function_code = 0x0b
167
171
    _rtu_frame_size = 8
168
172
 
169
 
    def __init__(self, count):
 
173
    def __init__(self, count=0x0000):
170
174
        ''' Initializes a new instance
171
175
 
172
176
        :param count: The current event counter value
173
177
        '''
174
178
        ModbusResponse.__init__(self)
175
179
        self.count = count
176
 
        self.status = True # this means we are ready, not waiting
 
180
        self.status = True  # this means we are ready, not waiting
177
181
 
178
182
    def encode(self):
179
183
        ''' Encodes the response
180
184
 
181
185
        :returns: The byte encoded message
182
186
        '''
183
 
        ready = ModbusStatus.Ready if self.status else ModbusStatus.Waiting
 
187
        if self.status: ready = ModbusStatus.Ready 
 
188
        else: ready = ModbusStatus.Waiting
184
189
        return struct.pack('>HH', ready, self.count)
185
190
 
186
191
    def decode(self, data):
199
204
        arguments = (self.function_code, self.count, self.status)
200
205
        return "GetCommEventCounterResponse(%d, %d, %d)" % arguments
201
206
 
 
207
 
202
208
#---------------------------------------------------------------------------#
203
209
# TODO Make these only work on serial
204
210
#---------------------------------------------------------------------------#
205
211
class GetCommEventLogRequest(ModbusRequest):
206
212
    '''
207
 
    This function code is used to get a status word, event count, message count,
208
 
    and a field of event bytes from the remote device. 
 
213
    This function code is used to get a status word, event count, message
 
214
    count, and a field of event bytes from the remote device.
209
215
 
210
 
    The status word and event counts are identical  to that returned by the
211
 
    Get Communications Event Counter function (11, 0B hex). 
 
216
    The status word and event counts are identical  to that returned by
 
217
    the Get Communications Event Counter function (11, 0B hex).
212
218
 
213
219
    The message counter contains the quantity of  messages processed by the
214
 
    remote device since its last restart, clear counters operation, or power-up.
215
 
    This count is identical to that returned by the Diagnostic function
216
 
    (code 08), sub-function Return Bus Message Count (code 11, 0B hex). 
 
220
    remote device since its last restart, clear counters operation, or
 
221
    power-up.  This count is identical to that returned by the Diagnostic
 
222
    function (code 08), sub-function Return Bus Message Count (code 11,
 
223
    0B hex).
217
224
 
218
 
    The event bytes field contains 0-64 bytes, with each byte corresponding to
219
 
    the status of one MODBUS send or receive operation for the remote device.
220
 
    The remote device enters the events into the field in chronological order.
221
 
    Byte 0 is the most recent event. Each new byte flushes the oldest byte
222
 
    from the field.
 
225
    The event bytes field contains 0-64 bytes, with each byte corresponding
 
226
    to the status of one MODBUS send or receive operation for the remote
 
227
    device.  The remote device enters the events into the field in
 
228
    chronological order.  Byte 0 is the most recent event. Each new byte
 
229
    flushes the oldest byte from the field.
223
230
    '''
224
231
    function_code = 0x0c
225
232
    _rtu_frame_size = 4
261
268
        '''
262
269
        return "GetCommEventLogRequest(%d)" % self.function_code
263
270
 
 
271
 
264
272
class GetCommEventLogResponse(ModbusResponse):
265
273
    '''
266
274
    The normal response contains a two-byte status word field,
267
275
    a two-byte event count field, a two-byte message count field,
268
 
    and a field containing 0-64 bytes of events. A byte count field 
269
 
    defines the total length of the data in these four field
 
276
    and a field containing 0-64 bytes of events. A byte count
 
277
    field defines the total length of the data in these four field
270
278
    '''
271
279
    function_code = 0x0c
272
280
    _rtu_byte_count_pos = 3
290
298
 
291
299
        :returns: The byte encoded message
292
300
        '''
293
 
        ready = ModbusStatus.Ready if self.status else ModbusStatus.Waiting
 
301
        if self.status: ready = ModbusStatus.Ready 
 
302
        else: ready = ModbusStatus.Waiting
294
303
        packet  = struct.pack('>B', 6 + len(self.events))
295
304
        packet += struct.pack('>H', ready)
296
305
        packet += struct.pack('>HH', self.event_count, self.message_count)
309
318
        self.message_count = struct.unpack('>H', data[5:7])[0]
310
319
 
311
320
        self.events = []
312
 
        for e in xrange(7, length+1):
 
321
        for e in xrange(7, length + 1):
313
322
            self.events.append(struct.unpack('>B', data[e])[0])
314
323
 
315
324
    def __str__(self):
320
329
        arguments = (self.function_code, self.status, self.message_count, self.event_count)
321
330
        return "GetCommEventLogResponse(%d, %d, %d, %d)" % arguments
322
331
 
 
332
 
323
333
#---------------------------------------------------------------------------#
324
334
# TODO Make these only work on serial
325
335
#---------------------------------------------------------------------------#
326
336
class ReportSlaveIdRequest(ModbusRequest):
327
337
    '''
328
 
    This function code is used to read the description of the type, the current
329
 
    status, and other information specific to a remote device. 
 
338
    This function code is used to read the description of the type, the
 
339
    current status, and other information specific to a remote device.
330
340
    '''
331
341
    function_code = 0x11
332
342
    _rtu_frame_size = 4
363
373
        '''
364
374
        return "ResportSlaveIdRequest(%d)" % self.function_code
365
375
 
 
376
 
366
377
class ReportSlaveIdResponse(ModbusResponse):
367
378
    '''
368
 
    The format of a normal response is shown in the following example. The
369
 
    data contents are specific to each type of device.
 
379
    The format of a normal response is shown in the following example.
 
380
    The data contents are specific to each type of device.
370
381
    '''
371
382
    function_code = 0x11
372
383
    _rtu_byte_count_pos = 2
373
384
 
374
 
    def __init__(self, identifier, status=True):
 
385
    def __init__(self, identifier=0x00, status=True):
375
386
        ''' Initializes a new instance
376
387
 
377
388
        :param identifier: The identifier of the slave
386
397
 
387
398
        :returns: The byte encoded message
388
399
        '''
389
 
        status = ModbusStatus.SlaveOn if self.status else ModbusStatus.SlaveOff
 
400
        if self.status: status = ModbusStatus.SlaveOn
 
401
        else: status = ModbusStatus.SlaveOff
390
402
        length = len(self.identifier) + 2
391
403
        packet = struct.pack('>B', length)
392
 
        packet += self.identifier # we assume it is already encoded
393
 
        packet += struct.pack('>B', self.status)
 
404
        packet += self.identifier  # we assume it is already encoded
 
405
        packet += struct.pack('>B', status)
394
406
        return packet
395
407
 
396
408
    def decode(self, data):
402
414
        :param data: The packet data to decode
403
415
        '''
404
416
        length = struct.unpack('>B', data[0])[0]
405
 
        self.identifier = data[1:length-1]
 
417
        self.identifier = data[1:length + 1]
406
418
        status = struct.unpack('>B', data[-1])[0]
407
419
        self.status = status == ModbusStatus.SlaveOn
408
420
 
419
431
#---------------------------------------------------------------------------#
420
432
# report device identification 43, 14
421
433
 
422
 
#---------------------------------------------------------------------------# 
 
434
#---------------------------------------------------------------------------#
423
435
# Exported symbols
424
 
#---------------------------------------------------------------------------# 
 
436
#---------------------------------------------------------------------------#
425
437
__all__ = [
426
438
    "ReadExceptionStatusRequest", "ReadExceptionStatusResponse",
427
439
    "GetCommEventCounterRequest", "GetCommEventCounterResponse",