Arduino 和 pySerial 的问题
我遇到了问题。我最近购买了一块 Arduino Uno 板。我尝试做一些有趣的事情,比如控制计算机的输入。我将 Python 与 pySerial 一起使用,程序如下:
arduino = serial.Serial(portaCOM, 9600, timeout = 1)
... in loop ->
arduino.write(value)
def sliderUpdate(self, event):
pos = self.slider.GetValue()
arduino.write(pos)
time.sleep(.1)
print arduino.readline()
try:
arduino = serial.Serial(portaCOM, 9600, timeout = 1)
except:
print "Errore di connessione alla porta seriale"
写入值应通过 USB 将值发送到我的板。 板上加载的程序是:
const int ledPin = 11;
byte brightness;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop(){
while(Serial.available()){
brightness = Serial.read();
Serial.print(brightness);
analogWrite(ledPin, brightness); //LED doesn't refresh the brightness
delay(10);
}
}
我的LED工作正常。我尝试使用 Arduino 提供的 Fade 示例,它正在工作。
我检查了程序是否正确发送数据。是的。它返回的内容与我之前发送的内容相同。
它应该检索发送的值并设置 analaogWriter(pin, VALUE),但出现问题或无法正常工作。
我该如何解决这个问题?
解决方案
Arduino代码
const int ledPin = 11;
byte valoreLed;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop(){
while(Serial.available()){
valoreLed = Serial.read();
analogWrite(ledPin, valoreLed);
delay(20);
}
}
Python脚本代码:
pos = self.slider.GetValue()
arduino.write(chr(pos))
谢谢大家!! :)
I got a problem. I recently bought an Arduino Uno board. I tried to make something funny like controlling an input from my computer. I used Python with pySerial and the program is the following:
arduino = serial.Serial(portaCOM, 9600, timeout = 1)
... in loop ->
arduino.write(value)
def sliderUpdate(self, event):
pos = self.slider.GetValue()
arduino.write(pos)
time.sleep(.1)
print arduino.readline()
try:
arduino = serial.Serial(portaCOM, 9600, timeout = 1)
except:
print "Errore di connessione alla porta seriale"
The write value should send the value to my board though USB.
The program loaded on board is:
const int ledPin = 11;
byte brightness;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop(){
while(Serial.available()){
brightness = Serial.read();
Serial.print(brightness);
analogWrite(ledPin, brightness); //LED doesn't refresh the brightness
delay(10);
}
}
My LED is working properly. I tried with the Fade example provided by Arduino and it's working..
I checked if the program is sending properly the data. Yes, it is. It returns the same thing I sent before.
It should retrieve the value sent and set analaogWriter(pin, VALUE)
, but something is wrong or not working.
How can I fix this problem?
Solution
The Arduino code
const int ledPin = 11;
byte valoreLed;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop(){
while(Serial.available()){
valoreLed = Serial.read();
analogWrite(ledPin, valoreLed);
delay(20);
}
}
Python script code:
pos = self.slider.GetValue()
arduino.write(chr(pos))
Thank you to everybody!! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
value
的值是多少?尝试 arduino.write(chr(0xFF)),LED 是否一直亮着?value
in your example? Tryarduino.write(chr(0xFF))
, does LED stay lit?我怀疑 arduino.readline() 等待换行符,而 Arduino 代码从不发送换行符。因此,Python 代码在发送第一个值后会无限期地阻塞。
I suspect that
arduino.readline()
waits for a newline, and you Arduino code never sends a newline. Hence the Python code blocks indefinitely after sending the first value.我在您的评论中看到 LED 适用于第一个字符,但您没有看到不同字符的变化。一定要确保您发送的 ASCII 字符与之前的字符明显不同,例如发送 ASCII 0 后跟 ASCII 255。
编辑:
既然您说 FadeLED 示例有效,您能否仅以该示例为例并向其添加串行端口功能?例如,仅在接收到串行字符后才淡出。这至少会让您确信串行端口和 LED 可以在您编写的代码中共存。另外,我假设您正在使用此处中的 FadeLED 示例。如果是这样,请确保您设置了正确的引脚号,因为参考代码使用引脚 9,但您的示例代码使用引脚 11。
另一个编辑:
你说你从 Arduino 正确接收到了字符,但你使用的是 pySerial
ReadLine
函数,该函数应该阻塞,直到它看到换行符,并且我在 Arduino 代码中没有看到任何内容会生成一个未首先发送给它的换行符。要消除问题中的变量数量,请尝试使用超级终端或类似的 COM 端口程序将字节发送到 Arduino,而不是 Python 程序。例如,您可以发送空格字符 (ASCII 32) 和“}”字符 (ASCII 125),它们应该足够不同,以便在 LED 中产生可见的差异。I see in your comments that the LED works with the first character, but you don't see a change with the different characters. Definitely make sure you're sending ASCII characters that are significantly different from the previous characters, e.g. send an ASCII 0 followed by an ASCII 255.
Edit:
Since you say the FadeLED example works, can you just take that example and add serial port functionality to it? For example, only fade after a serial character is received. This will at least get you confident that the serial port and leds can coexist in code you write. Also, I assume you're using the FadeLED example from here. If so, make sure you've set the correct pin number, as the reference code uses pin 9, but your sample code uses pin 11.
Another Edit:
You say you receive the characters back properly from the Arduino, but you're using the pySerial
ReadLine
function, which should block until it sees a newline, and I don't see anything in the Arduino code that would generate a newline that wasn't sent to it first. To eliminate the number of variables in the question, try using HyperTerminal or a similar COM port program to send bytes to the Arduino, rather than your Python program. For example, you can send space characters (ASCII 32) and '}' characters (ASCII 125), which should be different enough to create a visible difference in the LED.我首先确定串行端口的哪一侧存在问题。
首先,您可以使用 Arduino 上的硬编码循环来驱动 LED 吗? LED 的亮度是否按预期变化?
一旦解决了这个问题,您可以让 Arduino 在从终端应用程序(例如内置于 Arduino IDE 的应用程序)手动发送内容时执行一些明显正确的操作吗?这可能需要更改 Arduino 代码。
一旦工作正常,我就会专注于 Python 代码,确保您正在与正确的 COM 端口等进行通信。
I would begin by establishing which side of the serial port harbours the problem.
First of all, can you drive the LED using a hard-coded loop on the Arduino. Does the LED change its brightness as expected?
Once that's out of the way, can you make the Arduino do something visibly correct when sending stuff manually from a terminal app (e.g. the one built into the Arduino IDE). This may require changes to the Arduino code.
Once that's working, I would then focus on the Python code, making sure you're talking to the right COM port etc.
“写入值应该通过 USB 将值发送到我的板”
如果这不是拼写错误,那么您不能使用 PySerial 访问 USB 端口。有 pyUSB 代替,可能还有其他。
"the write value should send the value to my board through USB"
If this is not a typo, then you can not use PySerial to access USB ports. There is pyUSB instead, and probably others.