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:Python可以做串口吗?但我不认为它完全符合我的目的。
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?
正如这里所证明的那样,我真的不明白我导入的模块到底在做什么。在阅读文档时,我看不到通过串行发送数据的明显方法。所以我正确地猜测无论该程序的输出是什么,它都将自动发送?
2 个解决方案
#1
13
The easiest way to communicate in Python with the Arduino (or any microcontroller with serial) is using pySerial.
用Arduino(或任何带串口的微控制器)在Python中进行通信的最简单方法是使用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')。
#2
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.
我在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程序中,您需要在serialPath中为USB端口设置正确的设备地址,否则您的数据将无法到达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。
#1
13
The easiest way to communicate in Python with the Arduino (or any microcontroller with serial) is using pySerial.
用Arduino(或任何带串口的微控制器)在Python中进行通信的最简单方法是使用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')。
#2
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.
我在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程序中,您需要在serialPath中为USB端口设置正确的设备地址,否则您的数据将无法到达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。