Skip to content

Repository files navigation

https://coveralls.io/repos/AdvancedClimateSystems/uModbus/badge.svg?service=github

uModbus

uModbus or (μModbus) is a pure Python implementation of the Modbus protocol as described in the MODBUS Application Protocol Specification V1.1b3. uModbus implements both a Modbus client (both TCP and RTU) and a Modbus server (both TCP and RTU). The "u" or "μ" in the name comes from the the SI prefix "micro-". uModbus is very small and lightweight. The source can be found on GitHub. Documentation is available at Read the Docs.

Quickstart

Creating a Modbus TCP server is easy:

#!/usr/bin/env python# scripts/examples/simple_tcp_server.pyimportloggingfromsocketserverimportTCPServerfromcollectionsimportdefaultdictfromumodbusimportconffromumodbus.server.tcpimportRequestHandler, get_serverfromumodbus.utilsimportlog_to_stream# Add stream handler to logger 'uModbus'.log_to_stream(level=logging.DEBUG)
# A very simple data store which maps addresss against their values.data_store=defaultdict(int)
# Enable values to be signed (default is False).conf.SIGNED_VALUES=TrueTCPServer.allow_reuse_address=Trueapp=get_server(TCPServer, ('localhost', 502), RequestHandler)
@app.route(slave_ids=[1], function_codes=[3, 4], addresses=list(range(0, 10)))defread_data_store(slave_id, function_code, address):
"""" Return value of address. """returndata_store[address]
@app.route(slave_ids=[1], function_codes=[6, 16], addresses=list(range(0, 10)))defwrite_data_store(slave_id, function_code, address, value):
"""" Set value for address. """data_store[address] =valueif__name__=='__main__':
try:
app.serve_forever()
finally:
app.shutdown()
app.server_close()

Doing a Modbus request requires even less code:

#!/usr/bin/env python# scripts/examples/simple_tcp_client.pyimportsocketfromumodbusimportconffromumodbus.clientimporttcp# Enable values to be signed (default is False).conf.SIGNED_VALUES=Truesock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 502))
# Returns a message or Application Data Unit (ADU) specific for doing# Modbus TCP/IP.message=tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 1, 1])
# Response depends on Modbus function code. This particular returns the# amount of coils written, in this case it is.response=tcp.send_message(message, sock)
sock.close()

Features

The following Modbus functions have been implemented:

  • 01: Read Coils
  • 02: Read Discrete Inputs
  • 03: Read Holding Registers
  • 04: Read Input Registers
  • 05: Write Single Coil
  • 06: Write Single Register
  • 15: Write Multiple Coils
  • 16: Write Multiple Registers

Other featues:

  • Support for signed and unsigned register values.

Roadmap

uModbus is far from complete. The next, unordered list shows what is going to be implemented in the future:

  • Support for all Modbus functions
  • Other Modbus 'flavours', so uModbus is able to handle 32 bit values.

License

uModbus software is licensed under Mozilla Public License. © 2016 Advanced Climate Systems.

About

Python implementation of the Modbus protocol.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages