pySerial write() 在 Python 解释器中工作正常,但不是 Python 脚本

问题描述

最近,我正在尝试在 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)

相关文章