3

I have an Arduino which I have coded to read from a USB serial port and power an LED. I know it is working because it works on the built serial monitor. Now I want to write a Bash script which writes to the serial port.

Here is the command:

 echo 121 > /dev/cu.usbmodem411

It outputs the string "123". How can I instead write a single byte with a value of 121?

1
  • Thanks that was right I found my problem. If I pass 121 it writes an integer. If I put "y" it will it will write 121 as a byte which is what I want. Commented Nov 19, 2011 at 4:32

1 Answer 1

11
echo 121 > /dev/cu.usbmodem411

will write four bytes: 0x31 (meaning '1'), 0x32 (meaning '2'), 0x31 again, 0x0A (meaning a newline).

If your goal is to write a single byte, with value 121, you would write this:

echo -n $'\171' > /dev/cu.usbmodem411

where 171 is 121 expressed in base-8, and -n tells echo not to print a newline character.

If that's not your goal, then please clarify.

Sign up to request clarification or add additional context in comments.

1 Comment

or you can also use the hex notation when sending the value: echo -en '\x79' > /dev/cu.usbmodem411

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.