Commit 7867e377 authored by Ivan Tyagov's avatar Ivan Tyagov

First modbus controlled relay1 example.

parent af7d9112
......@@ -86,6 +86,7 @@ class CustomModbusRequest(ModbusRequest):
self.address, self.count = struct.unpack('>HH', data)
def execute(self, context):
print1("da"*80)
if not (1 <= self.count <= 0x7d0):
return self.doException(ModbusExceptions.IllegalValue)
if not context.validate(self.function_code, self.address, self.count):
......@@ -94,32 +95,3 @@ class CustomModbusRequest(ModbusRequest):
self.count)
return CustomModbusResponse(values)
# --------------------------------------------------------------------------- #
# This could also have been defined as
# --------------------------------------------------------------------------- #
class Read16CoilsRequest(ReadCoilsRequest):
def __init__(self, address, **kwargs):
""" Initializes a new instance
:param address: The address to start reading from
"""
ReadCoilsRequest.__init__(self, address, 16, **kwargs)
# --------------------------------------------------------------------------- #
# execute the request with your client
# --------------------------------------------------------------------------- #
# using the with context, the client will automatically be connected
# and closed when it leaves the current scope.
# --------------------------------------------------------------------------- #
if __name__ == "__main__":
with ModbusClient(host='localhost', port=5020) as client:
client.register(CustomModbusResponse)
request = CustomModbusRequest(1, unit=1)
result = client.execute(request)
print(result.values)
......@@ -21,6 +21,21 @@ from pymodbus.transaction import (ModbusRtuFramer,
ModbusAsciiFramer,
ModbusBinaryFramer)
from custom_message import CustomModbusRequest
from pyA20Lime2 import i2c
def sendI2Ccommand(code):
# init I2C
i2c.init("/dev/i2c-1")
i2c.open(0x58)
while 1:
try:
i2c.write([0x10, code])
break
except:
print("Failed co sent command.")
i2c.close()
# --------------------------------------------------------------------------- #
# configure the service logging
......@@ -32,9 +47,25 @@ logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)
sendI2Ccommand(0x00) # all relays off
class IvanModbusSlaveContext(ModbusSlaveContext):
"""
XXX:
"""
def setValues(self, fx, address, values):
if not self.zero_mode:
address = address + 1
log.debug("setValues1111[%d] %d:%d" % (fx, address, len(values)))
self.store[self.decode(fx)].setValues(address, values)
# XXX:
sendI2Ccommand(0x01)
def run_async_server():
store = ModbusSlaveContext(
store = IvanModbusSlaveContext(
di=ModbusSequentialDataBlock(0, [0]*10),
co=ModbusSequentialDataBlock(0, [0]*10),
hr=ModbusSequentialDataBlock(0, [0]*10),
......@@ -63,3 +94,6 @@ def run_async_server():
if __name__ == "__main__":
run_async_server()
# switch off all
sendI2Ccommand(0x00)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment