by jamie allen on Feb 11, 2007

This code is a slight variation to the code available here for finding your Lantronix Device IP on the network, if you're using DHCP on your device. For the Xport DHCP is enabled by setting your server IP to 000.000.005.000. I've added a delay between udp.sends to all ports on the subnet between 1 and 254. Seems to be more consistent in it's read-back of Lantronix devices, although it does take longer this way (50 seconds).

This code is a good starting point for a nice little app that would query the network for your XPORT on startup, and report back it's own IP address.

/*
  Lantronix UDP Device Query - 

 Sends out a UDP broadcast packet to query a subnet for Lantronix
 serial-to-ethernet devices.

 Lantronix devices are programmed to respond to UDP messages received on
 port 30718.  If a Lantronix device receives the string 0x00 0x00 0x00 0xF6, 
 it respond with a UDP packet containing the status message on port 30718.
 See the Lantronix integration guide from http://www.lantronix guide for the details.

 This program uses the Hypermedia UDP library available at 
 http://hypermedia.loeil.org/processing/

 by Tom Igoe
 Created 18 April 2006
 jamie allen 
 revised 17 Feb 2007
 to scan through all devices on a XXX.XXX.XXX.(1-254) subnet - with a little delay
 */

// import UDP library
import hypermedia.net.*;

boolean queryRun = false;
//insert subnet here... i.e.: the first three digits of your IP XXX.XXX.XXX.000
//the program will ping XXX.XXX.XXX.000-XXX.XXX.XXX.255
//if you get an Lantronix response it will print it to the monitor window
String ipAddress[] = {"192","168","001","1"};  

UDP udp;                  // define the UDP object
int queryPort = 30718;    // the port number for the device query
//String broadcastIpAddress = "192.168.1.104";  // fill in IP address here

void setup() {
  // create a new connection to listen for 
  // UDP datagrams on query port
  udp = new UDP(this, queryPort);

  // listen for incoming packets:
  udp.listen( true );
}

//process events
void draw() {

/*
 send the query message once at run time:
 */
if (queryRun == false) 
{
  byte[] queryMsg = new byte[4];
  queryMsg[0] = 0x00;
  queryMsg[1] = 0x00;
  queryMsg[2] = 0x00;		
  queryMsg[3] = (byte)0xF6;

  for (int i=1; i<255 ; i++)
  {
    //  ipAddress[] = "CCCP";
    ipAddress[3] = str(i);

    String joinedIP = join(ipAddress, "."); 

    //println(joinedIP);
    // send the message
    // udp.send( queryMsg, broadcastIpAddress, queryPort );
    udp.send(queryMsg, joinedIP, queryPort );
    delay(200);
  }
  delay(500);
  queryRun = true;
}

}

/*
  listen for responses
 */
void receive( byte[] data, String ip, int port ) {	

  String inString = new String(data);    // data converted to a string
  int[] intData = int(data);             // data converted to ints
  int i = 0;                             // counter
  // print the result:
  //println( "received "+inString+"  from "+ip+" on port "+port );

  // parse the payload for the appropriate data:
  //print("Opcode: ");
  //println(intData[3]);  //247 is <F7>, which IDs the lantronix stuff

  // if the fourth byte is <F7>, we got a status reply:
  if (intData[3] == 0xF7) {		
    println( "received "+inString+"  from "+ip+" on port "+port );
    println(intData[3]);  //247 is <F7>, which IDs the lantronix stuff
    // firmware data is bytes 4 to 20:
    print("Firmware data: ");
    for (i=4; i < 20; i++) {
      print(" " + Integer.toHexString(intData[i]));
    }
    // MAC address is bytes 24 to 30 (the end):
    print("\nMAC Addr: ");
    for (i=24; i < intData.length; i++) {
    //could write in a convenience test for a given mac address here?
    //i.e.: only if your mac address matches this one, print IP
      print(" " + Integer.toHexString(intData[i]));
    }

    print("\n\n");
  }  

}