Drop Down Menu for sending serial commands

openhab2
binding
items
Tags: #<Tag:0x00007f1e6037d298> #<Tag:0x00007f1e6037cd48> #<Tag:0x00007f1e6037cb68>
(Jehanzeb Mirza) #1

Hello Everybody,

I am using serial binding and my goal is to send simple commands to my serial device. I have to send 3 commands.

  1. u0
  2. u1
  3. u2

Right now I am using two switches to achieve this, with something like this:

Switch hostusb “HOST MODE” { serial="/dev/ttyACM0,ON(u1\n),OFF(u0\n)" }

Switch dutusb “DUT MODE” { serial="/dev/ttyACM0,ON(u2\n),OFF(u0\n)" }

However, I want to have one single drop down menu, which will have 3 options: DUT, HOST and OFF and will of course send u1 if I press DUT, u2 if I press HOST and u0 if I press OFF from that drop down menu.

How can I do this in the most simple way?

Many thanks.

Regards,

Jay.

0 Likes

(charalampos varytimos) #2

try out mappings
test
item

String Test "Test [%s]" { serial="/dev/ttyACM0,u1(u1\n),u2(u2\n),u0(u0\n)" }

sitemap

Switch item=Test  label=" MODE [] " mappings=[u2="DUT",u1="HOST",u0="Off"]

0 Likes

(Jehanzeb Mirza) #3

Hello,

Thank you. I already tried that but that has its problem, for instance it is not so intuitive. I will try to explain why. However, there is a slight modification which needs to be done to the implementation you posted.

Items:

String Test “Test [%s]” { serial="/dev/ttyACM0" }

Sitemaps:

Switch item=Test label=" MODE [ ] " mappings=[“u2\n”=“dut”,“u1\n”=“host”,“u0\n”=“Off”]

This is because it is only taking commands and forwarding to the command line from the mappings. Which is strange but this is how it is doing it. Hence I need to put “\n” to send the command.

However, the problem here is that if I select any one of the mappings, it only lightens up for a second. So this does not give the user a current status at which my board is.

Can anything be done in that regards?

Thank you!

0 Likes

(Rossko57) #4

What value does the serial binding set the String Item to, after transmitting the ux command?

0 Likes

(Jehanzeb Mirza) #5

The values are correct, for instance, if I press DUT then the u2 command is transmitted, I am sure about that because I can see it on my command line terminal which I have opened. And the commands seem to be correct. Also I am displaying the status on the board through a OLED display and it shows that the board is switching between different modes. However, I want the mode which I select to remain lighted on my UI. That is not happening.

0 Likes

(Rossko57) #6

What value does the serial binding set the String Item to, after transmitting the ux command?
The state of this Item is what is used to select a sitemap mapping button to “light up”.

I think this suggestion needs the REGEX magic word, perhaps? Could probably be simplified just to add \n to whatever command, but I don’t know REGEX.
I think it’s a good idea, because the \n might be confusing the sitemap.

0 Likes

(Jehanzeb Mirza) #7

Do you mean something like this in the sitemap?

Switch item=Test label=" MODE [] " mappings=[REGEX(s/\s/\n/g),"u2 "=“DUT”,"u1 "=“HOST”,"u0 "=“Off”]

however, this doesn’t work. If somebody can tell me, what I am doing wrong, it would be a great help.

Thanks

0 Likes

(Jehanzeb Mirza) #8

Also can you please tell me that from where can I check that?

Thanks.

0 Likes

(Rossko57) #9

No. The Item definition suggested by @mpampinos that I highlighted is an Item definition, not a sitemap entry.

My overall thinking here is that you should command your string Item to values like “u1”, and have the serial binding automatically add a “\n” that you need. I think a single REGEX in the Item’s serial binding can do that for you.

But it would be really really helpful to find out what your string Item gets set to after a command, because then you can use that to map your sitemap buttons to get the highlighting working, and then translate that (whatever it is) into something that you really want to send out over serial.

The file events.log shows you command and state changes for Items. You could show us a snippet of that for when you post a command that works, if you put things back to a working condition.

0 Likes

(Jehanzeb Mirza) #10

Hello, Thank you for taking out time to explain me in detail. Here is the screenshot of the logs. My board is currently on u2, which is DUT.

Let me know if this helps.

0 Likes

(Jehanzeb Mirza) #11

yes I understand and that is what I tried to do here:

This should find a ‘white space’ in the command and replace it with \n but this doesn’t work so far either :frowning:

String Test “Test [%s]” { serial="/dev/ttyACM0,REGEX(s/\s/\n/g),u1(u1 ),u2(u2 ),u0(u0 )" }

0 Likes

(Rossko57) #12

Well, whatever your REGEX does (I do not understand REGEX) you are also trying to apply what I think are invalid transforms as well.

ItemStateChangedEvent] - Test changed from u1 to

Okay, so your serial responds to a command with a null or blank or non-printable characters, and that becomes your Item state after a command.

That’s of no use to you at all for the sitemap to use to highlight which button you clicked last.

You need some other way to remember the last click.
A simple way would be to have two Items

String Test "serial command" {serial = "whatevers"}
String Test_Proxy "Mode"

Put the proxy on your sitemap with the text mappings you want. When you click to send a command, autoupdate will also update its state to match the command and the last clicked button will be highlighted.
Write a rule that listens for commands to the proxy and converts them into commands to the real serial Item.

1 Like

(Jehanzeb Mirza) #13

Thank you. This work around works, however, for future reference, one can implement the REGEX command and that might help. But for my purpose this solution works really good.

0 Likes