制作不阻塞ESP8266/Arduino的UDP类 [英] Making a UDP class which doesn't block an ESP8266 / Arduino
问题描述
我需要在Arduino兼容项目中侦听ESP8266上的特定UDP端口并在收到适当消息时做出响应,同时在主程序循环中执行其他应用程序工作.
I have a need in my Arduino compatible project to listen on an ESP8266 to a specific UDP port and respond when an appropriate message is received, whilst doing other application stuff in the main program loop.
我想将UDP内容抽象到自己的类中,这就是我的问题所在.
I want to abstract the UDP stuff into its own class, and this is where my question comes.
如何让我的班级继续听,阅读一个UDP数据包,然后调用发送响应方法,而无需在主程序循环中放入大量代码?
How do I let my class continue to listen, read a UDP packet, and then call a send response method, without putting lots of code into the main program loop?
我的课程的接口是:
#ifndef Discover_Me_h
#define Discover_Me_h
#include "Arduino.h"
class DiscoverMe
{
public:
DiscoverMe(); //Constructor
listenForPacket();// listens for packet, if one arrives it calls respond()
respond();//Responds to the host which sent the packet with some data
};
#endif
主程序具有:
#include "DiscoverMe.h"
include "Arduino.h"
DiscoverMe dm;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
dm.listenForPacket();
}
void loop() {
// I WANT MY DiscoverMe class to still work when my program gets here
int switchVar = 1;
digitalWrite(ledPin, switchVar);
delay(200);
if (switchVar == 1) {
switchVar = 0;
} else {
switchVar = 1;
}
}
如果我初始化并调用DiscoverMe
对象,然后调用listenForPacket()
,我有2个问题:
If I initalise and call my DiscoverMe
object , and call listenForPacket()
I have 2 questions:
- 如何不阻塞
UDP.begin()
(将在listenForPacket()方法中使用),以使程序到达其loop()
? - 如果我的程序到达了它的循环,
DiscoverMe
侦听器将继续无限侦听,如果没有,我如何使它表现得如此?我想我想问的是,一旦loop()
被击中,这些类的行为是否会被忽略,或者它们是否在单独的线程中运行?
- How do I make the
UDP.begin()
(which will be in the listenForPacket() method) not block, allowing my program to reach itsloop()
? - If my program reaches it's loop, will the
DiscoverMe
listener continue to listen infinitely, if no, how do I make it behave as such? I guess I am asking, onceloop()
is hit, are the classes behaviours ignored, or do they run in separate threads?
推荐答案
答案与@maximilian相同:
The answer was as per @maximilian:
在循环中,您将必须每次检查是否有数据包到达.这就是UDP库的工作方式(if(Udp.parsePacket()> 0)).
In the loop, you will have to check if a packet has arrived every time. That's how the UDP library works (if(Udp.parsePacket() > 0)).
从设计角度来看,最好创建一个DiscoverMe :: HandlePacket函数,该函数将检查数据包是否可用,然后对其进行操作,并在每次循环迭代中调用它.在设置功能中,您只能将UDP客户端绑定到特定端口. Udp.begin()仍然不会阻止.
Design-wise, it would be best to make a DiscoverMe::HandlePacket function which will check if a packet is available, then act on it, and is called in every loop iteration. In the setup function, you may only bind your UDP client to a specific port. Udp.begin() will not block anyways.
这已实施,我可以确认它是否有效.
This was implemented and I can confirm it works.
长话短说,图书馆似乎需要一种称为的方法.在主程序循环中,使该类与基础接触,看看它是否需要做任何工作.
Long story short, libraries seem to require a method which will be called . during the main program loop, to touch base with the class and see if it needs to do any work.
这篇关于制作不阻塞ESP8266/Arduino的UDP类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!