pySerial write()在Python解释器中可以正常工作,但在Python脚本中不能正常工作 [英] pySerial write() works fine in Python interpreter, but not Python script
问题描述
最近,我正在尝试在Arduino上进行某种灯光控制".我使用Raspberry Pi通过串行端口(USB电缆)发送控制消息.这是Arduino代码:
Recently, I am trying to make sort of "light control" on Arduino. I use Raspberry Pi to send the control message via serial port (USB cable).Here is the Arduino code :
int redled = 12;
int whiteled = 48;
void setup()
{
Serial.begin(9600);
pinMode(redled,OUTPUT);
pinMode(whiteled,OUTPUT);
}
void loop()
{
if(Serial.available())
{
char cmd = Serial.read();
switch(cmd)
{
case'r':
digitalWrite(redled,HIGH);
delay(2000);
digitalWrite(redled,LOW);
break;
case'w':
digitalWrite(whiteled,HIGH);
delay(2000);
digitalWrite(whiteled,LOW);
break;
}
}
else
{
Serial.println("hello pi");
delay(1000);
}
}
在那之后,我使用了Python解释器中的pySerial来控制引脚,并且一切工作正常.这是一段解释器输出:
After that, I used pySerial from Python interpreter to control the pins, and everything was working fine. Here is a piece of interpreter output:
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial('/dev/ttyACM0',9600)
>>> x = ser.read(10)
>>> print 'x = ',x
x = hellhello
>>> ser.write('w') #white led turn on and off
1
>>> ser.close()
>>>
一切正常,并且led确实可以打开和关闭,所以我决定编写一个简单的Python脚本来执行相同的操作:
Everything worked fine and led did turn on and off, so I decided to write a simple Python script to do the same:
import serial
import time
ser = serial.Serial('/dev/ttyACM0',9600)
x = ser.read(10)
print 'x = ',x
time.sleep(2)
ser.write('w')
ser.close()
以下是执行命令和结果:
The following is the execution command and result:
pi@raspberrypi ~ $ python serialtest.py
x = helello pi
它只出现在Arduino的字符串中,而根本没有打开LED. 看起来一切都应该很好,所以我不知道问题可能出在哪里.我已经搜索了一些文章,并在"ser.write()"之前添加了"time.sleep(2)",但仍然无法正常工作.我将不胜感激,在此先感谢您!
It only appeared the string from Arduino, but no led turn on at all. It looks like everything should be fine, so I don't know what the problem can be. I already search some articles and add "time.sleep(2)" before "ser.write()", but it still couldn't work.I would appreciate any help, many thanks in advance!
更新: 我让控制器将接收到的数据发回给我,并且在运行脚本时似乎什么也没接收到,但是当我从解释器发送数据时,接收到了一切. arduino代码的代码现在看起来像这样:
UPDATE : I made the controller send me back the data it was receiving and it looks like it isn't receiving anything when I am running the script, but receives everything when I send the data from the interpreter. The code of the arduino code now looks like this:
int redled = 12;
int whiteled = 48;
void setup()
{
Serial.begin(9600);
pinMode(redled,OUTPUT);
pinMode(whiteled,OUTPUT);
}
void loop()
{
if(Serial.available())
{
char cmd = Serial.read();
switch(cmd)
{
case'r':
digitalWrite(redled,HIGH);
delay(2000);
digitalWrite(redled,LOW);
Serial.println("Cmd received");
break;
case'w':
digitalWrite(whiteled,HIGH);
delay(2000);
digitalWrite(whiteled,LOW);
Serial.println("Cmd received");
break;
}
}
}
推荐答案
问题是初始化端口需要一些时间. 在ser = serial.Serial()之后立即添加5秒钟的睡眠时间
The problem is that it takes some time to initiate the port. add a sleep of 5 seconds immediately after ser = serial.Serial()
time.sleep(5)
这篇关于pySerial write()在Python解释器中可以正常工作,但在Python脚本中不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!