DJRyan
DJRyan

Reputation: 149

Write a single byte to the serial port with Bash

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?

Upvotes: 3

Views: 6442

Answers (1)

ruakh
ruakh

Reputation: 183321

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.

Upvotes: 11

Related Questions