Thanks a lot Wim. This works fine!
(I still haven't got the hang of those pointers, but were getting there :-))
#include "mbed.h"
#include "EthernetNetIf.h"
LocalFileSystem local("local");
int main() {
printf("Opening File IP.txt...\n\r");
FILE *fp = fopen("/local/ip.txt", "r");
if(!fp) {
fprintf(stderr, "File /local/ip.txt could not be opened!\n\r");
exit(1);
}
int ip[4];
int subnetmask[4];
int gateway[4];
int dns[4];
fscanf(fp, "%d.%d.%d.%d\n", &ip[3], &ip[2], &ip[1], &ip[0]);
fscanf(fp, "%d.%d.%d.%d\n", &subnetmask[3], &subnetmask[2], &subnetmask[1], &subnetmask[0]);
fscanf(fp, "%d.%d.%d.%d\n", &gateway[3], &gateway[2], &gateway[1], &gateway[0]);
fscanf(fp, "%d.%d.%d.%d\n", &dns[3], &dns[2], &dns[1], &dns[0]);
fclose(fp);
printf("Setting up ethernet...\r\n");
EthernetNetIf eth(
IpAddr(ip[3], ip[2], ip[1], ip[0]), //IP Address
IpAddr(subnetmask[3], subnetmask[2], subnetmask[1], subnetmask[0]), //Network Mask
IpAddr(gateway[3], gateway[2], gateway[1], gateway[0]), //Gateway
IpAddr(dns[3], dns[2], dns[1], dns[0]) //DNS
);
//EthernetNetIf eth;
printf ("My IP: %d.%d.%d.%d\n\r", ip[3], ip[2], ip[1], ip[0]);
printf ("My subnetmask: %d.%d.%d.%d\n\r", subnetmask[3], subnetmask[2], subnetmask[1], subnetmask[0]);
printf ("My gateway: %d.%d.%d.%d\n\r", gateway[3], gateway[2], gateway[1], gateway[0]);
printf ("My DNS: %d.%d.%d.%d\n\r", dns[3], dns[2], dns[1], dns[0]);
EthernetErr ethErr = eth.setup();
if(ethErr){
printf("Error %d in setup.\n\r", ethErr);
return -1;
}
printf("Ethernet setup OK\r\n");
while(1){
}
return 0;
}
Hey
I'm currently trying to give the mbed a ip-address from a local file on the mbed. But since i do not know a lot of C or C++, i'm having a hard time solving this... The problem is converting the chararray to the 4 integers for the Ipaddr() function.
My code:
How do i do this conversion? Thanks!