Devices USB HOST

Hello

I’am David

I explain my problem

I am using an Arduino Yun, I connect a device to the USB HOST port, with PUTTY I send a command to the device switch on Remote mode ON « echo -en ‘\xF1\x00\x36\x10\x10\x01\x47’ > /dev/ttyACM0 »

and Remote OFF « echo -en ‘\xF1\x00\x36\x10\x00\x01\x37’ > /dev/ttyACM0 »

it’s ok !

I would use the commands in the Arduino

Here is my sketch but does not work

#include <Process.h>

void setup()
{
Bridge.begin();
Serial.begin(9600);
while (!Serial);
}

void loop()
{
if (Serial.read() == ‘A’)
{
Remote();
}
}
void Remote()
{
Process p;
p.begin(“echo”);
p.addParameter("-en");
p.addParameter("\xF1\x00\x36\x10\x10\x01\x47");
p.addParameter("> /dev/ttyACM0");
p.run();

while (p.available() > 0)
{
char c = p.read();
Serial.print(c);
}
Serial.flush();
}

Have an idea?

Thanx !

Hello,
try to split

p.addParameter("> /dev/ttyACM0");

into

p.addParameter(">");
p.addParameter( "/dev/ttyACM0");

does it return from p.run?

Hello

I split the command but the devices does switch on REMOTE ON

In serial monitoring, I received “ñ > /dev/ttyACM0”

A question: syntax “p.addParameter("\xF1\x00\x36\x10\x10\x01\x47");” Arduino send in ASCII ?

thanks

david

David68:
A question: syntax “p.addParameter("\xF1\x00\x36\x10\x10\x01\x47");” Arduino send in ASCII ?

Yes, it sends in ASCII but it is not relevant because the entire string is passed to the linux bridge process and then it runs the full command string.

I just noticed that in

 p.addParameter("\xF1\x00\x36\x10\x10\x01\x47");

quotes are missing, while in the command that you run from ssh there are quotes.

Ok

yes i forget the quote

Process p;
  p.begin("echo");
  p.addParameter("-en");
  p.addParameter("'\xF1\x00\x36\x10\x10\x01\x47'");
  p.addParameter(">");
  p.addParameter("/dev/ttyACM0");
  p.run();

I added but does not change

David68:

  p.addParameter("'\xF1\x00\x36\x10\x10\x01\x47'");

The backslash character "" has special meaning in C and C++ strings. It is an ESCAPE SEQUENCE that allows you to add certain unprintable characters to your string. It takes the form of a backslash followed by another character, at which point that character takes on a new meaning. For example, "\r" becomes a carriage return character.

In C and C++, the "\x" escape sequence takes the next two characters, treats them as hexadecimal, and replaces the four characters ("\xNN") with the single character with that hexadecimal code. So, you are actually passing raw binary characters to the echo command.

In your case, you want the backslash character to be part of the string, and you want it to be an ASCII string. To do so, you have to double up the backslashes: "\" when between quotes becomes a single backslash in the resulting string.

You need to change that line to:

 p.addParameter("'\\xF1\\x00\\x36\\x10\\x10\\x01\\x47'");

Note how each backslash is doubled, which will actually put a single backslash in the string.

ShapeShifter:
::::SNIP::::
In C and C++, the "\x" escape sequence takes the next two characters, treats them as hexadecimal, and
::::SNIP::::

@ShapeShifter,
yeah I think you guys are right. My headache (now 3days) has me in a fog.
Just a note, I've had the annoying issues of having to use three (3) slashes.
I even recall a rare fourth slash
Like this:

"'\\\xF1\\\x00...

Jesse

jessemonroy650:
Just a note, I've had the annoying issues of having to use three (3) slashes.
I even recall a rare fourth slash
Like this:

"'\\\xF1\\\x00...

That code with triple slashes will result in a string containing a backslash character, then a character with the binary value 0xf1, then another backslash character, then a NULL character with a binary value of 0x00, and so on.

Four backslashes in a row will put two backslashes in the string.

ShapeShifter:
The backslash character "" has special meaning in C and C++ strings. It is an ESCAPE SEQUENCE that allows you to add certain unprintable characters to your string. It takes the form of a backslash followed by another character, at which point that character takes on a new meaning. For example, "\r" becomes a carriage return character.

Yeah, you're right! I was thinking to read the bridge library to find the problem, you saved me a lot of time :slight_smile:

Thank everybody

It’s OK !

I use command “runShellCommand”

#include <Process.h>

void setup()
{
  Bridge.begin();
  Serial.begin(9600);
  while (!Serial);
}

void loop()
{
  if (Serial.read() == 'A')
  {
    Remote();
  }
}
void Remote()
{
  Process p;
  p.runShellCommand("echo -en '\\xF1\\x00\\x36\\x10\\x10\\x01\\x47' > /dev/ttyACM0");

  while (p.available() > 0)
  {
    char c = p.read();
    Serial.print(c);
  }
  Serial.flush();
}

thank again !