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

Python 使用pyserial控制Arduino Uno板上的特定引脚,python,arduino,pyserial,Python,Arduino,Pyserial,我有一个python代码,它以一种模式发送,其中灯光必须闪烁。(例如101010。每次运行代码时,模式可能会有所不同)。当它无限期地执行此操作时,我需要一个中断(同样由python代码发送)来保存灯光的当前状态(假设它正在运行序列的1),并执行特定任务,如关闭灯光10秒钟,然后恢复序列。 一种方法是通过使中断引脚处于高位来中断程序。问题是这种高/低的制作是否可以由pyserial控制。 因此,一个简单的伪代码是: 代码的PYTHON部分: Read the sequence: Send the

我有一个python代码,它以一种模式发送,其中灯光必须闪烁。(例如101010。每次运行代码时,模式可能会有所不同)。当它无限期地执行此操作时,我需要一个中断(同样由python代码发送)来保存灯光的当前状态(假设它正在运行序列的1),并执行特定任务,如关闭灯光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管脚)激活“断电ISR”,而无需使用管脚上的物理开关。因此,引脚状态必须由软件控制

2) 是否可以执行断电功能注释中提到的操作

3) 在python代码中,可以只发送一次数据(即模式,speedy),并使arduino以无限方式执行模式,而无需通过
serial.write
命令再次发送数据。因此,避免了
while(1)
循环在
ser.isOpen()

之后,请查看以下内容:

这是我在Arduino端集中力量发布的任意命令,如将引脚切换为高/低和设置PWM电平等。它可以通过串行和网络工作,尽管目前在网络端是一个触控错误

要使用它,将代码放在arduino上,然后您只需编写一个python脚本(或任何其他可以使用串行连接的语言)通过串行连接进行连接,然后告诉它您想要做什么,例如DIGW 1 HIGH等


还可以看看:在Django中,我使用这个库的一个变体来控制一些发光二极管,这是基于python的。你需要通过物理信号pin2来中断吗?如果没有,那么您可能会通过写入arduino寄存器导致软件中断。我们如何使用pyserial执行软件中断?你能举个例子解释一下吗?是不是有点像端口操纵?pyserial允许端口操作吗?你的意思是延迟吗?不,我不想要延迟。中断的输入应该来自python代码,因为我不想将按钮连接到特定的引脚。因此,软件(pyserial)应该能够控制arduino板的引脚。