6 years, 9 months ago.

Can someone please explain this section of code

EthernetInterface eth;
TCPSocketConnection socket;
 
typedef struct packetToSent_s {
uint16_t data[80]; 
} packetToSend_t;
 
packetToSend_t dataOut;
 
int main()
{
    eth.init("192.168.1.4","255.255.255.0","192.168.1.1");  // Static IP of 192.168.1.4
    eth.connect();
 
    socket.connect("192.168.1.5", 1001)  // connect to the PC at the given IP address and socket 
 
 
    socket.send_all((char*)&dataOut,sizeof(packetToSend_t)); // send the data
}

Retrieved from https://developer.mbed.org/questions/5238/Ethernet-for-high-speed-data-transfer-NO/ credit to Andy A.

Where do I define the data that I want to send from my mbed to the server? I'm trying to define dataOut since socket.send_all function sends the data contained in the address of dataOut pointed to.. yet I get compilation errors.

Please help.

J

Hi Joe, in order to assist you better can you please edit your post and place "<<code>> <</code>>" tags around your code excerpt? Thanks!

posted by Jenny Plunkett 11 Jul 2017

Thank you Jenny did not know that was how to do it.

posted by Joe Danlloh 11 Jul 2017

I'm also having an issue when using DHCP that the eth.getIPAddress() function is not printing anything to my terminal, just appears blank and then continues to run the code

posted by Joe Danlloh 11 Jul 2017

1 Answer

6 years, 9 months ago.

Here's a more annotated version of the code. This was originally posted as a basic framework to get data sent rather than a full working application.

Also keep in mind this is mbed2 code, mbed5 changes some of the function calls slightly.

// the ethernet and socket objects
EthernetInterface eth;
TCPSocketConnection socket;
 
// A data structure of the data to send
// In this case it's fairly pointless, you may as well just use a uint16_t[] directly in place of this
// But if you package what you want into a structure then you don't need to change the 
// transmission code if you change what is being sent, you just change the structure definition.
typedef struct packetToSent_s {
uint16_t data[80]; 
} packetToSend_t;

// an instance of the packet to send.
packetToSend_t dataOut;
 
int main()
{
   // initialize the ethernet interface to a static IP address
    eth.init("192.168.1.4","255.255.255.0","192.168.1.1");  // Static IP of 192.168.1.4

   // connect. Ideally you should check at this point that the connect succeeded.
    eth.connect();
 
   // open a network socket. to port 1001 on IP address 192.168.1.5
   // it is assumed that a TCP server is listening for connections on that address/port
    socket.connect("192.168.1.5", 1001) 
  
   // at this point you should really check that the socket opened OK. 
 
   // you should now set the data to send. e.g.
   for (int i=0;i<80;i++)
     dataOut.data[i]=i*5;

   // send the data packet.
    socket.send_all((char*)&dataOut,sizeof(packetToSend_t));

   // ideally we should also check the return value from send_all to verify that there weren't any errors.
}

Accepted Answer

Thanks so much Andy. It's the struct part that I didn't quite understand. You've clarified perfectly.

posted by Joe Danlloh 12 Jul 2017

Hi Andy, having an issue with the code. Where you comment you should now set the data to send. e.g....

No matter what I place in this section, the output in Hercules is always the same.

"NVT: FF FB 18 NVT: FF FD 03 NVT: FF FB 03 NVT: FF FD 01 NVT: FF FB 1F NVT: FF FE 18 NVT: FF FC 01 NVT: FF FD 1F NVT: FF FA 1F 00 50 00 18 FF F0 NVT: FF FA 1F 00 50 00 50 FF F0"

I'm slightly confused. I notice you send &dataOut, so is this just the memory location of the data we want to send? Is that the same as sending the actual data? Sorry if I sound a bit daft.

J

posted by Joe Danlloh 12 Jul 2017

Yes, you pass a pointer to the start of the data to send and the size of the data rather than passing the actual data. This is standard practice when passing a flexible amount of data. This means that when calling the function only a pointer has to be copied to the stack rather than the entire data structure, it's both faster and more memory efficient.

I'm not sure why you're not getting the expected data, that or similar code has worked for me in the past. All I can suggest is checking the return values from the various network functions to see if any of them are giving you an error code.

posted by Andy A 12 Jul 2017

It is sending the expected data as addresses, but even if I increase the size of uint16_t[80] to something larger, it still prints out the same result to Hercules, indicating that the data is occupying the same amount of space in memory, which just can't be true no? Also, so it doesn't matter whether you transmit the actual data or just the memory addresses? Do they not need dereferencing?

EDIT: Turns out the hexadecimal values I see printed to my screen in Hercules do not change as they indicate TelNet commands... whatever that means :) apologies

posted by Joe Danlloh 12 Jul 2017

Okay Andy, I've had an absolute disaster. It turns out I have been connecting my TeraTerm to Hercules and communicating between those two, thinking that I was talking to my mbed, as I used the same socket and IP address thinking this was correct. Removing Tera Term, with this code, and listening for a socket connection on Hercules from the mbed and NOT from Tera Term.. I get nothing. It won't even say connected.

:(

posted by Joe Danlloh 12 Jul 2017

Does it matter that I'm using a network bridge on my laptop to University wifi at all?

posted by Joe Danlloh 12 Jul 2017

First off, check the physical connection:

Look at the socket at the other end of your Ethernet cable, are the connection lights on indicating a physical layer link? If not then forget looking at the code, you need to get the wiring correct first.

It looks like you are using an LPC1768. Is the Ethernet socket connected to the board correctly? Are you using a mag jack or separate magnetics? You need one or the other, you can't just use a dumb RJ45 socket and nothing else. Technically all of those connections should be impedance controlled and matched pairs but without that it should still work fine as long as you keep the wires short.

If you are going straight from the mbed to the PC you may need a crossover cable, if you are going to a hub a normal cable will do.

If you have a physical link are the IP addresses, subnet mask and gateway in the mbed code correct for your network? Or if you're using DHCP are you getting an IP address?

What are the return values or eth.connect() and socket.connect()?

posted by Andy A 12 Jul 2017

Okay, I took the mbed home with me and used a direct connection to my router instead of the network bridge and it worked. I now get the information I send, so the code is fine. Thank you. So the mbed has to have a MagJack? It can't just use a standard RJ45 connector like this? http://eu.mouser.com/ProductDetail/Amphenol-Commercial-Products/RJE08-1AA-0410H/?qs=sGAEpiMZZMvQhAhQbXdbBtGY70GmnRNRnDtfGxlU%2Foo%3D

posted by Joe Danlloh 13 Jul 2017

I'm not saying it would never work without a magJack or separate magnetics but everything will be designed with the assumption that they will be there. I wouldn't trust it to work reliably or over any distance without one.

posted by Andy A 13 Jul 2017