与IDE捆绑在一起的UDPSendReceive.pde示例可以正常工作,并在接收UDP数据包时在串行监视器上显示正确的输出,但似乎禁用所有pinOutputs?
#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)
答案 0 :(得分:0)
更新 - 注意到SPI包含在您的代码中.SPI库使用引脚12(用于MOSI),因此它已经保留。
上一个: 不确定您使用的是哪个以太网屏蔽/电路板,但通常它们使用SPI协议通过引脚10-13进行通信。引脚12用于MISO。 (那是Master In,Slave Out - 所以它被以太网设备(Slave)用作输出到Aurduino的(Master)。 所以引脚1至9应该可以用作LED指示灯。
答案 1 :(得分:0)
JDH提供的信息是正确的。以太网/ SPI使用多个引脚与以太网屏蔽通信。其余的免费供您使用。有关详细信息,请参阅http://arduino.cc/en/Reference/SPI。 Connections部分显示了几个常见Arduino板使用的引脚。对于Uno和Duemilanove,它的引脚是10-13。