Bash、串行 I/O 和 Arduino

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3918032/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:44:53  来源:igfitidea点击:

Bash, serial I/O and Arduino

bashcommand-lineserial-portarduino

提问by Myk

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 AIRapplication.

我正在使用 Arduino 微控制器,我正在尝试编写两个 Bash 脚本(现在在 Mac OS X 10.6 中运行),它们将 (a) 将来自 Arduino 单元的所有串行数据打印到标准输出,并且(b) 允许我向 Arduino 单元发送串行数据。然后将使用 Adob​​e AIR 的 NativeProcess API 调用这些脚本,以实现 Arduino 单元和 Flex Adobe AIR应用程序之间的紧密集成。

My two scripts are very simple -

我的两个脚本非常简单——

Here's my WriteToSerial.sh script:

这是我的 WriteToSerial.sh 脚本:

echo  > 

($1 is obviously my string, $2 is the location of the serial port - currently /dev/tty.usbserial-A800eIUj)

($1 显然是我的字符串,$2 是串口的位置——目前是 /dev/tty.usbserial-A800eIUj)

And here's my ReadSerialOutput.sh script:

这是我的 ReadSerialOutput.sh 脚本:

tail -f 

($1 is the location of my serial port, currently /dev/tty.usbserial-A800eIUj)

($1 是我的串口位置,目前是/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+ Cout of the process.

当我调用之一这些脚本(或者哪怕我只是直接键入命令到Bash的控制台),我的电脑只是挂起-我可以输入文字,但没有任何反应,直到我Ctrl+C的过程中进行。

However, if I open the Arduino IDE and turn on the Serial Monitor, then tail -fthe port, close the serial monitor, and then echo "test" > serial port, everything works just great.

但是,如果我打开 Arduino IDE 并打开串行监视器,然后tail -f打开端口,关闭串行监视器,然后回显“测试”> 串行端口,一切正常。

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.

这向我表明,在 Arduino IDE 中打开串行监视器以某种方式初始化串行端口,这反过来又使我可以毫无问题地跟踪它。这反过来又告诉我,我只是没有输入某种初始化命令。但是,几天来我一直在高低搜索,似乎找不到任何解决此问题的方法。

What is the solution?

解决办法是什么?

采纳答案by J. Polfer

Try using the tool stty:

尝试使用工具stty

stty -F /dev/my_serial_port <baud_rate> cs8 cread clocal

As always, read the manpage before applying the above. creadallows you to receive data. You may want to omit clocalif 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.

与往常一样,在应用上述内容之前阅读联机帮助页。 cread允许您接收数据。clocal如果您使用流控制,您可能希望省略。如果您不确定上述设置是什么,请询问,我可以写出更完整的答案。

回答by Yihui Xiong

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 resetwhen the serial device is opened for the first time,but will not reset when the serial device is opened again.

我也遇到同样的问题。我在 Ubuntu 12.04 上使用 Arduino Uno。经过几个小时的搜索和尝试,我发现第一次打开串口设备时Arduino会重置,但再次打开串口设备时不会重置。

So, run command - echo "input string" > /dev/ttyXXXin 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/ttyXXXwill reset Arduino too.

因此,在 bash 中运行命令 - echo "input string" > /dev/ttyXXX将重置 Arduino 并立即发送"input string"。Arduino 需要一些时间来初始化,并且不够快来接收这个字符串。cat /dev/ttyXXX也会重置 Arduino。

When /dev/ttyXXXis opened in somewhere firstly, these commands will work.

/dev/ttyXXX首先在某处打开时,这些命令将起作用。

Here is my solution:

这是我的解决方案:

1) open /dev/ttyXXX by redirecting /dev/ttyXXX to file description 3

1) 通过将 /dev/ttyXXX 重定向到文件描述 3 来打开 /dev/ttyXXX

exec 3<> /dev/ttyXXX

执行 3<> /dev/ttyXXX

2) wait for Arduino's initialization

2)等待Arduino的初始化

sleep 1

睡觉 1

3) communicate with Arduino

3)与Arduino通信

echo "input something" >&3

cat <&3

echo "输入内容" >&3

猫<&3

4) close /dev/ttyXXX

4) 关闭 /dev/ttyXXX

exec 3>&-

执行 3>&-

回答by chrism

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.

我也遇到了这个问题,尝试了无休止的 stty 设置和技巧,将我的文件放到 /dev/tty.usbserial-FTF7YNJ5 (在我的情况下),同时站在一个脚趾等。

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.

然后我做了一个 ls /dev 并注意到 /dev/cu.usbserial-FTF7YNJ5 -- 哦,这是什么?显然,设备的“调用单元”版本不期望或不提供任何流量控制。将字节转储到端口。正是我需要的。

So just do: cat super_file.bin > /dev/cu.usbserial-XXXXX

所以就这样做: 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

希望这可以帮助。直到现在我知道答案,我才发现:http: //stuffthingsandjunk.blogspot.com/2009/03/devcu-vs-devtty-osx-serial-ports.html

回答by J. Polfer

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.

在 Linux 上,您需要调用 setserial 来配置您的串行端口选项(波特率、奇偶校验、流量控制等),然后才能正确读/写端口。

You need to find a way to do this with your Mac OS X Bash system.

您需要找到一种使用 Mac OS X Bash 系统执行此操作的方法。

Or you could write a Python scriptto do this.

或者您可以编写一个 Python 脚本来执行此操作。

回答by dan

Maybe try some serial command line tool similar to serial-1.0.

也许尝试一些类似于 serial-1.0 的串行命令行工具。

See: Serial port loopback/duplex test, in Bash or C? (process substitution)

请参阅:Bash 或 C 中的串行端口环回/双工测试?(过程替换)

回答by bm7150

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.

您还可以尝试在 1 个终端窗口中运行该命令,然后打开一个新的终端窗口/选项卡,然后尝试从那里拖尾。

回答by tol

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.

通过使用不同的应用程序(例如 Cornflake(Mac OS X 的串行终端),而不是使用 Arduino IDE 和串行监视器)来检查向 Arduino 单元发送数据/从 Arduino 单元接收数据是否正常工作。

In addition, you may want to check out if you could benefit from switching to Xcode (in terms of debugging features, etc.).

此外,您可能想看看是否可以从切换到 Xcode 中受益(在调试功能等方面)。

See: Setting up Xcode to Compile & Upload to an Arduino ATMega328 (Duemilanove)

请参阅:设置 Xcode 以编译并上传到 Arduino ATMega328 (Duemilanove)

回答by ceeit

There's also Apple's SerialPortSample command line tool that allows you to set arbitrary baud rates:

还有 Apple 的 SerialPortSample 命令行工具,可让您设置任意波特率:

// from: SerialPortSample/SerialPortSample.c
// ...
// Starting with Tiger, the IOSSIOSPEED ioctl can be used to set arbitrary baud rates
// other than those specified by POSIX. The driver for the underlying serial hardware
// ultimately determines which baud rates can be used. This ioctl sets both the input
// and output speed. 
// ...

For more information see: http://www.arduino.cc/playground/Interfacing/Cocoa

有关更多信息,请参阅: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).

另一段向您展示如何通过串行连接与 Arduino 微控制器通信的 Cocoa 示例代码是 Objective-candarduino(托管在 Google 代码上)。

回答by gskielian

A one-liner Something that works really wellfor datalogging, and acting on data:

一个单行的东西,非常适合数据记录和处理数据:

概括
  • monitoring:the arduino output can trigger actions on the computer
  • data-logging:it simultaneously save that streaming data to a file
  • notchecked? sending-messages:I haven't tried sending data yet to the arduino, but see the second example, might be able to send a message to the serial port via some cmdline util.
  • 监控:arduino 输出可以触发计算机上的动作
  • 数据记录:它同时将流数据保存到文件中
  • 没有检查?发送消息:我还没有尝试向 arduino 发送数据,但请参阅第二个示例,也许可以通过某些 cmdline util 向串行端口发送消息。

the following timestamps and sends to stdout

以下时间戳并发送到标准输出

cat /dev/cu.usbmodem1421 | awk '{ for (i=0; i<NF; i++) printf $i + system("echo  , `date`")}'

Sample Output:

示例输出:

enter image description here

在此处输入图片说明

This method can even be adapted to monitor and act upon the data in real time:

这种方法甚至可以适用于实时监控和处理数据:

cat /dev/cu.usbmodem1421 | awk '{ for (i=0; i<NF; i++) printf $i + system("echo , `date`)}'

more examples here: https://github.com/gskielian/Arduino-DataLogging/tree/master/Bash-One-Liner

更多示例:https: //github.com/gskielian/Arduino-DataLogging/tree/master/Bash-One-Liner