11 years, 3 months ago.

Help with Ethernet

I'm reasonably new to understanding ethernet and am struggling to determine which libraries I do and don't need.

My application is very straight forward - I would like to use the mbed to interface to a legacy power supply which uses RS232.

The mbed will have a static IP, and the to start with I'm thinking the first byte of the ethernet frame (data packet) will be used as the byte that will be sent to the legacy item via RS232.

I appreciate this may not be the most efficient way of using ethernet - but I'm trying to use this project to build up my understanding of ethernet etc..

Any pointers would be appreciated.

2 Answers

11 years, 3 months ago.

What you are asking is not going to work. Ethernet has a strict set of standards. Within the payload of an Ethernet packet, you can extract and inject your own data structures, but you cannot use the first byte of a packet to send to your RS232 interface. There are many Ethernet examples within the community.

Try this first: http://mbed.org/users/rolf/code/ethersniff_hex/ It's a simple packet sniffer that displays the content of each packet received from your network on your local PC. From there, both EthnetNetIf and EthernetInterface both are widely used with the mbed group. Both drivers can use DHCP to static IP.

As you develop your Ethernet strategy, I would recommend downloading an Ethernet protocol sniffer like Wireshark. It is great at desecting packets, protocols, filtering, etc.

...kevin

11 years, 3 months ago.

Being also new to this I have chosen to build on the (fewer) examples using the new mbed Rtos

Import programTCPSocket_HelloWorld

TCP Socket Hello World with Ethernet

and some reading around the other very helpful posts based on this.

I have set up a visual C# client on windows to talk with an mbed running the below code. Note that the string "buffer" can contain most anything you like just watch our for it's length and portability - integers, sent this way, can come out not as expected (look up "htons"). Floating points and doubles need some kind of serialisation - not necessarily trivial.

You should be able to "ping" this fixed IP as soon as the mbed has booted up which results in a warm fuzzy feeling.

#include "mbed.h"
#include "EthernetInterface.h"
#include "NokiaLCD.h"

NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
DigitalOut led1(LED1, "led1");
DigitalOut led2(LED2, "led2");
DigitalOut led3(LED3, "led3");
DigitalOut led4(LED4, "led4");

u32_t ethIp = ipaddr_addr("198.168.1.20");              //Proper container for the fixed IP
EthernetInterface eth; //, ethMask, ethNull, ethNull);  //The ethernet object
  
int main() {
    lcd.printf("Running Program");  
    eth.init("198.168.1.20","255.255.255.0","0.0.0.0");
    eth.connect();
    lcd.locate(0,0);
    lcd.printf("IP Address is %s\n", eth.getIPAddress());
    
    TCPSocketServer sock;
    //int sock.bind(80);
    if (sock.bind(80)>=0){
    lcd.printf("Bound");
    led4=1;}
        
    sock.listen(1);
    
    /** Accept a new connection.
    \param connection A TCPSocketConnection instance that will handle the incoming connection.
    \return 0 on success, -1 on failure.
    */
    TCPSocketConnection client;    
    while (!client.is_connected()){    
    sock.accept(client);
    }    
    led3 = 1;
  
    char buffer[300];
    char data[300];        
    int count = 0;
    int ret;
    int out;
    while(true){    
        ret = client.receive(buffer, sizeof(buffer)-1);             
        led2=1;
        buffer[ret] = '\0';
        //ret will be -1 if fails, 0 if it gets something from the client.
        if (ret >=0){        
          lcd.cls();
          lcd.printf(buffer);          
          sprintf(data, "count %d", count);
          client.send(data, sizeof(data));  //reply to the client with a count 
          count = count + 1;
          ret = -1;        
        }        
    }