早上好!最近,我买了一个Arduino开发板来对房间进行“灯光控制”。这是我编写的固件代码:
int control = 0; int pin = 0; void setup() { Serial.begin(9600); for(pin = 0; pin <= 13; pin++) pinMode(pin, OUTPUT); } void loop() { control = Serial.read(); if (control > 0 && control <= 13) digitalWrite(control, HIGH); if (control < 256 && control >= (256-13)) digitalWrite((256-control), LOW); }
之后,我使用了Python解释器中的pySerial来控制引脚,一切工作正常。这是一段解释器输出:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import serial >>> ser = serial.Serial('/dev/ttyUSB0', 9600) >>> ser.write(chr(12)) >>> # The light turned on here ... >>> ser.write(chr(256-12)) >>> # The light turned off here ...
然后,我决定编写一个简单的Python脚本来执行相同的操作:
#!/usr/bin/env python import serial import time ser = serial.Serial('/dev/ttyUSB0', 9600) ser.write(chr(12)) time.sleep(1) ser.write(chr(256-12))
但这根本不起作用!Arduino显示在我启动脚本的过程中收到了一些东西,但是什么也没发生。这是脚本的strace输出:
open("/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NONBLOCK) = 3 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(3, SNDCTL_TMR_START or TCSETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 open("/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NONBLOCK) = 4 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(4, SNDCTL_TMR_START or TCSETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 write(4, "\f", 1) = 1 close(4) = 0 rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7f45cf4c88f0}, {0x4d9820, [], SA_RESTORER, 0x7f45cf4c88f0}, 8) = 0 exit_group(0) = ?
看起来一切都应该很好,所以我不知道问题可能出在哪里。我将不胜感激,在此先感谢您!
PS当我在PDB下运行该程序时,一切正常。Heisenbug。
更新:我让控制器将接收到的数据发回给我,并且在运行脚本时似乎什么也没接收到,但是当我从解释器发送数据时,接收到了所有内容。固件的代码现在如下所示:
int control = 0; int pin = 0; void setup() { Serial.begin(9600); for(pin = 0; pin <= 13; pin++) pinMode(pin, OUTPUT); } void loop() { if (Serial.available() > 0) { control = Serial.read(); if (control <= 13) digitalWrite(control, HIGH); if (control < 256 && control >= (256-13)) digitalWrite((256-control), LOW); Serial.println(control); } }
我认为这可能是打开串行端口和发送数据之间的竞争状态。我可能会在打开和写入调用之间进行睡眠。
另外,您可能不想打开这个库“ serial”,而是直接打开并直接写入设备,这可能是在做一些有趣的事情(请参阅其他文章中提到的double open)