string for Ipaddr()

17 May 2011

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:

#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);
    }
    
    char ip [512];
    char subnetmask [512];
    char gateway [512];
    char dns [512];
             
    fscanf(fp, "%s", ip); //contains: 192.168.2.102
    fscanf(fp, "%s", subnetmask); //contains: 255.255.255.0
    fscanf(fp, "%s", gateway); //contains: 192.168.2.101
    fscanf(fp, "%s", dns); //contains: 192.168.2.101
      
    fclose(fp); 
    
    EthernetNetIf eth( //instead of these standard values, it should be the data in the variables.
        IpAddr(192,168,0,101), //IP Address
        IpAddr(255,255,255,0), //Network Mask
        IpAddr(192,168,0,1), //Gateway
        IpAddr(192,168,0,1)  //DNS
    );
        
    printf("Setting up ethernet...\r\n");
    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; 
}

How do i do this conversion? Thanks!

17 May 2011

Just a guess (pseudo code):-

    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]); //contains: 192.168.2.102
    fscanf(fp, "%d.%d.%d.%d\n", subnetmask[3], subnetmask[2], subnetmask[1], subnetmask[0]); //contains: 255.255.255.0
    fscanf(fp, "%d.%d.%d.%d\n", gateway[3], gateway[2], gateway[1], gateway[0]); //contains: 192.168.2.101
    fscanf(fp, "%d.%d.%d.%d\n", dns[3], dns[2], dns[1], dns[0]); //contains: 192.168.2.101

    EthernetNetIf eth( //instead of these standard values, it should be the data in the variables.
        IpAddr(ip[3], ip[2], ip[1], ip[0]), //IP Address
        //... and on, got bored of typing here but I'm sure you get the idea! :)
    );


17 May 2011

This doesn't seem to work.. It doesn't compile an error, but when executing this, you will lose the USB connection with the mbed.

So, i've put just a number in the textfile to try reading decimals. Using this code, i got the same result... It seems you can't read decimals directly from a textfile?

int test;
    
printf("Reading Data...\n");    

fscanf(fp, "%d",test);

printf ("I have read: %d \n", test);

/UPDATE: I've found some information and managed to convert a char array to an integer. Still got only a number in the ip.txt file when this works fine:

char read[512];
int test;
    
printf("Reading Data...\n");    
fscanf(fp, "%s",read);
   
test = atoi(read);
printf ("I have read: and %d \n", test);

But, this still doens't fix my problem, because when i read the ip, i read the entire 192.168.0.101, witch afcourse can't be translated to an integer. Any idea's to read just until the first "."? Or maybe split the char array? (i know in vb you can use string.split command...)

17 May 2011

Hi Rob, your problem is that you need to use pointers for the destination of the fscanf:

Use: fscanf(fp, "%d.%d.%d.%d\n", &ip[3], &ip[2], &ip[1], &ip[0]); contains: 192.168.2.102

Instead of fscanf(fp, "%d.%d.%d.%d\n", ip[3], ip[2], ip[1], ip[0]); contains: 192.168.2.102

You use the results like this: printf ("I have read IP = %d.%d.%d.%d \n\r", ip[3], ip[2], ip[1], ip[0]);

Note that your ''char'' destination IS in fact a pointer since you defined it as an array of 512 The "ip[3]" on the other hand is in fact an int and not a pointer. The & gives you the pointer to that int.

18 May 2011

Thanks a lot Wim. This works fine! (I still haven't got the hang of those pointers, but were getting there :-))

The entire code:

#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; 
}