The Simple Serial plugin works fine for sending commands to serial devices expecting text commands, as the "action" for the plugin allows the user to input a text string that is then queued and sent.
However, for devices that require low-level characters (e.g. hex 0x01, 0x02, etc.) entering these in a text box just sends the character string of 4 characters, not the single low-level character.
Thoughts?
Jim
Send Hex to Serial Device
Forum rules
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
- matt (support)
- Site Admin
- Posts: 21542
- Joined: Mon Jan 27, 2003 1:17 pm
- Location: Texas
- Contact:
Re: Send Hex to Serial Device
The escaped hex sequences (0x01, etc.) are interpreted by python as it compiles source code which is why those sequences only work in that (source code) context. More on that in a bit. First, I would suggest you have 3 different text fields in the action. One to send character data like this:
One to send decimal binary data like:
And one to send hexadecimal binary data like:
So the user would select which one of the 3 to use (only one would work per action). You could have a popup menu where the user chooses the type (character string, decimal binary, hex binary) then have that value bound to the visibility of the three controls, or you could just have all 3 always visible and have the UI validation show an error if they enter anything in more than one edit field.
The idea is to make it so the user doesn't have to try to encode the data using backslashes or any special notation. Instead have it be very explicit by having 3 different options and then they can chose the option that is being used by their hardware protocol. Note that all 3 examples shown above actually send the same raw data out on the serial since the ASCII, decimal and hex values are all equivalent.
Now inside your action you have the string field you handle normally (just send it), and you need to convert the other 2 into a binary stream to be sent. I'd recommend allowing the data to be delimited by either spaces or commas. Here is what I would do for the decimal binary case:
And almost identical for the hexadecimal case (just changed the base to the int() cast):
Note you have to catch ValueError which will be raised from the loop if the user enters bad data (hex values that are bigger than FF, decimal values larger than 255, missing commas or spaces, etc.)
Code: Select all
Character String: HELLOCode: Select all
Decimal Byte List: 72, 69, 76, 76, 79Code: Select all
Hex Byte List: 48, 45, 4C, 4C, 4FThe idea is to make it so the user doesn't have to try to encode the data using backslashes or any special notation. Instead have it be very explicit by having 3 different options and then they can chose the option that is being used by their hardware protocol. Note that all 3 examples shown above actually send the same raw data out on the serial since the ASCII, decimal and hex values are all equivalent.
Now inside your action you have the string field you handle normally (just send it), and you need to convert the other 2 into a binary stream to be sent. I'd recommend allowing the data to be delimited by either spaces or commas. Here is what I would do for the decimal binary case:
Code: Select all
example = "72, 69, 76, 76, 79"
binData = ""
normalized = example.replace(",", " ") # replace commas with spaces
try:
for num in normalized.split():
binData += chr(int(num,10))
# binData ready to sent out here!
except ValueError, err:
indigo.server.log("bad data!")Code: Select all
example = "48, 45, 4C, 4C, 4F"
binData = ""
try:
normalized = example.replace(",", " ") # replace commas with spaces
for num in normalized.split():
binData += chr(int(num,16))
# binData ready to sent out here!
except ValueError, err:
indigo.server.log("bad data!")Re: Send Hex to Serial Device
Thanks as always Matt,
I knew there had to be a simple way to encode the binary or hex strings.
I varied a bit from your code, but I think it's essentially the same (version 0.9.1 of Simple Serial plugin posted).
Jim
I knew there had to be a simple way to encode the binary or hex strings.
I varied a bit from your code, but I think it's essentially the same (version 0.9.1 of Simple Serial plugin posted).
Jim
