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

发布于 2024-12-03 03:19:50 字数 2963 浏览 1 评论 0原文

我有一个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)有没有办法能够激活“中断 ISR”取决于引脚(在本例中为引脚 2,即 INT0 引脚)的条件,而不使用引脚上存在的物理开关。因此,引脚状态必须由软件操纵。

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

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

I have a python code that sends in a pattern, in which a light has to blink in. (say eg. 101010. pattern may vary every time the code is run). when it is executing this infinitely i want an interrupt( again sent by the python code )to save the present conditions of the lights (say it is running 1 of the sequence) and perform a specific task like turn off the lights for 10 seconds and then resume the sequence.
one way of doing this is by interrupting the program by making the interrupt pin high. The question is can this making of high/low controlled by the pyserial.
So a simple pseudo code would be :

PYTHON part of the code:

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 part of the code :

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.
 }

FOR A BETTER UNDERSTANDING :

I built up small codes to show the doubts i had :

CODE FOR THE ARDUINO IS :

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.***

}

*/

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

CODE FOR THE PYTHON FRONT IS :

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"

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

Now the questions I had :

1) Is there a way to be able to activate the "blackout ISR" depending on the conditions of a pin(in this case pin2 which is the INT0 pin) without using a physical switch present on the pin. Hence the pin state has to be manipulated by the software.

2) Is it possible to perform the operations as mentioned in the comments of the blackout functions?

3) In the python code is it possible to just send in the data(i.e. patterns,speedy) only once and make the arduino perform the pattern in a infinite way without again sending the data by the serial.write command. Hence avoiding the while(1) loop after the ser.isOpen().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这样的小城市 2024-12-10 03:19:50

看看这个:

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

这是我在 Arduino 端整合起来的东西,用于发出任意命令,例如切换引脚高/低并设置 PWM 电平等。它可以通过串行和网络工作,尽管目前在网络端还存在触摸问题。

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

等看一下:https://github.com/ajfisher/arduino-django-visualiser 这是我使用这个库的变体来根据 Django 中发生的一些事情来控制一些 LED - 它更多很大程度上基于Python。

Have a look at this:

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

It's something I pulled together on the Arduino side to issue arbitrary commands like switch a pin high / low and set PWM levels etc. It works over both serial and network though it's a touch buggy on the network side at the moment.

To use it, put the code on your arduino then you just write a python script (or any other language that can use a serial connection) to connect over the serial connection and then tell it what you want to do eg DIGW 1 HIGH etc

Also have a look at: https://github.com/ajfisher/arduino-django-visualiser which is where I use a variation of this library to control some LEDs based on some things going on in Django - it's more heavily python based.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文