Carrage Returns RS232

Hey guys, Ive been butchering a custom componant I found here to get my Cambridge Audio amplifier working in home assistant. The componant loads fine and I can see it in lovelace. The problem is the commands are not being sent.
If I use a program like termite and set carrage returns on and I can control the amp. However when using Home Assistant I get nothing.
Here is the componant I have been playing with https://github.com/lievencoghe/cambridge_cxa

and here is the specific lines Im trying to add a carrage return to

AMP_CMD_GET_PWSTATE = "#01,01"
AMP_CMD_GET_CURRENT_SOURCE = "#03,01"
AMP_CMD_GET_MUTE_STATE = "#01,03"

I have tried \n and \r and a combination of both, inside and outside of the quotes to no avail.
Has anyone seen this before? How would I go about fixing it?

Don’t know if it’s the cause, but apparently echo is known for doing strange things. Maybe -e or -n on the echo command, or use printf instead?

So something like

result = subprocess.run(['ssh', self._username + '@' + self._host, 'tty=/dev/ttyUSB0; exec 4<$tty 5>$tty; stty -F $tty 9600 -echo; echo "' + command + '-n" >&5; read reply <&4; echo $reply'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)

Sorry Im no good with python

Well, python is only adding the command part. The rest of it, and presumably what’s causing you trouble, is the shell command.

I was actually suggesting echo -e <remainder_of_command> (or maybe -n) or replacing it with printf. I don’t know if echo requires the arguments to be right after the command, so safest to put it there. Have you tried just sending the SET commands to see if one direction works? Perhaps the problem is actually capturing the response.

But for debugging, I’d suggest cutting out the python part… makes it quicker and easier to iterate. So instead of opening the terminal program, just issue the shell commands yourself. Something like:

stty -F /dev/ttyUSB0 9600 -echo
printf "#01,04,1\n\r" > /dev/ttyUSB0
read reply < /dev/ttyUSB0
echo $reply

to turn set mute on. Not sure if you get a reply from that command (what does your terminal program show?)… but once you get one way stuff working, then work on capturing GET commands.

You can monitor the replies by replacing the “read” and “echo” commands with another terminal window which just does something like cat < /dev/ttyUSB0 … .it’ll run until you hit ctrl-C or something.

Another debugging tip: Do you have another serial port in the house (either on the same computer, or another computer)? If so, you could connect the two together (using a “null modem” aka crossover) cable and watch with a terminal program to see what actually get spit out.

Thanks for the reply. Im not infront of the amp/home assistant at the moment, Im running a vpn from my samsung phone using Dex at work :rofl:
I did think of adding an arduino or something to see if I could read the output.
Yeah the amp auto replies with termite on windows and even in the arduino serial monitor. However they both auto return.
Ill try and monitor the incoming signal when I get home. Thanks again

So I got home and its working when i ssh into the pi and run

printf "#01,04,1\n\r" > /dev/ttyUSB0

The amp replies as expected. I even deleted the \n and it worked no problem.
Ive run the who command on the pi and its telling me that home assistant is connected.
This weird, Im so close I can smell it. Im missing something here

Does it work without the \n, or is that required also?

Regardless, the next step I’d take is to verify the whole ssh line works. If you turn debug logging on in HA, presumably it will give you a copy of the command it is trying to send. Is there an obvious problem with it?

If not, have you done anything to accept the ssh key? ssh may be waiting at a prompt for you. There are a few ways to fix this: manually accept the key by either running ssh to that host yourself (with the user that HA would be launching the command with), or using one or more of the command options outlined here: https://askubuntu.com/questions/123072/ssh-automatically-accept-keys Note that the version of ssh that’s being called by HA may not have the same options… probably best to try your ssh command from within your HA instance (I’m not sure what host you’re running on).

Yes \n is not required when I send the command over the terminal

yeah ssh seems connected. I generated ssh keys and ssh-copy-id to the pi. A w command on the pi confirms that my home assistant ip is connected.
Sorry Im no good with python, just a mechanic with a passion to tinker.
Ill try looking through HA debug logs and see what they give out
Thanks for all the help! :+1:

Hello @IamDan

I can’t get the custom component to load. I’ve cloned the repo and followed the instructions and it just won’t work apparently. Could you help me figure out what is going on?

Hey mate, Im sorry its been a long time since I tried doing this. I have since got a new amp and no longer use the Cambridge. There is a good chance that HA has changed to much for the old component to work.