9 years, 5 months ago.

Ethernet for high speed data transfer, NOT for internet

Greetings! So here is my dilemma (and i will try to keep this exceptionally short and to the point) I would like to transfer 80 x 14 bit values from an mbed to a pc in less than 400 micro seconds, I have attempted to accomplish this using the serial (USBTX,USBRX) at the highest BAUD rate and was unsuccessful (I anticipated this by doing the math for the timing, but had to try out of principle). Following a bit of research I came to the conclusion that Ethernet had the data transfer rates that I would require to meet my objectives. I have no interest in using the ethernet to connect to the internet (as it seems most people are), I simply wish to use it for very very very simple data transfer. My initial thought was to implement a crossover cable directly to a PC, however there seems to be no support for that anywhere on the web (or at least none that I could find). The problem with all the solutions presented to this problem on various other threads is that they tend to not work. I have attempted to use a socket client hello world example (https://developer.mbed.org/handbook/Socket) working with no success (program connects to the IP, but no socket connection occurs). I am positive that my ethernet jack is wired correctly as I have tested it using a connection to a friends pc and was able to echo data from one to the other. However the mbed seems incapable of connecting. Does anyone have any suggestions of what direction I should look for very basic data transfer over ethernet? I am using Windows 8.... I am well aware that this may be the source of all my frustrations.

1 Answer

9 years, 5 months ago.

It should be fairly simple. Connecting to a local machine and connecting to the internet are identical other than not needing to do a DNS lookup to find out the remote IP address.

If you are using a crossover cable and a direct connection to the PC you need to use static IPs on both the mbed and the PC. If you are using DHCP then you can skip the IP address for the init() function but you still need the IP for the target PC.

The basic code is:

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
}

That has absolutely no error handling, most of the complexity in network code comes from dealing with lost connections and other error conditions neatly. If you don't mind it crashing if there is an error then you can skip a lot of that.

You will need to have a TCP socket server listening on the port specified on the target machine.

Accepted Answer

Thanks! This is by far the most straight forward and to the point answer I could have expected. I will employ it and see what happens :-)

posted by OPS HUD 13 Nov 2014

Success!!! For anyone looking to replicate these efforts, make sure you change line 18 to socket.send_all((char*)&dataOut,sizeof(packetToSend_t)); Additionally you will need Python code based off of the TCP socket server Python code at the following address https://wiki.python.org/moin/TcpCommunication . You will also have to set the IP ethernet connection. I used the video that was at the following link as I had never done it before. https://www.youtube.com/watch?v=rHQzHCQhHHg. Good Luck and Happy Hunting!

posted by OPS HUD 13 Nov 2014

Line 18 - oops.

Rather than messing around with python for a quick test and then not being sure where the problem is there is also this program: http://www.hw-group.com/products/hercules/index_en.html It can act as a TCP server when you need to test code out.

It also can act as a tcp client or serial terminal if needed.

posted by Andy A 14 Nov 2014

Could you explain how this sends data to HERCULES? I don't understand the struct, and when I run this program as is, no data seems to be transmitted when I set up TCP on hercules?

posted by Joe Danlloh 11 Jul 2017

You need to set HERCULES as a TCP server and tell it to listen on the correct port. You also need to change the destination address & port in the code to match the PC address. The data struct in the code is mainly a placeholder to show how the code works. The code above doesn't set the values of the data so you'll get meaningless gibberish as it stands (probably 80 bytes of 0x00). You need to define the correct data structure for your application and put suitable data into it if you want anything meaningful out of the other end.

posted by Andy A 11 Jul 2017