使用 Bash 将单个字节写入串行端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8191888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a single byte to the serial port with Bash
提问by DJRyan
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.
我有一个 Arduino,我已经将它编码为从 USB 串行端口读取并为 LED 供电。我知道它可以工作,因为它可以在内置的串行监视器上工作。现在我想编写一个写入串行端口的 Bash 脚本。
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?
它输出字符串“123”。我怎样才能写一个值为 121 的单个字节?
回答by ruakh
echo 121 > /dev/cu.usbmodem411
will write four bytes: 0x31 (meaning '1'), 0x32 (meaning '2'), 0x31 again, 0x0A (meaning a newline).
将写入四个字节:0x31(表示“1”)、0x32(表示“2”)、0x31 再次、0x0A(表示换行)。
If your goal is to write a single byte, with value 121, you would write this:
如果您的目标是写入单个字节,值为 121,您可以这样写:
echo -n $'1' > /dev/cu.usbmodem411
where 171 is 121 expressed in base-8, and -ntells echonot to print a newline character.
其中 171 是以 base-8 表示的 121,并-n告诉echo不要打印换行符。
If that's notyour goal, then please clarify.
如果这不是你的目标,那么请澄清。

