通过 Bash 打开到 Arduino 的串行连接

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

Opening serial connection to Arduino through Bash

bashshellserial-portarduino

提问by hcaw

I have set up my Arduino so when I send a "0" via the serial monitor, a stepper motor turns a given amount.

我已经设置了我的 Arduino,所以当我通过串行监视器发送“0”时,步进电机会转动给定的量。

I want to include this in a bash script, but I can only get this to work when the arduino serial monitor is open and entering echo 0 > /dev/tty.usbserial641in bash. I assume this is because serial monitor is opening the connection for me.

我想将其包含在 bash 脚本中,但只有在 arduino 串行监视器打开并输入echo 0 > /dev/tty.usbserial641bash时才能使其工作。我认为这是因为串行监视器正在为我打开连接。

In my struggle to open the connection in bash (without serial monitor open) I have tried all manner of options with stty -f /dev/tty.usbserial641and have also tried connecting reset to ground with a 10uF capacitor.

在我努力在 bash 中打开连接(没有打开串行监视器)时,我尝试了各种选项,stty -f /dev/tty.usbserial641还尝试使用 10uF 电容器将复位连接到地。

Can any help me open the connection in bash without the use of arduino serial monitor?

有没有人可以帮助我在不使用 arduino 串行监视器的情况下在 bash 中打开连接?

System: Arduino Uno rev3 OS X 10.8.4

系统:Arduino Uno rev3 OS X 10.8.4

Many thanks, hcaw

非常感谢,hcaw

回答by John b

Do the commands below work for you.

执行以下命令对您有用。

 # stty -F /dev/ttyUSB0 9600 cs8 -cstopb
 # sleep 0.1
 # echo "0" > /dev/ttyUSB0

There is a difference between the value 0 and the ascii char 0 (48). Which one you trying to send, and which one are you trying to receive?

值 0 和 ascii char 0 (48) 之间存在差异。您尝试发送哪一个,您尝试接收哪一个?

If you want to read the port from the terminal you can do it like this

如果你想从终端读取端口,你可以这样做

head -n 1 /dev/ttyUSB0  //the number after n is how many lines you want to read

As a last note, I am a fan of pySerial. I would much rather write an interface in python than shell scripts.

最后一点,我是pySerial的粉丝。我更愿意用 python 编写一个接口而不是 shell 脚本。

回答by hcaw

I found a great binary written in C that solves my problem called Arduino-serial. Here's the link.I hope this helps people with similar problems!

我找到了一个用 C 编写的很棒的二进制文件,它解决了我的问题,称为 Arduino 串行。这是链接。我希望这可以帮助有类似问题的人!