在pyino板上使用serio专用的pin

2024-09-28 19:35:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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代码的一部分:

^{pr2}$

为了更好地理解:

我建立了一些小代码来表达我的疑虑:

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)是否有一种方法可以在不使用管脚上的物理开关的情况下,根据管脚(在本例中为pin2,即INT0管脚)的条件激活“blackout ISR”。因此,管脚状态必须由软件控制。在

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

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


Tags: oftheto代码serial模式arduinoser
1条回答
网友
1楼 · 发布于 2024-09-28 19:35:20

看看这个:

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

它是我在Arduino端联合起来发出任意命令的东西,如开关引脚的高/低和设置PWM电平等。它可以通过串行和网络工作,尽管它目前在网络端是一个触控错误。在

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

另外请看一下:https://github.com/ajfisher/arduino-django-visualiser这是我使用这个库的一个变体来根据Django中发生的一些事情来控制一些led的——它更基于python。在

相关问题 更多 >