Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
osie
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nikola Balog
osie
Commits
110e28a8
Commit
110e28a8
authored
Jul 21, 2020
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
On / off relay1.
parent
9f42b593
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
15 deletions
+28
-15
examples/modbus/modbus-server.py
examples/modbus/modbus-server.py
+28
-15
No files found.
examples/modbus/modbus-server.py
View file @
110e28a8
#!/usr/bin/env python
"""
Pymodbus Asynchronous Server Example
--------------------------------------------------------------------------
Modbus implementation for Lime2 -> MOD-IO which controls relays.
root@a20:~/osie/examples/modbus# pymodbus.console tcp --host localhost --port 502
To switch on relay 1:
> client.write_registers address=0 values=1 unit=1
To switch OFF:
> client.write_registers address=0 values=0 unit=1
The asynchronous server is a high performance implementation using the
twisted library as its backend. This allows it to scale to many thousands
of nodes which can be helpful for testing monitoring software.
"""
# --------------------------------------------------------------------------- #
# import the various server implementations
# --------------------------------------------------------------------------- #
from
pymodbus.server.asynchronous
import
StartTcpServer
from
pymodbus.server.asynchronous
import
StartUdpServer
from
pymodbus.server.asynchronous
import
StartSerialServer
...
...
@@ -23,6 +26,7 @@ from pymodbus.transaction import (ModbusRtuFramer,
from
custom_message
import
CustomModbusRequest
from
pyA20Lime2
import
i2c
# XXX: create Lime2MODIOCommunicator class that encapcuslates I2C com
def
sendI2Ccommand
(
code
):
# init I2C
i2c
.
init
(
"/dev/i2c-1"
)
...
...
@@ -50,26 +54,35 @@ log.setLevel(logging.DEBUG)
sendI2Ccommand
(
0x00
)
# all relays off
class
Ivan
ModbusSlaveContext
(
ModbusSlaveContext
):
class
Lime
ModbusSlaveContext
(
ModbusSlaveContext
):
"""
XXX:
XXX:
Control Lime2 -> Mod-io ->relay {1..4}
"""
def
setValues
(
self
,
fx
,
address
,
values
):
if
not
self
.
zero_mode
:
address
=
address
+
1
log
.
debug
(
"setValues
1111[%d] %d:%d"
%
(
fx
,
address
,
len
(
values
)
))
log
.
debug
(
"setValues
[%d] %d:%s"
%
(
fx
,
address
,
values
))
self
.
store
[
self
.
decode
(
fx
)].
setValues
(
address
,
values
)
# XXX:
sendI2Ccommand
(
0x01
)
# control relays
value
=
int
(
values
[
0
])
log
.
debug
(
address
)
log
.
debug
(
value
)
if
int
(
address
)
==
0
:
# relay1
if
value
==
1
:
# on
sendI2Ccommand
(
0x01
)
elif
value
==
0
:
sendI2Ccommand
(
0x00
)
def
run_async_server
():
store
=
Ivan
ModbusSlaveContext
(
store
=
Lime
ModbusSlaveContext
(
di
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
10
),
co
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
10
),
hr
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
10
),
ir
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
10
))
ir
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
10
)
,
zero_mode
=
True
)
store
.
register
(
CustomModbusRequest
.
function_code
,
'cm'
,
ModbusSequentialDataBlock
(
0
,
[
17
]
*
100
))
context
=
ModbusServerContext
(
slaves
=
store
,
single
=
True
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment