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
|
#!/usr/bin/env python
"""
Pymodbus Synchronous Client Extended Examples
--------------------------------------------------------------------------
The following is an example of how to use the synchronous modbus client
implementation from pymodbus to perform the extended portions of the
modbus protocol.
"""
# --------------------------------------------------------------------------- #
# import the various server implementations
# --------------------------------------------------------------------------- #
# from pymodbus.client.sync import ModbusTcpClient as ModbusClient
# from pymodbus.client.sync import ModbusUdpClient as ModbusClient
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
# --------------------------------------------------------------------------- #
# import the extended messages to perform
# --------------------------------------------------------------------------- #
from pymodbus.diag_message import *
from pymodbus.file_message import *
from pymodbus.other_message import *
from pymodbus.mei_message import *
# --------------------------------------------------------------------------- #
# configure the client logging
# --------------------------------------------------------------------------- #
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)
UNIT = 0x01
def execute_extended_requests():
# ------------------------------------------------------------------------#
# choose the client you want
# ------------------------------------------------------------------------#
# make sure to start an implementation to hit against. For this
# you can use an existing device, the reference implementation in the tools
# directory, or start a pymodbus server.
#
# It should be noted that you can supply an ipv4 or an ipv6 host address
# for both the UDP and TCP clients.
# ------------------------------------------------------------------------#
client = ModbusClient(method='rtu', port="/dev/ptyp0")
# client = ModbusClient(method='ascii', port="/dev/ptyp0")
# client = ModbusClient(method='binary', port="/dev/ptyp0")
# client = ModbusClient('127.0.0.1', port=5020)
# from pymodbus.transaction import ModbusRtuFramer
# client = ModbusClient('127.0.0.1', port=5020, framer=ModbusRtuFramer)
client.connect()
# ----------------------------------------------------------------------- #
# extra requests
# ----------------------------------------------------------------------- #
# If you are performing a request that is not available in the client
# mixin, you have to perform the request like this instead::
#
# from pymodbus.diag_message import ClearCountersRequest
# from pymodbus.diag_message import ClearCountersResponse
#
# request = ClearCountersRequest()
# response = client.execute(request)
# if isinstance(response, ClearCountersResponse):
# ... do something with the response
#
#
# What follows is a listing of all the supported methods. Feel free to
# comment, uncomment, or modify each result set to match with your ref.
# ----------------------------------------------------------------------- #
# ----------------------------------------------------------------------- #
# information requests
# ----------------------------------------------------------------------- #
log.debug("Running ReadDeviceInformationRequest")
rq = ReadDeviceInformationRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
# assert (not rr.isError()) # test that we are not an error
# assert (rr.information[0] == b'Pymodbus') # test the vendor name
# assert (rr.information[1] == b'PM') # test the product code
# assert (rr.information[2] == b'1.0') # test the code revision
log.debug("Running ReportSlaveIdRequest")
rq = ReportSlaveIdRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
# assert(not rr.isError()) # test that we are not an error
# assert(rr.identifier == 0x00) # test the slave identifier
# assert(rr.status == 0x00) # test that the status is ok
log.debug("Running ReadExceptionStatusRequest")
rq = ReadExceptionStatusRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
# assert(not rr.isError()) # test that we are not an error
# assert(rr.status == 0x55) # test the status code
log.debug("Running GetCommEventCounterRequest")
rq = GetCommEventCounterRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
# assert(not rr.isError()) # test that we are not an error
# assert(rr.status == True) # test the status code
# assert(rr.count == 0x00) # test the status code
log.debug("Running GetCommEventLogRequest")
rq = GetCommEventLogRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
# assert(not rr.isError()) # test that we are not an error
# assert(rr.status == True) # test the status code
# assert(rr.event_count == 0x00) # test the number of events
# assert(rr.message_count == 0x00) # test the number of messages
# assert(len(rr.events) == 0x00) # test the number of events
# ------------------------------------------------------------------------#
# diagnostic requests
# ------------------------------------------------------------------------#
log.debug("Running ReturnQueryDataRequest")
rq = ReturnQueryDataRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
# assert(rr.message[0] == 0x0000) # test the resulting message
log.debug("Running RestartCommunicationsOptionRequest")
rq = RestartCommunicationsOptionRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
# assert(rr.message == 0x0000) # test the resulting message
log.debug("Running ReturnDiagnosticRegisterRequest")
rq = ReturnDiagnosticRegisterRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ChangeAsciiInputDelimiterRequest")
rq = ChangeAsciiInputDelimiterRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ForceListenOnlyModeRequest")
rq = ForceListenOnlyModeRequest(unit=UNIT)
rr = client.execute(rq) # does not send a response
log.debug(rr)
log.debug("Running ClearCountersRequest")
rq = ClearCountersRequest()
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ReturnBusCommunicationErrorCountRequest")
rq = ReturnBusCommunicationErrorCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ReturnBusExceptionErrorCountRequest")
rq = ReturnBusExceptionErrorCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ReturnSlaveMessageCountRequest")
rq = ReturnSlaveMessageCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ReturnSlaveNoResponseCountRequest")
rq = ReturnSlaveNoResponseCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ReturnSlaveNAKCountRequest")
rq = ReturnSlaveNAKCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ReturnSlaveBusyCountRequest")
rq = ReturnSlaveBusyCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ReturnSlaveBusCharacterOverrunCountRequest")
rq = ReturnSlaveBusCharacterOverrunCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ReturnIopOverrunCountRequest")
rq = ReturnIopOverrunCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running ClearOverrunCountRequest")
rq = ClearOverrunCountRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
log.debug("Running GetClearModbusPlusRequest")
rq = GetClearModbusPlusRequest(unit=UNIT)
rr = client.execute(rq)
log.debug(rr)
# assert(rr == None) # not supported by reference
# ------------------------------------------------------------------------#
# close the client
# ------------------------------------------------------------------------#
client.close()
if __name__ == "__main__":
execute_extended_requests()
|