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.
8 years, 9 months ago. This question has been closed. Reason: Opinion based - no single answer
wiz550io blocks the while loop
Managed to transfer data to/from PC and Nucleo F401RE using wiz550io!! See below my code. But the while loops is always waiting for a connection from PC. Is there any way around such that I can run my other (non Ethernet related) code in the while loop while exchanging data with the PC.
It will be ideal to have an Interrupt service routine for receiving and sending data?
int main (void) { SPI spi2(PC_12, PC_11, PC_10); // mosi, miso, sclk EthernetInterface eth(&spi2, PC_6, PB_8); wait(1); // 1 second for stable state // late binding TCPSocketServer svr; TCPSocketConnection client; ledTick.attach(&ledTickfunc,2); // as your env. change to real IP address and so on. int ret = eth.init(MY_IP, MY_NETMASK, MY_GATEWAY); if (!ret) { serial.printf("Initialized, MAC: %s\n\r", eth.getMACAddress()); serial.printf("Connected, IP: %s, MASK: %s, GW: %s\n\r", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); } else { serial.printf("Error eth.init() - ret = %d\n\r", ret); return -1; } //setup tcp socket if(svr.bind(PORT)< 0) { serial.printf("tcp server bind failed.\n\r"); return -1; } else { serial.printf("tcp server bind successed.\n\r"); serverIsListened = true; } if(svr.listen(1) < 0) { serial.printf("tcp server listen failed.\n\r"); return -1; } else { serial.printf("tcp server is listening...\n\r"); } char buffer[256] = {}; //blocking mode(never timeout) client.set_blocking(true,0); // Timeout after (1.5)s while (serverIsListened) { if(svr.accept(client)<0) { serial.printf("failed to accept connection.\n\r"); }else if(svr.accept(client)== 5){ // there is no connection }else { serial.printf("connection success!\n\rIP: %s\n\r",client.get_address()); clientIsConnected = true; while(clientIsConnected) { switch(client.receive(buffer, 255)) { case 0: //serial.printf("recieved buffer is empty.\n\r"); clientIsConnected = false; client.close(); break; case -1: //serial.printf("failed to read data from client.\n\r"); clientIsConnected = false; client.close(); break; default: //serial.printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); //if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { //serial.printf("GET request incomming.\n\r"); //wait(1); float cnv; cnv = atof(buffer); cnv = -cnv/10.0f; //serial.printf("data in %f.\n\r",cnv); for(int i =0; i < 255; i++) buffer[i] = '\0'; sprintf(buffer,"%f\r\n",cnv); client.send(buffer,strlen(buffer)); clientIsConnected = false; //serial.printf("echo back done.\n\r"); //} for(int i =0; i < 255; i++) buffer[i] = '\0'; clientIsConnected = false; // *** client.close(); // *** break; } // switch(client.receive(buffer, 255)) client.close(); // *** } // while(clientIsConnected) //serial.printf("close connection here.\n\rtcp server is listening...\n\r"); } //if(svr.accept(client)<0) clientIsConnected = false; // *** client.close(); } //while (serverIsListened) }
A very bad hack is to modify the TCPSocketServer.cpp file as follows. Indeed the data is missed few times