-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for KMTronic usb relay controller #1568
Open
benjamin1313
wants to merge
3
commits into
labgrid-project:master
Choose a base branch
from
benjamin1313:kmtronic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
main: | ||
location: Work Desk | ||
KMTronicRelay: | ||
index: 3 | ||
ports: 8 | ||
match: | ||
ID_SERIAL_SHORT: 'AB0LBF2U' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
targets: | ||
main: | ||
resources: | ||
KMTronicRelay: | ||
index: 2 | ||
|
||
drivers: | ||
KMTronicRelayDriver: {} | ||
DigitalOutputPowerDriver: | ||
delay: 2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import attr | ||
|
||
from .common import Driver | ||
from ..factory import target_factory | ||
from ..step import step | ||
from ..protocol import DigitalOutputProtocol | ||
from ..util.agentwrapper import AgentWrapper | ||
from ..resource.remote import NetworkKMTronicRelay | ||
|
||
|
||
@target_factory.reg_driver | ||
@attr.s(eq=False) | ||
class KMTronicRelayDriver(Driver, DigitalOutputProtocol): | ||
bindings = { | ||
"relay": {"KMTronicRelay", "NetworkKMTronicRelay"}, | ||
} | ||
|
||
def __attrs_post_init__(self): | ||
super().__attrs_post_init__() | ||
self.wrapper = None | ||
|
||
def on_activate(self): | ||
if isinstance(self.relay, NetworkKMTronicRelay): | ||
host = self.relay.host | ||
else: | ||
host = None | ||
self.wrapper = AgentWrapper(host) | ||
self.proxy = self.wrapper.load('kmtronic_relay') | ||
|
||
def on_deactivate(self): | ||
self.wrapper.close() | ||
self.wrapper = None | ||
self.proxy = None | ||
|
||
@Driver.check_active | ||
@step(args=['status']) | ||
def set(self, status): | ||
self.proxy.set(self.relay.path, self.relay.index, status) | ||
|
||
@Driver.check_active | ||
@step(result=True) | ||
def get(self): | ||
status = self.proxy.get(self.relay.path, self.relay.index, self.relay.ports) | ||
return status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import serial | ||
|
||
class USBKMTronicRelay: | ||
def set_output(self, path, index, status): | ||
# second and third is index and status on/off | ||
# \xFF\x01\x00 = turn relay 1 off | ||
# \xFF\x01\x01 = turn relay 1 on | ||
cmd = bytes([255, index, int(status == True)]) | ||
with serial.Serial(path, 9600) as ser: | ||
ser.write(cmd) | ||
|
||
def get_output(self, path, index, ports): | ||
# \xFF\x01\x03 will read relay 1 status | ||
# \xFF\x09\x00 will read from all relays, only works on 4 and 8 relay controller | ||
cmd = bytes([255, index, 3]) | ||
if ports > 2: | ||
cmd = bytes([255, 9, 0]) | ||
with serial.Serial(path, 9600, timeout=10) as ser: | ||
ser.write(cmd) | ||
if ports > 2: | ||
data = ser.read(ports) | ||
if data[0] == 255: | ||
print("WARNING: Unexpected return value from KMTronic relay.") | ||
print("Make sure to configure ports correctly in your config.2") | ||
return -1 | ||
state = data[index-1] | ||
else: | ||
data = ser.read(3) | ||
if data[0] != 255: | ||
print("WARNING: Unexpected return value from KMTronic relay.") | ||
print("Make sure to configure ports correctly in your config.1") | ||
return -1 | ||
state = data[2] | ||
return state | ||
|
||
_relays = {} | ||
|
||
def _get_relay(path): | ||
if (path) not in _relays: | ||
_relays[(path)] = USBKMTronicRelay() | ||
return _relays[(path)] | ||
|
||
def handle_set(path, index, status): | ||
relay = _get_relay(path) | ||
relay.set_output(path, index, status) | ||
|
||
def handle_get(path, index, ports): | ||
relay = _get_relay(path) | ||
return relay.get_output(path, index, ports) | ||
|
||
methods = { | ||
"set": handle_set, | ||
"get": handle_get, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from labgrid.resource.udev import KMTronicRelay | ||
from labgrid.driver.kmtronicrelay import KMTronicRelayDriver | ||
|
||
|
||
def test_kmtronicrelay_resource(target): | ||
r = KMTronicRelay(target, name=None, match={"ID_SERIAL_SHORT": "AB0LBF2U"}, index=2) | ||
|
||
|
||
def test_kmtronicrelay_driver(target): | ||
r = KMTronicRelay(target, name=None, match={"ID_SERIAL_SHORT": "AB0LBF2U"}, index=2) | ||
d = KMTronicRelayDriver(target, name=None) | ||
target.activate(d) | ||
|
||
|
||
def test_kmtronicrelay_control(target): | ||
r = KMTronicRelay(target, name=None, match={"ID_SERIAL_SHORT": "AB0LBF2U"}, index=2, ports=4) | ||
d = KMTronicRelayDriver(target, name=None) | ||
target.activate(d) | ||
d.set(1) | ||
assert d.get() == 1 | ||
d.set(0) | ||
assert d.get() == 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It breaks the working with USB One Relay https://forums.raspberrypi.com/viewtopic.php?t=125621
I don't have four channel relay for testing, and link from PR description is broken, thus I don't know the commands.
If commands differ for one and four channels relays, I suggest to add branch:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like there has been made some changes on the site. Here is a new link https://kmtronic.com/product/2786/usb-relay-controller-four-channel-box.html
Originally i used
cmd = bytes([255, index, 3])
This is a single read command and only reads from the specified relay.This works on the 4 port kmtronic I have primarily been testing on. But when a client i'm making this for tried on a 8 port kmtronic this did not work. After writing to kmtronic we found the 8 port version does not support single reads. We where told a read all command could be used.
FF 09 00
can be used to get the state from all relays on the kmtronic,Returned as
xx xx xx xx xx xx xx xx
where 01 is on and 00 is off. Sadly it seems there are problems with the links to the docs where this is noted.Now i don't have a 1 or 2 port kmtronic to test on those. but my hope was the
FF 09 00
command to read from all would work on the 1 and 2 port as well. If you have a 1 port are you able to test this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My case is inverse, I only have one channel relay. And I couldn't work with it.
I tried to add the conditional branch from the previous comment, and it works!
FF 09 00
command is ignored, driver hangs on in reading state (there is no response from the device?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, there's not much to be done about that. Thanks for letting me know.
Do you know if the two port has support for reading from a single relay and or reading from all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, no, I don't know. The only thing that could have helped me, it's a page [1]. But it doesn't have any information about reading.
I would offer you to add extra branch and to print warning or to raise an exception like "we don't know how it works with 2-channel relay".
[1] https://kmtronic.com/product/2783/usb-relay-controller-two-channels.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote an email to KMTronic Technical support. And they tell me only the
FF 0x 03
read command is supported on the usb 2 relay controller. Seems going back toFF 0x 03
being the default is the way to go. Then if port is set to something other that 1 it tries to read from that number of ports usingFF 09 00
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe with a warnning on 2 port since it is not supported on that one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cidlik would you be able to check if the new changes work on the 1 relay controller?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it. Verified, it works. TY