Bash,串行I/O和Arduino。

时间:2021-11-21 18:11:00

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.

我使用一个Arduino单片机,我试图写两个Bash脚本(现在运行在Mac OS X 10.6)将(a)打印所有的串行数据Arduino单位到标准输出,和(b)请允许我向Arduino串行数据单元。然后,这些脚本将被调用使用adobeair的NativeProcess API,以允许Arduino单元和Flex adobeair应用程序之间的紧密集成。

My two scripts are very simple -

我的两个脚本非常简单。

Here's my WriteToSerial.sh script:

这是我的WriteToSerial。sh脚本:

echo $1 > $2

($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

($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 + C out of the process.

当我调用这些脚本中的任何一个(或者即使我只是直接将命令输入到Bash控制台)时,我的计算机就挂起了——我可以键入字符,但是在我按Ctrl + C退出进程之前,什么都不会发生。

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并打开串行监视器,然后tail -f端口,关闭串行监视器,然后echo“test”>串口,一切都很好。

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?

解决方案是什么?

10 个解决方案

#1


12  

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

和往常一样,在使用上述方法之前先读一下manpage。cread允许您接收数据。如果使用流控制,您可能想省略clocal。如果你不确定上面的设置是什么,问一下,我可以写一个更完整的答案。

#2


27  

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.

我也有同样的问题。我使用Arduino Uno和Ubuntu 12.04。搜索和尝试的几个小时后,我发现Arduino串行设备时将重置开了第一次,但不会重置串行设备时又开了。

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.

因此,在bash中运行命令- echo“输入字符串”> /dev/ttyXXX将会重置Arduino并立即发送“输入字符串”。Arduino需要花一些时间来初始化,并且不能快速地接收到这个字符串。cat /dev/ttyXXX也会重置Arduino。

When /dev/ttyXXX is 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,将/dev/ttyXXX重定向到文件描述3。

exec 3<> /dev/ttyXXX

exec 3 < > /dev/ttyXXX

2) wait for Arduino's initialization

2)等待Arduino的初始化。

sleep 1

睡眠1

3) communicate with Arduino

3)与Arduino交流

echo "input something" >&3

回声“输入”> 3

cat <&3

猫< 3

4) close /dev/ttyXXX

4)关闭/dev/ttyXXX

exec 3>&-

3 > & - exec

#3


4  

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/ cu2。usbserial-FTF7YNJ5 -哦,这是什么?显然,该设备的“呼叫单元”版本不期望或提供任何流控制。将字节转储到端口。正是我需要的。

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

所以只需要做:cat super_file。本> /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-dev -osx-serial-ports.html。

#4


2  

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上,您需要调用setseries来配置您的串行端口选项(波特率、奇值、流控制等),然后才能正确地读取/写入端口。

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 script to do this.

或者您可以编写一个Python脚本来完成这个任务。

#5


1  

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?(进程替换)

#6


0  

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.

您还可以尝试在一个终端窗口中运行该命令,并打开一个新的终端窗口/制表符,并尝试从那里跟踪。

#7


0  

Try / modify ttyecho:

试/修改ttyecho:

http://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/

http://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/

#8


0  

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.

检查从Arduino单元发送数据到/接收数据是否使用不同的应用程序,如玉米片(Mac OS X的串行终端),而不是使用Arduino IDE和串行监视器。

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)

#9


0  

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

还有苹果的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).

另一个Cocoa样例代码展示了如何通过串行连接与Arduino微控制器进行对话,这是objective-candarduino(托管在谷歌代码上)。

#10


0  

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

这是一种能很好地处理数据,并能对数据进行操作的一行程序。

Summary
  • monitoring: the arduino output can trigger actions on the computer
  • 监视:arduino输出可以触发计算机上的操作。
  • 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.
  • notchecked吗?发送消息:我还没有尝试向arduino发送数据,但是看到第二个示例,可能可以通过一些cmdline util向串行端口发送消息。

the following timestamps and sends to stdout

下面的时间戳和发送到stdout。

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

Sample Output:

样例输出:

Bash,串行I/O和Arduino。

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

#1


12  

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

和往常一样,在使用上述方法之前先读一下manpage。cread允许您接收数据。如果使用流控制,您可能想省略clocal。如果你不确定上面的设置是什么,问一下,我可以写一个更完整的答案。

#2


27  

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.

我也有同样的问题。我使用Arduino Uno和Ubuntu 12.04。搜索和尝试的几个小时后,我发现Arduino串行设备时将重置开了第一次,但不会重置串行设备时又开了。

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.

因此,在bash中运行命令- echo“输入字符串”> /dev/ttyXXX将会重置Arduino并立即发送“输入字符串”。Arduino需要花一些时间来初始化,并且不能快速地接收到这个字符串。cat /dev/ttyXXX也会重置Arduino。

When /dev/ttyXXX is 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,将/dev/ttyXXX重定向到文件描述3。

exec 3<> /dev/ttyXXX

exec 3 < > /dev/ttyXXX

2) wait for Arduino's initialization

2)等待Arduino的初始化。

sleep 1

睡眠1

3) communicate with Arduino

3)与Arduino交流

echo "input something" >&3

回声“输入”> 3

cat <&3

猫< 3

4) close /dev/ttyXXX

4)关闭/dev/ttyXXX

exec 3>&-

3 > & - exec

#3


4  

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/ cu2。usbserial-FTF7YNJ5 -哦,这是什么?显然,该设备的“呼叫单元”版本不期望或提供任何流控制。将字节转储到端口。正是我需要的。

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

所以只需要做:cat super_file。本> /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-dev -osx-serial-ports.html。

#4


2  

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上,您需要调用setseries来配置您的串行端口选项(波特率、奇值、流控制等),然后才能正确地读取/写入端口。

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 script to do this.

或者您可以编写一个Python脚本来完成这个任务。

#5


1  

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?(进程替换)

#6


0  

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.

您还可以尝试在一个终端窗口中运行该命令,并打开一个新的终端窗口/制表符,并尝试从那里跟踪。

#7


0  

Try / modify ttyecho:

试/修改ttyecho:

http://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/

http://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/

#8


0  

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.

检查从Arduino单元发送数据到/接收数据是否使用不同的应用程序,如玉米片(Mac OS X的串行终端),而不是使用Arduino IDE和串行监视器。

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)

#9


0  

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

还有苹果的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).

另一个Cocoa样例代码展示了如何通过串行连接与Arduino微控制器进行对话,这是objective-candarduino(托管在谷歌代码上)。

#10


0  

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

这是一种能很好地处理数据,并能对数据进行操作的一行程序。

Summary
  • monitoring: the arduino output can trigger actions on the computer
  • 监视:arduino输出可以触发计算机上的操作。
  • 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.
  • notchecked吗?发送消息:我还没有尝试向arduino发送数据,但是看到第二个示例,可能可以通过一些cmdline util向串行端口发送消息。

the following timestamps and sends to stdout

下面的时间戳和发送到stdout。

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

Sample Output:

样例输出:

Bash,串行I/O和Arduino。

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