使用pyserial控制Arduino Uno板上的特定引脚

3 投票
1 回答
5643 浏览
提问于 2025-04-17 01:17

我有一段Python代码,它会发送一个灯光闪烁的模式,比如说101010。每次运行代码时,这个模式可能会有所不同。当代码无限执行这个模式时,我想要一个中断(也是由Python代码发送的),来保存当前灯光的状态(比如说它正在执行序列中的某一部分),然后执行一个特定的任务,比如关掉灯10秒钟,然后再继续执行这个序列。

实现这个的一个方法是通过将中断引脚设为高电平来中断程序。我的问题是,这个高低电平的控制能否通过pyserial来实现。

简单的伪代码如下:

Python部分代码:

Read the sequence:
Send the sequence to the arduino board using pyserial.
while(1)
{
    Run a timer for 15 second.
    When the timer overflows interrupt the arduino.
}

Arduino部分代码:

Read the sequence 
while (1)
{
    keep repeating the sequence on the LED.
}

// if interrupted on pin2  // assuming pin2 has the interrupt procedure
// Pyserial has to enable the interrupt WITHOUT using a switch for enabling the pin.

ISR 
{
    Save the present state of execution.
    Turn off the LED.
 }

为了更好地理解:

我写了一些小代码来展示我遇到的疑问:

Arduino的代码是:

int ledpin1 = 13;

int speedy;

int patterns;

void setup()

{

  Serial.begin(9600);

  Serial.print("Program Initiated: \n");

  pinMode(ledpin1,OUTPUT);

  //activate the blackout ISR when a interrupt is achieved at a certain pin. In this case pin2 of the arduino

  attachInterrupt(0,blackout,CHANGE);

}

void loop()

{

  if (Serial.available()>1)

  {

    Serial.print("starting loop \n");

    patterns = Serial.read();

    patterns = patterns-48;

    speedy = Serial.read();

    speedy = (speedy-48)*1000;

    while(1)

    {

      patterns = !(patterns);

      Serial.print(patterns);

      digitalWrite(ledpin1,patterns);

      delay(speedy);

    }

  }

}

/*

void blackout()

{

  // ***Save the present state of the LED(on pin13)***

  Serial.print ("The turning off LED's for performing the python code\n");

  digitalWrite(ledpin,LOW);

  //wait for the Python code to complete the task it wants to perform, 

  //so got to dealy the completion of the ISR

  delay(2000);// delay the whole thing by 2 seconds

  //***Continue with the while loop by setting the condition of the light to the saved condition.***

}

*/

==================================================================================

Python前端的代码是:

import serial

import time

patterns=1

speedy=1

ser = serial.Serial()

ser.setPort("COM4")

ser.baudrate = 9600

ser.open()

def main():

    if (ser.isOpen()):

        #while(1):

        ser.write(patterns)

        ser.write(speedy)

        blackoutfunc()

        #ser.close()



def blackoutfunc():

    while(1):

        time.sleep(2)

        print "Performing operations as required"

===============================================================================

现在我有几个问题:

1) 有没有办法根据引脚的状态(在这个例子中是引脚2,也就是INT0引脚)来激活“黑暗中断服务程序”,而不需要在引脚上使用物理开关?因此,引脚的状态需要通过软件来控制。

2) 是否可以执行在黑暗功能的注释中提到的操作?

3) 在Python代码中,是否可以只发送一次数据(即模式和速度),让Arduino无限执行这个模式,而不需要再次通过serial.write命令发送数据?这样就可以避免在ser.isOpen()之后使用while(1)循环。

1 个回答

0

看看这个:

https://github.com/ajfisher/arduino-command-server

这是我在Arduino上做的一个项目,可以发送一些命令,比如把某个引脚设置为高电平或低电平,或者调整PWM(脉宽调制)级别等等。它可以通过串口和网络两种方式工作,不过现在网络那边有点小问题。

使用方法很简单,把代码上传到你的Arduino上,然后写一个Python脚本(或者其他任何可以使用串口连接的语言)来通过串口连接,然后告诉它你想做什么,比如输入DIGW 1 HIGH等等。

另外,你也可以看看这个:https://github.com/ajfisher/arduino-django-visualiser,在这里我用这个库的一个变种来控制一些LED灯,具体是根据Django中的一些事情来控制的,这个项目更多是基于Python的。

撰写回答