I have a serial device running on 115200 baud at /dev/ttyUSB0. I need to Pipe the output of a command to it. I Need somthing like:

command-that-gives-stdout | command-that-puts-stdin-to-serial --baud 115200 /dev/ttyUSB0

Thanks Ahead for your help!

Additional info:
I have a grbl Sheild runing off of an arduino. It controlls a CNC machine. I Can send GCode to the arduino via the serial connection (/dev/ttyUSB0 running on 115200 baud). I normally send gcode via the arduino ide's serial monitor. I have Several python files for generating gcode, and they output to the stdout. I could just copy&paste the gcode, but a lot of people that will be using it may get confused by this.


I think the following command should work:

( stty raw speed 115200 >&2; echo -ne 'output-string' ) >/dev/ttyUSB0 <&1


  • this launches a shell with input and output redirected to your output device;
  • input needs redirection because stty works on the standard input device, while its diagnostic output should still be directed to the launching terminal;
  • stty raw stops extraneous characters being added to the output;
  • echo -n stops the implicit new-line at the end of the string, while echo -e allows bask-slashed escape sequences to produce non-printable characters if your device requires it;
  • and a subshell is needed so that /dev/ttyUSB0 isn't closed and reopened, which may reset its settings.

Do not redirect standard error: you don't want error messages sent to the device!

I have tested this on /dev/tty1 and it seems to work, but I don't have a serial device to verify it completely. I needed to be in a root shell for the command to work, but your permissions may allow it from a user shell.

  • would this work piping from another program? this looks very similar to a question i currently have a bounty on. superuser.com/questions/1300782/… – Tim_Stewart Mar 12 '18 at 0:46
  • By another program, do you mean something which will generate output strings instead of echo (though I don't understand where the piping comes in)? If so, yes, of course. Without testing, which I can't easily do now, I can't be sure if you can read responses, which wasn't a requirement of the question. You'll need to try it. – AFH Mar 12 '18 at 1:08
  • the program i had in mind was crunch in kali linux. where you can pipe it to other programs using "crunch 4 4 abcd | aircrack-ng *.cap" for instance. but instead of piping it to a program sending the output through a serial connection. i'm open to any suggestions. i believe you can also output it to a file if that makes this easier? – Tim_Stewart Mar 12 '18 at 1:23
  • You could use a sub-subshell or command grouping { ...; }, but it's probably easier to replace the echo command with a function or script name, and do whatever you need to within that. – AFH Mar 12 '18 at 1:45
  • Thank you very much for the suggestion. If it works I'll send you a message to claim the bounty on my question! – Tim_Stewart Mar 12 '18 at 1:50

Have you tried simply directing stdout to the /dev/ttyUSB0 (command-that-gives-stdout > /dev/ttyUSB0)? I would think that the OS would handle any buffering and baud rate conversion for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.