Sending Serial commands in HEX rather than ASCII

Hey all. Eyes are burning from the reading I’ve done over the past few hours

This feels like it should be simple, but I sure seem to be at a loss. Maybe I’m just overtired!!

I want to just send a string of hex values out to an end device through the USB to Serial adapter I have plugged in to switch inputs on an old video matrix. Its a Monoprice Blackbird 4x4 21905. Support is terrible as expected

I can get this working using a tool (CommWatch) and can switch the OPs just fine
I have the output from HA tied over to CommWatch to see the packets

the “switch” is not really what I want (rather this just be a 1 shot type command. Feedback looks like a pain with this device. Proof of concept first and this find a better method (unless you have something better in mind than “switch” that is!)

Device expects HEX
Sending this specific string changes output 1 to input 1
50 56 54 02 03 01 00 01 00 00 00 00 00 00 00 00 00 01
Sending this specific string changes output 1 to input 2
50 56 54 02 03 02 00 01 00 00 00 00 00 00 00 00 00 02
This works flawlessly from CommWatch

Config (trying with and without spaces):

sensor
  - platform: serial
    serial_port: /dev/ttyUSB0
    baudrate: 115200

switch:
  - platform: command_line
    switches: 
      set_op1_to_in1:
        command_on: 'echo "505654020301000100000000000000000001" > /dev/ttyUSB0'
      set_op1_to_in2:
        command_on: 'echo "505654020302000100000000000000000002" > /dev/ttyUSB0'
      set_op1_to_in1x:
        command_on: 'echo "50 56 54 02 03 01 00 01 00 00 00 00 00 00 00 00 00 01" > /dev/ttyUSB0'
      set_op1_to_in2x:
        command_on: 'echo "50 56 54 02 03 02 00 01 00 00 00 00 00 00 00 00 00 02" > /dev/ttyUSB0'

In CommWatch, this shows up as (in order)

35 30 35 36 35 34 30 32 30 33 30 31 30 30 30 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 31 0A
35 30 35 36 35 34 30 32 30 33 30 32 30 30 30 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 32 0A
35 30 20 35 36 20 35 34 20 30 32 20 30 33 20 30 31 20 30 30 20 30 31 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 31 0A
35 30 20 35 36 20 35 34 20 30 32 20 30 33 20 30 32 20 30 30 20 30 31 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 30 20 30 32 0A

These are the ASCII version of my intended message, so the command_line is definitely sending ASCII
How do I get this to go out as the HEX line that I’m trying to send

https://stackoverflow.com/questions/657846/how-do-i-write-non-ascii-characters-using-echo/65878758#65878758

@tom_l Thanks!

Read and tried this. Not quite right as this looks like an octal to hex, but it lead me to searching out the command and locating this:

\0NNN byte with octal value NNN (which can be 1 to 3 digits).
\xHH byte with hexadecimal value HH (which can be either 1 or 2 digits)

So my new commands that do function as expected are:

      set_op1_to_in1:
        command_on: 'echo -e "\x50\x56\x54\x02\x03\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" > /dev/ttyUSB0'
      set_op1_to_in2:
        command_on: 'echo -e "\x50\x56\x54\x02\x03\x02\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02" > /dev/ttyUSB0'

I’ll roll with this and post a thread when done for all the coding in case someone else can make use of it

Next hurdles:

  • Make this more like a button push (not a switch)
  • Status command of output #1:
    – Send “\x50\x56\x54\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFE”
    – Receive 50 56 54 02 01 01 00 02 00 00 00 00 00 00 00 00 00 00
    – 8th byte in return is the value for what output is currently selected
    – Take that 2 and display the current input on output 1

I don’t know if you have any thoughts for those two things?

Use a shell command instead of a command line switch. You can then assign the command to the tap action of a button.

Use a command line sensor.

The value_template will be tricky but not impossible.

This is how I ended up. Shell Command was pretty straight forward.

I think getting the byte of interest out of a string return may just be a little out of my league for now. gonna need to run though a lot more learning prior to that

shell_command:
    matrix_op1_in1: /bin/bash -c "echo -e '\x50\x56\x54\x02\x03\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01' > /dev/ttyUSB0"
    matrix_op2_in1: /bin/bash -c "echo -e '\x50\x56\x54\x02\x03\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02' > /dev/ttyUSB0"

Then in Lovelace, I put 4 buttons stacked with a different View for each Room (TV):

type: button
tap_action:
  action: call-service
  service: shell_command.matrix_op1_in1
  service_data: {}
  target: {}
name: Bell Satellite
show_icon: false
icon_height: 4px
show_name: true
icon: hass:video

Not pretty, but I’m still a noob here

If you have some good spots to point me at to get a handle on some of this, I’d be grateful to be pointed at them!

My final solution (albeit incomplete) is located here:

Thanks again Tom!

1 Like