SimpleSocket Issues

14 Dec 2011

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! :)

14 Dec 2011

Hi Jason,

Try setting debug option by either specifying the fourth argument of the constructor or using setDebug(bool) member function. The debugging info will be displayed on the termnal app (thru serial).

      while (sock.available ()) { } // Clear out buffer

This code does not clear out the buffer but enters infinite loop. Call the sock.read() within the while loop.

Thanks,

Hiroshi