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



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

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 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)上没有发生任何事情

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

上一页:不确定您使用的是哪种以太网屏蔽/板,但通常它们使用SPI协议通过引脚10-13进行通信。引脚12用于MISO。(这是主输入,从输出——所以它被以太网设备(从)用作Aurduino的输入(主)的输出。因此,引脚1到9应该可以用作LED指示灯。

JDH提供的信息是正确的。以太网/SPI使用多个引脚与以太网屏蔽进行通信。其余的免费供您使用。看见http://arduino.cc/en/Reference/SPI了解一些细节。连接部分显示了几种常见的Arduino板使用的引脚。对于Uno和Duemilanove来说,这是10胜13负。

最新更新