sending serial command to linux box [SOLVED]

Hi.

I’m trying to send strings over serial port to a linux box to be interpreted as command line. Sending from arduino by Serial.println.
I got it working but in some case got issue with data to be send.

One of this case is to send this string: snra=wl rssi ; echo -ne “<$snra>” > /dev/tts/0.

The way I use at this time:

String cmd = ("snra=`wl rssi` ; echo -ne "<$snra>" > /dev/tts/0 ");
...
...
Serial.println (cmd);

return error

error: stray ‘$’ in program
error: ‘snra’ was not declared in this scope

If I omit " " at $snra, no error, but he command will be miss-interpreted by the linux box.

The arduino serial.println will send :

snra=wl rssi ; echo -ne <$snra> > /dev/tts/0

not: snra=wl rssi ; echo -ne “<$snra>” > /dev/tts/0. as I need.

Knowing that I can´t do without sending: “expression”, how to avoid this error, or any other way to send this kind of string by serial ?

Any help is welcome.

You need to escape the quotes that are inside the string:

String cmd = ("snra=`wl rssi` ; echo -ne \"<$snra>\" > /dev/tts/0 ");

otherwise the compiler sees that as two separate strings with <$snra> between them.

Pete

Hi

Thanks.

I got it. It's working as it should be.
Now got some other issue but is on linux side. Need to fix the way to send back to arduino the result of command.

Again, thank for your help.

You could probably run something like this in a shell to take commands via the serial port and have the output of those commands sent back to the serial port:

PS1= PS2= sh </dev/ttyUSB0 >/dev/ttyUSB0 2>&1

(Change ttyUSB0 to your actual serial device.)

This command just runs a shell (without a prompt) that reads from and writes to the serial port.

Now, I wouldn’t recommend allowing for arbitrary commands over a serial port like this; I would strongly prefer to write a program on the PC side (perhaps in Python) that will take a restricted set of commands over the serial port.

christop:
You could probably run something like this in a shell to take commands via the serial port and have the output of those commands sent back to the serial port:

PS1= PS2= sh </dev/ttyUSB0 >/dev/ttyUSB0 2>&1

(Change ttyUSB0 to your actual serial device.)

Hi.
Thanks for suggestion.

That was the way I was doing, sh /dev(tts/0

Now, I wouldn’t recommend allowing for arbitrary commands over a serial port like this; I would strongly prefer to write a program on the PC side (perhaps in Python) that will take a restricted set of commands over the serial port.

You are right, but at this time I’m on test mode, not using a real linux computer. Testing with some dd-wrt based router, and I want to keep it simple as it could be.
So i changed the way to do it, now I just send expression, and in linux side, depending of that expression, it will run some command.

arduino:

Serial.write ("send \r\n");

Linux side:

#!/bin/sh
while sleep 1; do
 read -r line < /dev/tts/0 ;
  if [ "$line" == "send" ]; then
       snra=`wl rssi | tr -d '-'` ; echo -ne "<$snra>" > /dev/tts/0
           else
       echo null > /dev/null
  fi
  done

By this way, I got what I need for now.
Thanks again for suggestion.