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



我有一个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 coden");
  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)是否有一种方法能够在不使用引脚上存在的物理开关的情况下,根据引脚(在本例中为INT0引脚的pin2)的条件激活"停电ISR"?因此,引脚状态必须由软件来操纵。

2)是否可以执行停电函数注释中提到的操作?

3)在python代码中,是否可以只发送数据(即:模式,快速)仅一次,并使arduino以无限的方式执行模式,而无需再次通过serial.write命令发送数据。从而避免了ser.isOpen()之后的while(1)循环

看看这个:

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

这是我在Arduino方面拉在一起发出任意命令,如开关引脚高/低和设置PWM电平等。它可以在串行和网络上工作,尽管目前在网络方面有触摸bug。

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

还可以看看:https://github.com/ajfisher/arduino-django-visualiser,这是我使用这个库的一个变体来控制一些基于Django中的一些事情的led -它更多地基于python。

最新更新