Skip to content

Instantly share code, notes, and snippets.

@boseji
Last active September 15, 2024 06:54
Show Gist options
  • Select an option

  • Save boseji/316bb5f330f1e35201a78fa716cebaf2 to your computer and use it in GitHub Desktop.

Select an option

Save boseji/316bb5f330f1e35201a78fa716cebaf2 to your computer and use it in GitHub Desktop.

Revisions

  1. boseji revised this gist Sep 15, 2024. No changes.
  2. boseji revised this gist Sep 15, 2024. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    # Python MODBUS/IP Server and Client

    This example builds upon the umodbus library.

    PyPi <https://pypi.org/project/uModbus/>

    Sources <https://github.com/AdvancedClimateSystems/uModbus/>

    Documents <https://umodbus.readthedocs.io/en/latest/>

    ## License

    `SPDX: MIT`

    MIT License

    Copyright (C) 2024 Abhijit Bose (aka. Boseji)

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3. boseji revised this gist Sep 15, 2024. 2 changed files with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion .gitignore
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    *venv*
    File renamed without changes.
  4. boseji revised this gist Sep 15, 2024. 6 changed files with 100 additions and 0 deletions.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    *venv*
    5 changes: 5 additions & 0 deletions clear-env.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    #!/usr/bin/env bash
    # Copyright (C) 2024 Abhijit Bose (aka. Boseji)
    # SPDX: MIT

    rm -rf venv
    37 changes: 37 additions & 0 deletions client.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/usr/bin/env python
    # Copyright (C) 2024 Abhijit Bose (aka. Boseji)
    # SPDX: MIT

    import socket

    from umodbus import conf
    from umodbus.client import tcp

    # Enable values to be signed (default is False).
    conf.SIGNED_VALUES = True
    # Server Detail
    svr = ('localhost', 50222)
    # Create the Socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print("\n Connecting to", svr)
    sock.connect(svr)

    # Returns a message or Application Data Unit (ADU) specific for doing
    # Modbus TCP/IP.
    message = tcp.write_single_register(
    slave_id=1,
    address=1,
    value=23,
    )
    # Return a Write Registers
    # message = tcp.write_multiple_registers(
    # slave_id=1,
    # starting_address=2,
    # values=[14, 13, 67],
    # )

    # 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()
    2 changes: 2 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    pyserial==3.5
    uModbus==1.0.4
    48 changes: 48 additions & 0 deletions server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/usr/bin/env python
    # Copyright (C) 2024 Abhijit Bose (aka. Boseji)
    # SPDX: MIT
    import logging
    from socketserver import TCPServer
    from collections import defaultdict

    from umodbus import conf
    from umodbus.server.tcp import RequestHandler, get_server
    from umodbus.utils import log_to_stream

    # Add stream handler to logger 'uModbus'.
    log_to_stream(level=logging.DEBUG)
    # Server Detail
    svr = ('localhost', 50222)

    # A very simple data store which maps address against their values.
    data_store = defaultdict(int)

    # Enable values to be signed (default is False).
    conf.SIGNED_VALUES = True

    TCPServer.allow_reuse_address = True
    # Open Socket
    app = get_server(TCPServer, svr, RequestHandler)


    @app.route(slave_ids=[1], function_codes=[3, 4], addresses=list(range(0, 10)))
    def read_data_store(slave_id, function_code, address):
    """" Return value of address. """
    print("Fetch ADDR:", address)
    return data_store[address]


    @app.route(slave_ids=[1], function_codes=[6, 16], addresses=list(range(0, 10)))
    def write_data_store(slave_id, function_code, address, value):
    """" Set value for address. """
    print("Store ADDR:", address, " Data:", value)
    data_store[address] = value

    if __name__ == '__main__':
    print("\n Starting Sever ", svr)
    try:
    app.serve_forever()
    finally:
    print("\n\n Closing down server...\n")
    app.shutdown()
    app.server_close()
    7 changes: 7 additions & 0 deletions setup-env.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #!/usr/bin/env bash
    # Copyright (C) 2024 Abhijit Bose (aka. Boseji)
    # SPDX: MIT

    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  5. boseji created this gist Sep 15, 2024.
    31 changes: 31 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    # Python MODBUS/IP Server and Client

    This example builds upon the `umodbus` library.

    PyPi <https://pypi.org/project/uModbus/>

    Sources <https://github.com/AdvancedClimateSystems/uModbus/>

    Documents <https://umodbus.readthedocs.io/en/latest/>

    ## License

    MIT License

    Copyright (C) 2024 Abhijit Bose (aka. Boseji)

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
    documentation files (the "Software"), to deal in the Software without restriction, including without
    limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
    following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial
    portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
    NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
    THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.