Bash、串行 I/O 和 Arduino
所以,我有点不知所措,我觉得我已经非常接近解决方案了,但它还没有完全奏效.这是我的情况:
So, I'm in a bit over my head, and I feel like I'm very close to a solution, but it's just not working quite yet. Here's my situation:
我正在使用 Arduino 微控制器,我正在尝试编写两个 Bash 脚本(现在在 Mac OS X 10.6 中运行),它们将 (a) 打印来自 Arduino 单元的所有串行数据到标准输出,以及 (b) 允许我将串行数据发送到 Arduino 单元.然后将使用 Adobe AIR 的 NativeProcess API 调用这些脚本,以允许 Arduino 单元和 Flex Adobe AIR 之间的紧密集成 申请.
I'm working with an Arduino microcontroller, and I'm attempting to write two Bash scripts (right now running in Mac OS X 10.6) which will (a) print all serial data coming out of the Arduino unit to the standard output, and (b) allow me to send serial data to the Arduino unit. These scripts will then be called using Adobe AIR's NativeProcess API to allow a tight integration between the Arduino unit and a Flex Adobe AIR application.
我的两个脚本很简单-
这是我的 WriteToSerial.sh 脚本:
Here's my WriteToSerial.sh script:
echo $1 > $2
($1 显然是我的字符串,$2 是串口的位置——目前是/dev/tty.usbserial-A800eIUj)
($1 is obviously my string, $2 is the location of the serial port - currently /dev/tty.usbserial-A800eIUj)
这是我的 ReadSerialOutput.sh 脚本:
And here's my ReadSerialOutput.sh script:
tail -f $1
($1 是我的串口位置,目前是/dev/tty.usbserial-A800eIUj)
($1 is the location of my serial port, currently /dev/tty.usbserial-A800eIUj)
当我调用这些脚本中的任何一个时(或者即使我只是直接在 Bash 控制台中输入命令),我的计算机就会挂起 - 我可以输入字符,但是直到我 Ctrl +C退出进程.
When I call either of these scripts (or even if I just type the commands directly into the Bash console), my computer just hangs - I can type characters, but nothing happens until I Ctrl + C out of the process.
但是,如果我打开 Arduino IDE 并打开串行监视器,然后 tail -f
端口,关闭串行监视器,然后 echo "test" > 串行端口,一切正常很棒.
However, if I open the Arduino IDE and turn on the Serial Monitor, then tail -f
the port, close the serial monitor, and then echo "test" > serial port, everything works just great.
这向我表明,在 Arduino IDE 中打开串行监视器以某种方式初始化串行端口,这反过来又使我可以毫无问题地跟踪它.这反过来又告诉我,我只是没有输入某种初始化命令.但是,我已经搜索了好几天,似乎找不到任何解决此问题的方法.
This suggests to me that opening the Serial Monitor within the Arduino IDE is somehow initializing the serial port, which in turn allows me to tail it with no problem. This in turn suggests to me that I'm simply failing to input some sort of initialization command. However, I've been searching high and low for days and can't seem to find anything that addresses this issue.
解决方案是什么?
尝试使用工具 stty:
stty -F /dev/my_serial_port <baud_rate> cs8 cread clocal
与往常一样,在应用上述内容之前阅读联机帮助页.cread
允许你接收数据.如果您使用流控制,您可能希望省略 clocal
.如果你不确定上面的设置是什么,问,我可以写一个更完整的答案.
As always, read the manpage before applying the above. cread
allows you to receive data. You may want to omit clocal
if you are using flow control. If you aren't sure what the above settings are, ask, and I can write up a more complete answer.