Bash、串行 I/O 和 Arduino
所以,我有点不知所措,我觉得我已经非常接近解决方案了,但它还没有发挥作用。这是我的情况:
我正在使用 Arduino 微控制器,并且我正在尝试编写两个 Bash 脚本(现在在 Mac OS X 10.6 中运行),这将 (a) 打印来自 Arduino 单元的所有串行数据到标准输出,(b) 允许我将串行数据发送到 Arduino 单元。然后,将使用 Adobe AIR 的 NativeProcess API 调用这些脚本,以实现 Arduino 单元和 Flex 之间的紧密集成 Adobe AIR 应用程序。
我的两个脚本非常简单 -
这是我的 WriteToSerial.sh 脚本:
echo $1 > $2
($1 显然是我的字符串,$2 是串行端口的位置 - 当前为 /dev/tty.usbserial-A800eIUj)
这是我的 ReadSerialOutput.sh 脚本:
tail -f $1
($1是我的串行端口的位置,当前为 /dev/tty.usbserial-A800eIUj)
当我调用这些脚本中的任何一个时(或者即使我只是直接在 Bash 控制台中输入命令),我的计算机只是挂起 - 我可以输入字符,但直到我 Ctrl + C 退出进程之前,什么也没有发生。
但是,如果我打开 Arduino IDE 并打开串行监视器,然后 tail -f
端口,关闭串行监视器,然后 echo "test" >串口,一切都很好。
这对我来说意味着在 Arduino IDE 中打开串行监视器会以某种方式初始化串行端口,这反过来又允许我毫无问题地跟踪它。这反过来表明我只是未能输入某种初始化命令。然而,我已经搜索了好几天了,似乎找不到任何可以解决这个问题的东西。
解决办法是什么?
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:
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.
My two scripts are very simple -
Here's my WriteToSerial.sh script:
echo $1 > $2
($1 is obviously my string, $2 is the location of the serial port - currently /dev/tty.usbserial-A800eIUj)
And here's my ReadSerialOutput.sh script:
tail -f $1
($1 is the location of my serial port, currently /dev/tty.usbserial-A800eIUj)
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.
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.
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.
What is the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我也遇到同样的问题。我在 Ubuntu 12.04 上使用 Arduino Uno。经过几个小时的搜索和尝试,我发现Arduino在第一次打开串口设备时会重置,但当串口设备被打开时不会重置。再次打开。
因此,运行命令 - echo "input string" > bash 中的 /dev/ttyXXX 将重置 Arduino 并立即发送“输入字符串”。 Arduino 需要一些时间来初始化,并且接收该字符串的速度不够快。 cat /dev/ttyXXX 也会重置 Arduino。
当/dev/ttyXXX首先在某处打开时,这些命令将起作用。
这是我的解决方案:
1)通过将 /dev/ttyXXX 重定向到文件描述 3 来打开 /dev/ttyXXX
2) 等待Arduino初始化
3) 与Arduino通信
4) 关闭 /dev/ttyXXX
I get the same problem too. I use Arduino Uno with Ubuntu 12.04. After a few hours of searching and trying, I find out that Arduino will reset when the serial device is opened for the first time,but will not reset when the serial device is opened again.
So, run command - echo "input string" > /dev/ttyXXX in bash will reset Arduino and send "input string" immediately. Arduino need take some time to initialize, and is not quick enough to receive this string. cat /dev/ttyXXX will reset Arduino too.
When /dev/ttyXXX is opened in somewhere firstly, these commands will work.
Here is my solution:
1) open /dev/ttyXXX by redirecting /dev/ttyXXX to file description 3
2) wait for Arduino's initialization
3) communicate with Arduino
4) close /dev/ttyXXX
尝试使用工具stty:
一如既往,先阅读手册页应用上述内容。
cread
允许您接收数据。如果您使用流量控制,您可能需要省略clocal
。如果您不确定上述设置是什么,请询问,我可以写出更完整的答案。Try using the tool stty:
As always, read the manpage before applying the above.
cread
allows you to receive data. You may want to omitclocal
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.我也遇到了这个问题,尝试了无数的 stty 设置和技巧,将我的文件保存到 /dev/tty.usbserial-FTF7YNJ5 (就我而言),同时站在一个脚趾上,等等。
然后我做了一个 ls /dev 并注意到/dev/cu.usbserial-FTF7YNJ5——哦,这是什么?显然,该设备的“呼叫单元”版本不期望也不提供任何流量控制。将字节转储到端口。正是我所需要的。
所以只需执行: cat super_file.bin > /dev/cu.usbserial-XXXXX
希望这有帮助。现在我知道答案了,我发现了这个: http://stuffthingsandjunk.blogspot.com/2009/03/devcu-vs-devtty-osx-serial-ports.html
I struggled with this problem also, trying no end of stty settings and tricks to cat my files to /dev/tty.usbserial-FTF7YNJ5 (in my case) whilst standing on one toe, etc.
Then I did an ls /dev and noticed /dev/cu.usbserial-FTF7YNJ5 -- oh, what's this? Apparently, a 'calling unit' version of the device that doesn't expect or provide any flow control. Dumps bytes to the port. Exactly what I needed.
So just do: cat super_file.bin > /dev/cu.usbserial-XXXXX
Hope this helps. And only now that I know the answer, I found this: http://stuffthingsandjunk.blogspot.com/2009/03/devcu-vs-devtty-osx-serial-ports.html
在Linux上,您需要调用setserial来配置串行端口选项(波特率、奇偶校验、流量控制等),然后才能正确读/写端口。
您需要找到一种方法来使用 Mac OS X Bash 系统执行此操作。
或者您可以编写一个Python脚本来执行此操作。
On Linux, you need to call setserial to configure your serial port options (baud rate, parity, flow-control, etc.) before you can read/write the port correctly.
You need to find a way to do this with your Mac OS X Bash system.
Or you could write a Python script to do this.
也许尝试一些类似于serial-1.0的串行命令行工具。
请参阅:串口环回/双工测试,在 Bash 或 C 中? (进程替换)
Maybe try some serial command line tool similar to serial-1.0.
See: Serial port loopback/duplex test, in Bash or C? (process substitution)
尝试在命令末尾添加一个与号 (&),以将进程置于后台。如果控制台挂起,则意味着脚本或进程仍在当前终端上运行,并且在进程或脚本完成之前您将无法输入或单击任何内容。
您还可以尝试在 1 个终端窗口中运行该命令,然后打开一个新的终端窗口/选项卡,然后尝试从那里进行跟踪。
Try adding an ampersand (&) to the end of the commands to put the process in the background. If the console is hanging up, then that means the script or process is still running on your current terminal, and you won't be able to input or click on anything until the process or script is done.
You can also try running the command in 1 terminal window, and open a new terminal window/tab, and try tailing from there.
尝试/修改ttyecho:
http ://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/
Try / modify ttyecho:
http://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/
使用不同的应用程序(例如 Cornflake(适用于 Mac OS X 的串行终端))检查向 Arduino 单元发送数据/从 Arduino 单元接收数据是否正常工作,而不是使用 Arduino IDE 和 Arduino 设备。串行监视器。
此外,您可能想看看切换到 Xcode 是否可以让您受益(在调试功能等方面)。
请参阅:将 Xcode 设置为编译&上传到 Arduino ATMega328 (Duemilanove)
Check to see if sending data to / receiving data from the Arduino unit is working by using a different app such as Cornflake (serial terminal for Mac OS X) - instead of using the Arduino IDE & the Serial Monitor.
In addition, you may want to check out if you could benefit from switching to Xcode (in terms of debugging features, etc.).
See: Setting up Xcode to Compile & Upload to an Arduino ATMega328 (Duemilanove)
还有 Apple 的 SerialPortSample 命令行工具,可让您设置任意波特率:
有关详细信息,请参阅:http: //www.arduino.cc/playground/Interface/Cocoa
向您展示如何通过串行连接与 Arduino 微控制器对话的另一段 Cocoa 示例代码是 Objective-candarduino(托管在 Google 代码上)。
There's also Apple's SerialPortSample command line tool that allows you to set arbitrary baud rates:
For more information see: http://www.arduino.cc/playground/Interfacing/Cocoa
Another piece of Cocoa sample code that shows you how to talk to the Arduino microcontroller over a serial connection is objective-candarduino (hosted on Google code).
一句话非常有效用于数据记录和对数据进行操作:
Summary
以下时间戳并发送到 stdout
示例输出:
此方法甚至可以适应实时监控数据并根据数据采取行动:
更多示例如下:
https://github.com/gskielian/Arduino-DataLogging/树/master/Bash-One-Liner
A one-liner Something that works really well for datalogging, and acting on data:
Summary
the following timestamps and sends to stdout
Sample Output:
This method can even be adapted to monitor and act upon the data in real time:
more examples here:
https://github.com/gskielian/Arduino-DataLogging/tree/master/Bash-One-Liner