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
ed314446
Commit
ed314446
authored
Jul 31, 2020
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First working relay1..4 implementation over modbus.
parent
b48e5fa2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
35 deletions
+34
-35
examples/modbus/modbus-server.py
examples/modbus/modbus-server.py
+34
-35
No files found.
examples/modbus/modbus-server.py
View file @
ed314446
...
...
@@ -36,10 +36,7 @@ class Lime2MODIOI2c:
"""
Class to communication from Lime2 -> MOD-IO (over I2c)
XXX: keep relay state internally as take into account to preserver relay state!
ba = bitarray.bitarray([0, 0, 0, 0, 1, 1, 0, 1])
mod_io.write(ba2int(ba)) # all relays off
Keep relay state internally as take into account to preserver relay state!
"""
# state of 4 relays of mod-io, by default OFF
...
...
@@ -63,10 +60,9 @@ class Lime2MODIOI2c:
"""
i2c
.
close
()
def
write
(
self
,
code
):
"""
Write / send
frommnad
to device.
Write / send to device.
"""
self
.
open
()
while
1
:
...
...
@@ -74,21 +70,23 @@ class Lime2MODIOI2c:
i2c
.
write
([
0x10
,
code
])
break
except
:
print
(
"Failed to sent command
.
: %s"
%
code
)
print
(
"Failed to sent command: %s"
%
code
)
# be a good citizen and close
self
.
close
()
def
setRelayState
(
relay_number
,
state
):
def
setRelayState
(
self
,
relay_number
,
relay_
state
):
"""
Set relay state.
relay_number: 0 - 3
state: 0 (off), 1 (on)
relay_
state: 0 (off), 1 (on)
"""
if
relay_state
not
in
(
0
,
1
):
raise
ValueError
(
"Incorect relay state!"
)
if
state
not
in
(
0
,
1
,
2
,
3
):
if
relay_number
not
in
(
0
,
1
,
2
,
3
):
raise
ValueError
(
"Incorrect relay number!"
)
self
.
relay_state_list
[
relay_number
+
4
]
=
state
# relay1 is the most right element in list / byte command representation
self
.
relay_state_list
[
-
(
1
+
relay_number
)]
=
relay_state
# generate proper relay state command of all 4 relays
# which doesn't override any other relays' state
...
...
@@ -96,6 +94,23 @@ class Lime2MODIOI2c:
ba
=
bitarray
.
bitarray
(
self
.
relay_state_list
)
self
.
write
(
ba2int
(
ba
))
def
setRelayStateAllOff
(
self
):
"""
Switch off all relays.
"""
self
.
relay_state_list
=
[
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
]
ba
=
bitarray
.
bitarray
(
self
.
relay_state_list
)
self
.
write
(
ba2int
(
ba
))
def
setRelayStateAllOn
(
self
):
"""
Switch on all relays.
"""
self
.
relay_state_list
=
[
0
,
0
,
0
,
0
,
1
,
1
,
1
,
1
]
ba
=
bitarray
.
bitarray
(
self
.
relay_state_list
)
self
.
write
(
ba2int
(
ba
))
# --------------------------------------------------------------------------- #
# configure the service logging
...
...
@@ -108,11 +123,12 @@ log = logging.getLogger()
log
.
setLevel
(
logging
.
DEBUG
)
# main class to communicate with MOD-IO
mod_io
=
Lime2MODIOI2c
()
class
LimeModbusSlaveContext
(
ModbusSlaveContext
):
"""
XXX:
Control Lime2 -> Mod-io ->relay {1..4}
Control Lime2 -> Mod-io ->relay {1..4}
"""
def
setValues
(
self
,
fx
,
address
,
values
):
...
...
@@ -126,24 +142,8 @@ class LimeModbusSlaveContext(ModbusSlaveContext):
log
.
debug
(
address
)
log
.
debug
(
value
)
# XXX: use setRelayState
# relay 1
if
int
(
address
)
==
0
:
if
value
==
1
:
mod_io
.
write
(
0x01
)
elif
value
==
0
:
mod_io
.
write
(
0x00
)
# relay 2
if
int
(
address
)
==
1
:
# relay1
if
value
==
1
:
# on
mod_io
.
write
(
0x03
)
elif
value
==
0
:
mod_io
.
write
(
0x00
)
# switch relays at MOD-IO
mod_io
.
setRelayState
(
address
,
value
)
def
run_async_server
():
store
=
LimeModbusSlaveContext
(
...
...
@@ -175,11 +175,10 @@ def run_async_server():
if
__name__
==
"__main__"
:
# switch OFF all relays
ba
=
bitarray
.
bitarray
([
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
])
mod_io
.
write
(
ba2int
(
ba
))
mod_io
.
setRelayStateAllOff
()
# run modbus "server"
run_async_server
()
# switch off all
mod_io
.
write
(
0x00
)
mod_io
.
setRelayStateAllOff
()
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