Thanks. Okay sorry I should have spotted something earlier. During compilation we have to automatically add any missing c++ prototypes. For example if you add a method called void test(){} we have to add the void test(); prototype if you have not already done that. This happens in the temp build folder, does not alter your own code.
We are about to release a version of visual micro that determines a better location in the .ino code where auto generated prototypes should be inserted. Currently missing prototypes are inserted before the 1st line of actual code. The Arduino IDE has already moved to the new way of working but it's not 100% reliable (see note at end of this post). I think it is the one difference we have right now but easily solved.
In your case you have added a line of code ( "int xyz = 1;") before you have #included the libraries you want to use, well at least before the Ethernet library is #included. A "type" from the Enter lib is used in a method signature, that required a prototype.
In your example the prototypes are added before int "xyz = 1;", therefore I have moved that line of code down so that the eter lib "#include" is before it. Then missing prototypes are added after the enter lib is available to the code.
Solution 1 //#include <net.h>
// Demonstrates usage of the new udpServer feature.
// You can register the same function to multiple ports,
// and multiple functions to the same port.
//
// 2013-4-7 Brian Lee <cybexsoft@hotmail.com>
//
// License: GPLv2
#include <EtherCard.h>
#include <IPAddress.h>
int xyz = 1;
#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
An alternative is to add the missing prototype(s) yourself. (Then the code will continue to work if you ever move it into .cpp files)
You can see below your code is unchanged except a prototype has been added after the enter lib has been included, because the method used a "type" from the lib.
Solution 2
//#include <net.h>
int xyz = 1;
// Demonstrates usage of the new udpServer feature.
// You can register the same function to multiple ports,
// and multiple functions to the same port.
//
// 2013-4-7 Brian Lee <cybexsoft@hotmail.com>
//
// License: GPLv2
#include <EtherCard.h>
#include <IPAddress.h>
// adding c++ prototypes when types are available
//
void udpSerialPrint(uint16_t dest_port, uint8_t src_ip[IP_LEN], uint16_t src_port, const char *data, uint16_t len);
#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,0,200 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };
#endif
// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
//callback that prints received packets to the serial port
void udpSerialPrint(uint16_t dest_port, uint8_t src_ip[IP_LEN], uint16_t src_port, const char *data, uint16_t len){
IPAddress src(src_ip[0],src_ip[1],src_ip[2],src_ip[3]);
Serial.print("dest_port: ");
Serial.println(dest_port);
Serial.print("src_port: ");
Serial.println(src_port);
Serial.print("src_port: ");
ether.printIp(src_ip);
Serial.println("data: ");
Serial.println(data);
}
void setup(){
Serial.begin(57600);
Serial.println(F("\n[backSoon]"));
// Change 'SS' to your Slave Select pin, if you arn't using the default pin
if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
Serial.println(F("Failed to access Ethernet controller"));
#if STATIC
ether.staticSetup(myip, gwip);
#else
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
#endif
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
//register udpSerialPrint() to port 1337
ether.udpServerListenOnPort(&udpSerialPrint, 1337);
//register udpSerialPrint() to port 42.
ether.udpServerListenOnPort(&udpSerialPrint, 42);
}
void loop(){
//this must be called for ethercard functions to work.
ether.packetLoop(ether.packetReceive());
}
/*
//Processing sketch to send test UDP packets.
import hypermedia.net.*;
UDP udp; // define the UDP object
void setup() {
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
//udp.log( true ); // <-- printout the connection activity
udp.listen( true ); // and wait for incoming message
}
void draw()
{
}
void keyPressed() {
String ip = "192.168.0.200"; // the remote IP address
int port = 1337; // the destination port
udp.send("Greetings via UDP!", ip, port ); // the message to send
}
void receive( byte[] data ) { // <-- default handler
//void receive( byte[] data, String ip, int port ) { // <-- extended handler
for(int i=0; i < data.length; i++)
print(char(data[i]));
println();
}
*/
Final Important Note The Arduino IDE (and shortly visual micro), includes prototypes before the first valid method in the code. This will still leave room for "types" from different #includes to be used in method signatures at the wrong time. This is more likely if if the #includes in .ino code are added in an ad-hoc order.