Send Hex to Serial Device

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
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
yergeyj
Posts: 260
Joined: Wed Dec 29, 2004 7:30 am

Send Hex to Serial Device

Post by yergeyj »

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
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Send Hex to Serial Device

Post by matt (support) »

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:

Code: Select all

Character String: HELLO
One to send decimal binary data like:

Code: Select all

Decimal Byte List: 72, 69, 76, 76, 79
And one to send hexadecimal binary data like:

Code: Select all

Hex Byte List: 48, 45, 4C, 4C, 4F
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:

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!")
And almost identical for the hexadecimal case (just changed the base to the int() cast):

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!")
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.)
Image
yergeyj
Posts: 260
Joined: Wed Dec 29, 2004 7:30 am

Re: Send Hex to Serial Device

Post by yergeyj »

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
Locked

Return to “Extending Indigo with Plugins and Python”