macos Python + Arduino 与 Mac OS X
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6039367/
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
Python + Arduino with Mac OS X
提问by danem
I'm having trouble communicating between my Arduino and Python. I have a couple of questions that I hope can be answered, but first and most importantly, I need to simply establish a connection.
我的 Arduino 和 Python 之间无法通信。我有几个问题希望能得到解答,但首先也是最重要的是,我需要简单地建立一个连接。
For Windows, apparently the solution is rather convenient, but on Mac OS X, I apparently need to access some system files (which I am not familiar with). The Python documentation points me to the specific post Re: Can Python do serial port stuff?, but I don't think it quite serves my purposes.
对于 Windows,显然该解决方案相当方便,但在 Mac OS X 上,我显然需要访问一些系统文件(我不熟悉)。Python 文档将我指向特定的帖子Re: Can Python do serial port stuff? ,但我不认为它完全符合我的目的。
At this point, trying to merely see evidence of communication I've tried this.
在这一点上,试图仅仅看到交流的证据,我已经试过了。
Arduino:
阿杜诺:
void setup(){
Serial.begin(9600);
}
void loop()
{
int d = Serial.read();
Serial.println(d,BYTE);
}
Python: (pretty much from the mentioned link...)
Python:(几乎来自提到的链接......)
#!usr/bin/python
import os, fcntl, termios, sys
serialPath = '/dev/tty.usbmodemfa141'
ser= os.open(serialPath, 0)
[iflag, oflag, cflag, lflag, ispeed, ospeed, cc] = range(7)
settings = termios.tcgetattr(ser)
settings[ospeed] = termios.B9600
settings[ispeed] = termios.B0
print 2
As evidenced here, I really don't understand what the modules I am importing are doing exactly. While reading the documentation I see no obvious way to send data over serial. So am I right in guessing that whatever the output of this program is it will be sent over automatically?
正如这里所证明的,我真的不明白我正在导入的模块到底在做什么。在阅读文档时,我看不到通过串行发送数据的明显方法。所以我猜这个程序的输出是什么,它会自动发送吗?
回答by JBernardo
The easiest way to communicate in Python with the Arduino (or any microcontroller with serial) is using pySerial.
在 Python 中与 Arduino(或任何带有串行的微控制器)进行通信的最简单方法是使用pySerial。
Here's an example:
下面是一个例子:
import serial
s = serial.Serial(port='/dev/tty.usbmodemfa141', baudrate=9600)
s.write('text')
s.read()
s.readline()
PS: If you're using Python 3, you should send bytes instead of strings (that is, b'text'
).
PS:如果您使用的是 Python 3,则应该发送字节而不是字符串(即b'text'
)。
回答by Rasika
I have done this using Perl under Linux, but have no experience with Python or Mac. I can give you a few pointers to look for.
我在 Linux 下使用 Perl 完成了此操作,但没有使用 Python 或 Mac 的经验。我可以给你一些指示来寻找。
First, in your Python program you need to put the proper device address for your USB port in serialPath
as otherwise your data will not reach the Arduino. In Linux I did a lsusb after I connected the board and found the device name from that.
首先,在您的 Python 程序中,您需要为 USB 端口输入正确的设备地址,serialPath
否则您的数据将无法到达 Arduino。在 Linux 中,我在连接电路板并从中找到设备名称后做了一个 lsusb。
In your Arduino code change it to be
在您的 Arduino 代码中将其更改为
void loop()
{
if(Serial.available() > 0)
{
d = Serial.read();
Serial.println(d,BYTE);
}
}
as otherwise you will be dumping a bunch of -1s if there is no data.
否则,如果没有数据,您将倾倒一堆 -1。
回答by gaellm
On my side I've solved Serial error on OSX using the sudo command; I think that on OSX you have to get admin rights to communicate throw /dev/cu.usbmodem14101 with Serial after a pip install.
在我这边,我已经使用 sudo 命令解决了 OSX 上的串行错误;我认为在 OSX 上,您必须获得管理员权限才能在 pip 安装后将 throw /dev/cu.usbmodem14101 与 Serial 通信。