Hello everyone!
I'm currently using Keil uVision 5 in order to program an UDP interface in order to communicate with another machine on my local network.
While the reception of messages via the configured UDP Socket work, the sending of messages does not appear to work, as I recieve a "netError" value on the netStatus value that the netUDP_Send() function returns. Based on the reference, this error simply states that the sending failed. I was not able to find any explanation as to why I am getting this error. Do you have any ideas or suggestions?
Here is a sample of the code I use to recieve and send the UDP messages (I am trying to send form the local 10000 UDP Port, to my computer's 61345 UDP port, whose IP adress is 192.168.1.72 :
NET_ADDR addr = { NET_ADDR_IP4, 61345, 192, 168, 1, 72}; int8_t start(void){ int32_t socket = netUDP_GetSocket(udp_receive_callback); if(socket >= 0){ //Le socket est bien alloué if(netUDP_Open(socket,10000) == netOK){ uint8_t *sendbuf; sendbuf = netUDP_GetBuffer(1); sendbuf[0]=0x65; netStatus status = netUDP_Send(socket,&addr,sendbuf,1); if(status == netOK) return 1; else{ return -1; } } else return -1; } return -1; }
In case of IPv4, netUDP_Send() will return netError, when it fails during the construction of IPv4 header.
Follow this webpage to enable the Network debug output, which will help you to have more insight on the root cause:
http://www.keil.com/support/docs/3982.htm
You might try
NET_ADDR4 addr = { NET_ADDR_IP4, 61345, 192, 168, 1, 72};
NET_ADDR addr = { NET_ADDR_IP4, 61345, 192, 168, 1, 72};
Thanks for the suggestion, but it didn't change much.
Thank you very much, showing the debug output was very useful! THanks to it I discovered that the state of the network Component was at "link-down", and by trying to send a first UDP message, the state changed to "link-up". My solution to the problem for now is to try to send the udp message while the netStatus returned is not netOK (so that as soon as the link gets up, the message gets sent).
You could wait until you get a valid IP address assigned after the link is up.
Try the following:
uint32_t addr; netInitialize(); do { osDelay(500U); netIF_GetOption(NET_IF_CLASS_ETH | 0, netIF_OptionIP4_Address, (uint8_t *)&addr, sizeof (addr)); } while (addr == 0U);