gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:27:13 25 4
gpt4 key购买 nike

我有一个 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 代码中是否可以只发送一次数据(即模式,快速)并使 arduino 以无限的方式执行模式而无需再次通过 serial.write 发送数据 命令。因此避免了 ser.isOpen() 之后的 while(1) 循环。

最佳答案

看看这个:

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

这是我在 Arduino 方面联合起来发出任意命令,例如将引脚切换为高电平/低电平和设置 PWM 电平等。它可以在串行和网络上工作,尽管它目前在网络端是一个触摸错误。

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

另请参阅:https://github.com/ajfisher/arduino-django-visualiser这是我使用这个库的一个变体来控制一些基于 Django 中发生的事情的 LED 的地方——它更多地基于 Python。

关于python - 使用 pyserial 控制 Arduino Uno 板上的特定引脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7288700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com