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.
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
Hello,
I'm trying to use the mbed to talk to a number of Ethernet-connected pieces of test equipment using SCPI commands. Basically telneting to the devices over port 5025, sending commands and reading responses. The code I have below seems to work fine, but occasionally it hangs while making the connection:
/* ** Initializes the Function Generator by opening a socket and querying the response to the "*IDN?" and ":SYSTem:COMMunicate:LAN:MAC?" commands. ** Returns 0 if valid, 1 if unable to connect to Function Generator, or invalid responses received */ int FGEN_Init () { unsigned int timeout = 0; int result = 0; char buf [256]; char buf2 [256]; char testResult [255]; // Open a socket if (DEBUG) printf ("Connecting socket...\r\n"); ClientSocket sock (FGEN_IP, 5025, 10); while (!(sock.connected () || timeout > 5)) { timeout ++; } // Set error if timed out if (timeout > 5) { if (DEBUG) printf ("Unable to connect to Agilent Function Generator.\r\n"); result = 1; } // Else proceed else { // Get Ethernet MAC of FGEN if (DEBUG) printf ("Socket connected!\r\n"); sock.printf (":SYSTem:COMMunicate:LAN:MAC?\n"); while (!sock.available ()) { } //sock.read (buf, 14); sock.scanf ("%s", buf); if (DEBUG) printf ("MAC Query Reply: [%s]\r\n", buf); if (strcmp (buf, FGEN_MAC) != 0) { if (DEBUG) printf ("Invalid MAC address for Agilent Function Generator. Check network configuration.\r\n"); result = 1; } // Else get identification string else { while (sock.available ()) { } // Clear out buffer sock.printf ("*IDN?\n"); while (!sock.available ()) { } sock.scanf ("%s %s", buf, buf2); //if (DEBUG) printf ("Reply: [%s %s]\r\n", buf, buf2); if (strcmp (buf, FGEN_ID1) != 0 || strcmp (buf2, FGEN_ID2) != 0) { if (DEBUG) printf ("Invalid ID string for Agilent Function Generator. Check configuration.\r\n"); result = 1; } } } // If all good, reset the device if (result == 0) { sock.printf ("*RST\n"); // Run the self-test sock.printf ("*TST?\n"); while (!sock.available ()) { } sock.scanf ("%s", testResult); printf ("Function Generator test result = %s", testResult); if (strcmp (testResult, "+0") != 0) { if (DEBUG) printf ("Function Generator self-test failed. Error code: %s", testResult); result = 1; } } // Close connection sock.close (); // Return results return (result); }Can anyone provide any ideas? Thanks in advance! :)