蟒蛇 + Arduino 与 Mac OS X
我在 Arduino 和 Python 之间通信时遇到问题。我有几个问题希望能够得到解答,但首先也是最重要的是,我需要简单地建立连接。
对于Windows,显然这个解决方案相当方便,但在Mac OS X上,我显然需要访问一些系统文件(我不熟悉)。 Python 文档将我指向特定的帖子 Re: Can Python do串行端口的东西?,但我不认为它完全符合我的目的。
在这一点上,为了仅仅查看沟通的证据,我尝试了这个。
Arduino:
void setup(){
Serial.begin(9600);
}
void loop()
{
int d = Serial.read();
Serial.println(d,BYTE);
}
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
正如此处所证明的,我真的不明白我导入的模块到底在做什么。在阅读文档时,我没有看到通过串行发送数据的明显方法。那么我的猜测是否正确,无论该程序的输出是什么,它都会自动发送?
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.
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.
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...)
#!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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Python 中与 Arduino(或任何具有串行功能的微控制器)进行通信的最简单方法是使用 pySerial。
下面是一个示例:
PS:如果您使用的是 Python 3,则应该发送字节而不是字符串(即
b'text'
)。The easiest way to communicate in Python with the Arduino (or any microcontroller with serial) is using pySerial.
Here's an example:
PS: If you're using Python 3, you should send bytes instead of strings (that is,
b'text'
).在我这边,我已经使用 sudo 命令解决了 OSX 上的串行错误;我认为在 OSX 上,您必须获得管理员权限才能在 pip 安装后与串行通信 throw /dev/cu.usbmodem14101 。
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.
我已经在 Linux 下使用 Perl 完成了此操作,但没有使用 Python 或 Mac 的经验。我可以给你一些寻找的建议。
首先,在您的 Python 程序中,您需要将 USB 端口的正确设备地址放入
serialPath
中,否则您的数据将无法到达 Arduino。在 Linux 中,我在连接板并从中找到设备名称后执行了 lsusb。在你的 Arduino 代码中将其更改为否则
如果没有数据你将转储一堆 -1。
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.
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.In your Arduino code change it to be
as otherwise you will be dumping a bunch of -1s if there is no data.