制作一个不会阻止 ESP8266/Arduino 的 UDP 类 [英] Making a UDP class which doesn't block an ESP8266 / Arduino

查看:94
本文介绍了制作一个不会阻止 ESP8266/Arduino 的 UDP 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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包,然后调用一个send响应方法,不用在主程序循环中放入大量代码?

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() 我有两个问题:

If I initalise and call my DiscoverMe object , and call listenForPacket() I have 2 questions:

  1. 如何使 UDP.begin()(将在 listenForPacket() 方法中)不被阻塞,从而允许我的程序到达它的 loop()?
  2. 如果我的程序到达它的循环,DiscoverMe 侦听器是否会继续无限地侦听,如果没有,我该如何让它表现得如此?我想我在问,一旦 loop() 被击中,类行为是否被忽略,或者它们是否在单独的线程中运行?
  1. How do I make the UDP.begin()(which will be in the listenForPacket() method) not block, allowing my program to reach its loop()?
  2. 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, once loop() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆