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
Martin Manchev
osie
Commits
af7d9112
Commit
af7d9112
authored
Jul 17, 2020
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Firts modbus server for lime2.
parent
5299d8de
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
93 deletions
+7
-93
examples/modbus/modbus-server.py
examples/modbus/modbus-server.py
+7
-93
No files found.
examples/modbus/modbus-server.py
View file @
af7d9112
...
...
@@ -34,65 +34,11 @@ log.setLevel(logging.DEBUG)
def
run_async_server
():
# ----------------------------------------------------------------------- #
# initialize your data store
# ----------------------------------------------------------------------- #
# The datastores only respond to the addresses that they are initialized to
# Therefore, if you initialize a DataBlock to addresses from 0x00 to 0xFF,
# a request to 0x100 will respond with an invalid address exception.
# This is because many devices exhibit this kind of behavior (but not all)
#
# block = ModbusSequentialDataBlock(0x00, [0]*0xff)
#
# Continuing, you can choose to use a sequential or a sparse DataBlock in
# your data context. The difference is that the sequential has no gaps in
# the data while the sparse can. Once again, there are devices that exhibit
# both forms of behavior::
#
# block = ModbusSparseDataBlock({0x00: 0, 0x05: 1})
# block = ModbusSequentialDataBlock(0x00, [0]*5)
#
# Alternately, you can use the factory methods to initialize the DataBlocks
# or simply do not pass them to have them initialized to 0x00 on the full
# address range::
#
# store = ModbusSlaveContext(di = ModbusSequentialDataBlock.create())
# store = ModbusSlaveContext()
#
# Finally, you are allowed to use the same DataBlock reference for every
# table or you you may use a seperate DataBlock for each table.
# This depends if you would like functions to be able to access and modify
# the same data or not::
#
# block = ModbusSequentialDataBlock(0x00, [0]*0xff)
# store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)
#
# The server then makes use of a server context that allows the server to
# respond with different slave contexts for different unit ids. By default
# it will return the same context for every unit id supplied (broadcast
# mode).
# However, this can be overloaded by setting the single flag to False
# and then supplying a dictionary of unit id to context mapping::
#
# slaves = {
# 0x01: ModbusSlaveContext(...),
# 0x02: ModbusSlaveContext(...),
# 0x03: ModbusSlaveContext(...),
# }
# context = ModbusServerContext(slaves=slaves, single=False)
#
# The slave context can also be initialized in zero_mode which means that a
# request to address(0-7) will map to the address (0-7). The default is
# False which is based on section 4.4 of the specification, so address(0-7)
# will map to (1-8)::
#
# store = ModbusSlaveContext(..., zero_mode=True)
# ----------------------------------------------------------------------- #
store
=
ModbusSlaveContext
(
di
=
ModbusSequentialDataBlock
(
0
,
[
17
]
*
10
0
),
co
=
ModbusSequentialDataBlock
(
0
,
[
17
]
*
10
0
),
hr
=
ModbusSequentialDataBlock
(
0
,
[
17
]
*
10
0
),
ir
=
ModbusSequentialDataBlock
(
0
,
[
17
]
*
10
0
))
di
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
1
0
),
co
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
1
0
),
hr
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
1
0
),
ir
=
ModbusSequentialDataBlock
(
0
,
[
0
]
*
1
0
))
store
.
register
(
CustomModbusRequest
.
function_code
,
'cm'
,
ModbusSequentialDataBlock
(
0
,
[
17
]
*
100
))
context
=
ModbusServerContext
(
slaves
=
store
,
single
=
True
)
...
...
@@ -103,49 +49,17 @@ def run_async_server():
# If you don't set this or any fields, they are defaulted to empty strings.
# ----------------------------------------------------------------------- #
identity
=
ModbusDeviceIdentification
()
identity
.
VendorName
=
'
Pymodbus
'
identity
.
ProductCode
=
'
PM
'
identity
.
VendorName
=
'
Nexedi
'
identity
.
ProductCode
=
'
Lime2.PLC
'
identity
.
VendorUrl
=
'http://github.com/bashwork/pymodbus/'
identity
.
ProductName
=
'Pymodbus Server'
identity
.
ModelName
=
'Pymodbus Server'
identity
.
MajorMinorRevision
=
'2.3.0'
# ----------------------------------------------------------------------- #
# run the server you want
# ----------------------------------------------------------------------- #
identity
.
MajorMinorRevision
=
'0.0.1'
# TCP Server
StartTcpServer
(
context
,
identity
=
identity
,
address
=
(
"localhost"
,
502
),
custom_functions
=
[
CustomModbusRequest
])
# TCP Server with deferred reactor run
# from twisted.internet import reactor
# StartTcpServer(context, identity=identity, address=("localhost", 5020),
# defer_reactor_run=True)
# reactor.run()
# Server with RTU framer
# StartTcpServer(context, identity=identity, address=("localhost", 5020),
# framer=ModbusRtuFramer)
# UDP Server
# StartUdpServer(context, identity=identity, address=("127.0.0.1", 5020))
# RTU Server
# StartSerialServer(context, identity=identity,
# port='/dev/ttyp0', framer=ModbusRtuFramer)
# ASCII Server
# StartSerialServer(context, identity=identity,
# port='/dev/ttyp0', framer=ModbusAsciiFramer)
# Binary Server
# StartSerialServer(context, identity=identity,
# port='/dev/ttyp0', framer=ModbusBinaryFramer)
if
__name__
==
"__main__"
:
run_async_server
()
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