Arduino 以太网 UDPSendReceive 示例禁用所有引脚输出?

发布于 2025-01-02 10:59:47 字数 2281 浏览 1 评论 0原文

与 IDE 捆绑在一起的 UDPSendReceive.pde 示例开箱即用,并且在收到 UDP 数据包时在串行监视器上显示正确的输出,但似乎禁用了所有 pinOutputs?

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: [email protected] 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
    pinMode(12, OUTPUT);  
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();

    digitalWrite(12, HIGH);   // set the LED on
  }
  delay(10);
}

即使只是将循环更改为

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
       digitalWrite(12, HIGH);
  }

意味着我的输出上没有任何反应(在本例中是 LED)

The UDPSendReceive.pde example bundled with the IDE works fine out of the box and displays the correct output on the serial monitor when it receives UDP packets, but seems to disable all the pinOutputs?

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: [email protected] 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
    pinMode(12, OUTPUT);  
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();

    digitalWrite(12, HIGH);   // set the LED on
  }
  delay(10);
}

Even just changing the loop to be

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
       digitalWrite(12, HIGH);
  }

means nothing happens on my outputs (in this case an LED)

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

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

发布评论

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

评论(2

撩动你心 2025-01-09 10:59:47

更新 - 刚刚注意到 SPI 包含在您的代码中。SPI 库使用引脚 12(用于 MOSI),因此它已被保留。

以前的:
不确定您使用的是哪个以太网扩展板/板,但通常它们使用 SPI 协议通过引脚 10-13 进行通信。引脚 12 用于 MISO。 (即主输入,从输出 - 因此以太网设备(从设备)将其用作 Aurduino 的输入(主设备)的输出。
因此,引脚 1 至 9 应该可以用作 LED 指示灯。

Update - Just noticed the SPI include in your code.The SPI library uses pin 12 (for MOSI) so its already reserved.

Previous:
Not sure which Ethernet shield/board you are using, but typically they use SPI protocol to communicate over pins 10-13. Pin 12 is used for MISO. (That's Master In, Slave Out - so its used by the Ethernet device (Slave) as output to the Aurduino's in (Master).
So pins 1 thru 9 should be fine to use as an LED indicator.

天暗了我发光 2025-01-09 10:59:47

JDH提供的信息是正确的。以太网/SPI 使用多个引脚与以太网扩展板进行通信。其余的免费供您使用。有关详细信息,请参阅 http://arduino.cc/en/Reference/SPI。连接部分显示了几种常见 Arduino 板使用的引脚。对于 Uno 和 Duemilanove,其引脚为 10-13。

The information provided by JDH is correct. Ethernet/SPI uses several pins to communicate with the Ethernet shield. The rest are free for your use. See http://arduino.cc/en/Reference/SPI for some details. The Connections section shows what pins are used by several common Arduino boards. For the Uno and Duemilanove it's pins 10-13.

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