Hi everyone. Playing around with EthernetNetIf with some success, but stumbled across something I cannot explain. I thought the recvfrom method of UDPSocket returned the number of bytes that have been received. A number of examples seem to treat it that way. However, when I check the value I get back when a packet containing 105 bytes is sent I find I am getting a return value of 1 every time. Can anyone explain why?
Is the value returned a byte count, or is it just a flag to indicate that data was received? i.e. 0 means failed, 1 means OK.
Here's the code. Have I made a stupid mistake somewhere?
/*
* SOURCE FILE : main.cpp
*
* Main program for a general purpose IO EtherBoard.
* Whenever it receives a message containing output states it updates the outputs,
* reads all inputs and transmits a reply containing the inputs.
*
* Adapted from the UDPSocketExample program by Donatien Garnier (6th August 2010).
* Uses Donatien Garnier's EthernetIf library.
*
* Richard Ellingworth. 20th February 2011.
*
*/
#include "mbed.h"
#include "EthernetNetIf.h"
#include "UDPSocket.h"
#include "Hexdump.h"
#include "EtherBoardPacket.h"
// Type number for this board.
#define BOARDTYPE ((ubyte)1) // 1 used to mean GPIO
// Ethernet connection object.
static EthernetNetIf eth(
IpAddr(172,27,12,99), //IP Address of the mbed (device running this code)
IpAddr(255,255,255,0), //Network Mask
IpAddr(172,27,12,253), //Gateway
IpAddr(172,27,12,253) //DNS
);
// UDP communications socket.
static UDPSocket udp;
// Serial port routed through the mbed's USB port back to the development PC.
// Used for debugging messages and so on.
static Serial pc( USBTX, USBRX );
/***************************************/
/* EVENT HANDLER FOR UDP SOCKET EVENTS */
/***************************************/
// Type of event is passed in e parameter.
static void onUDPSocketEvent(UDPSocketEvent e) {
switch(e) {
case UDPSOCKET_READABLE: //The only event for now
EtherBoardPacket rxPacket, txPacket( EBRXTYPE, BOARDTYPE );
Host host;
while( int len = udp.recvfrom( (char*)rxPacket.Bytes, ETHERBOARDPACKETLEN, &host ) > 0 ) {
pc.printf("From %d.%d.%d.%d: ", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3] );
Hexdump::Dump( pc, rxPacket.Bytes, ETHERBOARDPACKETLEN );
pc.printf( " Length=%d\r\n", len );
// Check packet length is correct and packet is valid.
if( ( len == ETHERBOARDPACKETLEN ) && rxPacket.IsValid( EBTXTYPE, BOARDTYPE ) ) {
pc.puts( "VALID\r\n" );
// Send a reply.
txPacket.UpdateChecksum();
udp.sendto( (char*)txPacket.Bytes, ETHERBOARDPACKETLEN, &host );
}
else {
pc.puts( "NOT VALID\r\n" );
}
}
break;
}
}
/****************/
/* MAIN PROGRAM */
/****************/
int main() {
// Setup serial port to development PC.
pc.baud( 38400 );
pc.format( 8, Serial::None, 1 );
pc.printf( "\r\nSetting up...\r\n" );
// Setup Ethernet link.
EthernetErr ethErr = eth.setup();
if( ethErr ) {
pc.printf( "Error %d in setup.\r\n", ethErr );
return -1;
}
pc.printf( "Setup OK\r\n" );
Host multicast( IpAddr(), 50000, NULL ); //Join multicast group on port 50000
// Set handler to be called when UDP socket event occurrs.
udp.setOnEvent(&onUDPSocketEvent);
udp.bind(multicast);
// Timer tmr;
// tmr.start();
while( true ) {
Net::poll();
/*
if(tmr.read() > 5) {
tmr.reset();
const char* str = "Hello world!";
udp.sendto( str, strlen(str), &multicast );
pc.printf("%s\r\n", str);
}
*/
}
}
and this is the output I get on the USB serial port thing. Note the text "Length=1" :
From 172.27.12.1: EB 54 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0
0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 BF FE Length=1
NOT VALID
From 172.27.12.1: EB 54 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0
0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 BF FE Length=1
NOT VALID
From 172.27.12.1: EB 54 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0
0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 BF FE Length=1
NOT VALID
I haven't bothered to put up the source code for the Hexdump or EtherBoardPacket modules, but I think it is fairly safe to say they are not the culprits.
Any thoughts anyone?
Hi everyone. Playing around with EthernetNetIf with some success, but stumbled across something I cannot explain. I thought the recvfrom method of UDPSocket returned the number of bytes that have been received. A number of examples seem to treat it that way. However, when I check the value I get back when a packet containing 105 bytes is sent I find I am getting a return value of 1 every time. Can anyone explain why?
Is the value returned a byte count, or is it just a flag to indicate that data was received? i.e. 0 means failed, 1 means OK.
Here's the code. Have I made a stupid mistake somewhere?
and this is the output I get on the USB serial port thing. Note the text "Length=1" :
I haven't bothered to put up the source code for the Hexdump or EtherBoardPacket modules, but I think it is fairly safe to say they are not the culprits.
Any thoughts anyone?