Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: JoelBender/modpypes
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.2.12
Choose a base ref
...
head repository: JoelBender/modpypes
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 4 commits
  • 3 files changed
  • 3 contributors

Commits on Aug 24, 2017

  1. wrong response type

    Joel Bender committed Aug 24, 2017
    Copy the full SHA
    f6e33c4 View commit details

Commits on Dec 26, 2022

  1. Bump wheel from 0.23.0 to 0.38.1

    Bumps [wheel](https://github.com/pypa/wheel) from 0.23.0 to 0.38.1.
    - [Release notes](https://github.com/pypa/wheel/releases)
    - [Changelog](https://github.com/pypa/wheel/blob/main/docs/news.rst)
    - [Commits](pypa/wheel@0.23.0...0.38.1)
    
    ---
    updated-dependencies:
    - dependency-name: wheel
      dependency-type: direct:production
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    dependabot[bot] authored Dec 26, 2022
    Copy the full SHA
    4ecb517 View commit details

Commits on Mar 4, 2023

  1. add sample code

    JoelBender committed Mar 4, 2023
    Copy the full SHA
    ba41fd3 View commit details
  2. Merge pull request #2 from JoelBender/dependabot/pip/wheel-0.38.1

    Bump wheel from 0.23.0 to 0.38.1
    JoelBender authored Mar 4, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    4cbac7c View commit details
Showing with 159 additions and 2 deletions.
  1. +1 −1 modpypes/server.py
  2. +1 −1 requirements.txt
  3. +157 −0 samples/read_input_register.py
2 changes: 1 addition & 1 deletion modpypes/server.py
Original file line number Diff line number Diff line change
@@ -196,7 +196,7 @@ def do_ReadInputRegistersRequest(self, req):

self.pull_registers(req.address, req.count)

return ReadMultipleRegistersResponse(self.registers[req.address:req.address+req.count])
return ReadInputRegistersResponse(self.registers[req.address:req.address+req.count])

#
# main
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
wheel==0.23.0
wheel==0.38.1
157 changes: 157 additions & 0 deletions samples/read_input_register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
"""
Read Input Registers
"""

import os

from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ArgumentParser

from bacpypes.core import run, deferred
from bacpypes.comm import Client, bind

from modpypes.pdu import (
ExceptionResponse,
ReadInputRegistersRequest,
ReadInputRegistersResponse,
ModbusStruct,
)
from modpypes.app import ModbusClient

# some debugging
_debug = 0
_log = ModuleLogger(globals())

# settings
CONNECT_TIMEOUT = int(os.getenv("CONNECT_TIMEOUT", 0)) or None
IDLE_TIMEOUT = int(os.getenv("IDLE_TIMEOUT", 0)) or None

#
# ReadClient
#


@bacpypes_debugging
class ReadClient(Client):

"""
Read Client
"""

def __init__(self):
if _debug:
ReadClient._debug("__init__")
Client.__init__(self)

def read_input_registers(self, addr, unit_id, register, count):
"""
Read an input register.
"""
if _debug:
ReadClient._debug(
"read_input_registers %r %r %r %r", addr, unit_id, register, count
)

# address might have a port
if ":" in addr:
addr, port = addr.split(":")
server_address = (addr, int(port))
else:
server_address = (addr, 502)

# build a request
req = ReadInputRegistersRequest(register - 1, count)

# set the destination
req.pduDestination = server_address
req.mpduUnitID = unit_id
if _debug:
ReadClient._debug(" - req: %r", req)

# send the request
self.request(req)

def confirmation(self, resp):
if _debug:
ReadClient._debug("confirmation %r", resp)

# read responses
if isinstance(resp, ExceptionResponse):
print(" ::= " + str(resp))

elif isinstance(resp, ReadInputRegistersResponse):
print(" ::= " + str(resp.registers))

for dtype, codec in ModbusStruct.items():
try:
value = codec.unpack(resp.registers)
print(" " + dtype + " ::= " + str(value))
except Exception as err:
if _debug:
ReadClient._debug("unpack exception %r: %r", codec, err)

else:
raise TypeError("unsupported response")


#
# main
#


def main():
# parse the command line arguments
parser = ArgumentParser(description=__doc__)

# connection timeout paramters
parser.add_argument(
"--connect-timeout",
nargs="?",
type=int,
help="idle connection timeout",
default=CONNECT_TIMEOUT,
)
parser.add_argument(
"--idle-timeout",
nargs="?",
type=int,
help="idle connection timeout",
default=IDLE_TIMEOUT,
)

# now parse the arguments
args = parser.parse_args()

if _debug:
_log.debug("initialization")
if _debug:
_log.debug(" - args: %r", args)

# make a controller
controller = ModbusClient(
connect_timeout=args.connect_timeout,
idle_timeout=args.idle_timeout,
)
if _debug:
_log.debug(" - controller: %r", controller)

# make a client
read_client = ReadClient()
if _debug:
_log.debug(" - read_client: %r", read_client)

# bind the client "on top" of the controller as a "stack"
bind(read_client, controller)

# read something when the stack is ready
deferred(read_client.read_input_registers, "10.0.1.70", 1, 1016, 1)

_log.debug("running")

run()

_log.debug("fini")


if __name__ == "__main__":
main()