I don't remember what I added to the program to cause it to stop working on the Uno, but earlier, simpler versions of the program worked, the latest breaks the networking (No ping or TCP/IP connections). Note, the card indicates ethernet hardware activity.
Here's the code:
#include <SPI.h>
#include <Ethernet.h>
#include <stdio.h>
float aatof(char* str);
char IDN_resp[] = "DGH Bias/Filament Supply";
char CMD_buf[64], RESP_buf[64], CMD[4][25], buf_c, f_str[16];
int i, j, n;
float volts;
// Power Supply configuration
struct {
char Name[16];
int EnablePin;
int SetPin;
float SetScale;
int SensorPin;
float SensorScale;
float SetValue;
float PValue;
} PS[] = {{"N48V", -1, 5, 256/50, A0, 1.0, 15.0, 0.0}, {"N8V", 4, 6, 256/7.5, A1, 1.0, 6.3, 0.0}};
int nSupplies = 2, selSupply = 0;
// network configuration. gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
byte ip[] = { 192, 168, 1, 41 };
// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };
// use port 40, but you may use telnet port 23
EthernetServer server = EthernetServer(40);
void setup()
{
// set pins as PWM outputs
for (i=0; i<nSupplies; i++) { pinMode(PS[i].SetPin, OUTPUT); }
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
i = 0;
while ((buf_c = client.read()) != -1 ) {CMD_buf[i++] = toupper(buf_c);}
CMD_buf[i++] = 0;
if (strncmp(CMD_buf, "*IDN?", 5) == 0) {client.println(IDN_resp); goto bailout;}
if (sscanf(CMD_buf, "%[^:\ ]:", CMD[0]) <0) client.println("Command not found\n");
else if (strncmp(CMD[0], "INST", 4) == 0) {
if (sscanf(CMD_buf, "%*[^:\ ] :%[^:\n]", CMD[1]) <0) client.println("Command not found\n");
if (strncmp(CMD[1], "NSEL", 4) == 0) {
if (sscanf(CMD[1], "NSEL%s\n", CMD[2]) >0) {
switch (CMD[2][0]) {
case '?': sprintf(RESP_buf, "%d\n", selSupply+1); client.println(RESP_buf); break;
case '1':
case '2': selSupply = CMD[2][0] - 49; break;
default: sprintf(RESP_buf, "bad command\n"); client.println(RESP_buf); break;
}
goto bailout;
}
}
if (strncmp(CMD[1], "SEL", 3) == 0) {
if (sscanf(CMD[1], "SEL%s\n", CMD[2]) >0) {
switch (CMD[2][0]) {
case '?': sprintf(RESP_buf, "%s\n", PS[selSupply].Name); client.println(RESP_buf); break;
default:
j = 1;
for (i=0; i<nSupplies; i++) {if (strcmp(CMD[2], PS[i].Name) == 0) {selSupply = i; j = 0; break;}}
if (j) {sprintf(RESP_buf, "bad command\n"); client.println(RESP_buf); break;}
}
goto bailout;
}
}
sprintf(RESP_buf, "CMD[0] = %s, CMD[1]=%s\n", CMD[0], CMD[1]); client.println(RESP_buf);
goto bailout;
}
else if (strncmp(CMD[0], "VOLT", 4) == 0) {
if (sscanf(CMD_buf, "%*[^:\ ] :%[^:\n]", CMD[1]) <0) client.println("Command not found\n");
if (strncmp(CMD[1], "TRIG", 4) == 0) {
if (sscanf(CMD[1], "TRIG %[.0-9]\n", f_str) >=0) {PS[selSupply].SetValue = aatof(f_str); goto bailout;}
}
if (strncmp(CMD[1], "IMM", 3) == 0) {
if (sscanf(CMD[1], "IMM %[.0-9]\n", f_str) >0) {PS[selSupply].SetValue = aatof(f_str); dtostrf(PS[selSupply].SetValue, 4, 2, f_str);
sprintf(RESP_buf, "%s PS, %s V\n", PS[selSupply].Name, f_str); client.println(RESP_buf); goto bailout;}
}
sprintf(RESP_buf, "VOLT CMD[0] = %s, CMD[1]=%s\n", CMD[0], CMD[1]); client.println(RESP_buf);
goto bailout;
}
if (strlen(CMD_buf) > 0) client.println("Command not found\n");
bailout: CMD_buf[0] = 0; RESP_buf[0] = 0;
}
for (i=0; i<nSupplies; i++) {analogWrite(PS[i].SetPin, PS[i].SetScale*PS[i].SetValue);}
delay(100);
for (i=0; i<nSupplies; i++) {PS[i].PValue = analogRead(PS[i].SensorPin)*1.5;}
delay(100);
}
float aatof(char* str) {
char fract[24];
int i, j, fraction;
sscanf(str, "%d.%s", &i, fract);
sscanf(fract, "%d", &fraction);
return (float) i + (float) fraction/pow(10, strlen(fract));
}