Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 3 months ago.
UDPSocket and Wifly Interface Requires Blocking
I cannot seem to get the socket.set_blocking(false, timeout) to work when using the Wifly interface.
If I set blocking to true on the socket, then I can receive UDP datagrams. However, if I try to set blocking to false with anything less than 1000 ms timeout, I do not receive any datagrams. And if I set the timeout to 1000ms or more, the socket seems to block forever, or at least I cannot get other code in the loop to run.
Here is the basic test code I'm working with (note this is a rework of code that was using TCP instead of UDP and the code was working):
#include "mbed.h" #include "WiflyInterface.h" #include "FRamIO.h" #include "PinDetect.h" #include "Interpreter.h" #include <string> DigitalOut LedYellow(LED4); DigitalOut LedGreen(LED3); DigitalOut LedBlue(LED2); DigitalOut LedRed(LED1); DigitalOut Output1(p30); DigitalOut Output2(p29); PinDetect Input1(p8, PullDown); PinDetect Input2(p7, PullDown); DigitalIn Button1(p25); DigitalIn Button2(p26); Serial pc(USBTX, USBRX); FRamIO fRam(p28, p27, 0xA0, p24); WiflyInterface wifly(p13, p14, NC, p5, "mynetwork", "mypassword", WPA); int main() { wifly.init(); wifly.connect(); UDPSocket socket; socket.init(); //socket.bind(1235); socket.set_blocking(false, 1000); Endpoint remoteTcp; remoteTcp.set_address("192.168.1.7", 1235); LedRed = 0; LedBlue = 1; LedGreen = 1; LedYellow = 1; char* address = wifly.getIPAddress(); pc.printf("%s\n", address); char buffer[256]; while(1) { if (pc.readable()) { pc.putc(pc.getc()); } int bytesRead = socket.receiveFrom(remoteTcp, buffer, sizeof(buffer)); if (bytesRead > 0) { pc.printf("read %i bytes\n", bytesRead); buffer[bytesRead] = '\0'; pc.printf("%s\n", buffer); } } }
Ultimately I need to be running a loop doing various work on each pass. One of the things I want to do on each pass is check for a received message, or possibly send a message. But there are other processes going on too so I can't sit around and wait forever for a UDP message; ideally I want to see if there is a message pending (wait no more than 1ms) and if not, check again on the next pass.
1 Answer
11 years, 3 months ago.
Hi Reed,
I had some similar challenges with WiFly module and blocking mode. I ended up deriving my own version of both WiflyInterface and Wifly. And for a practical application, you can look at Smart-WiFly-Webserver where I use the mods I made.