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
nexedi
osie
Commits
7867e377
Commit
7867e377
authored
Jul 21, 2020
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First modbus controlled relay1 example.
parent
af7d9112
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
30 deletions
+36
-30
examples/modbus/custom_message.py
examples/modbus/custom_message.py
+1
-29
examples/modbus/modbus-server.py
examples/modbus/modbus-server.py
+35
-1
No files found.
examples/modbus/custom_message.py
View file @
7867e377
...
...
@@ -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
)
examples/modbus/modbus-server.py
View file @
7867e377
...
...
@@ -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
=
Ivan
ModbusSlaveContext
(
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
)
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