Lantronix Discovery Protocol

From Lantronix Wiki!
Revision as of 23:56, 24 March 2015 by Gary marrs (talk | contribs)
Jump to navigation Jump to search

This example is a Python app in source code that shows how to search and find Lantronix Devices using a UDP broadcast. It is run from the command line.

Prerequisites

1) import socket, time and sys 2) While this code could be incorporated in a Premierwave device, it was written and tested on a PC. (win 7)

Sample Code

# utility to search and find Lantronix devices
#
# written by: 
#	Gary Marrs
#	Lantronix FAE
#	garym@lantronix.com
#	03-20-15
#	Rev 0.1
#

import socket   #for sockets
import sys  	#for exit
import time

j = 0
k = 0
host = ''
port = 30718
DATA = []
LTRXnode = []
packet = []
 
print "searching for Lantronix devices...\n"
 
# create datagram udp socket
try:
	s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
	s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
	s.bind((host,port))
	s.settimeout(2)
except socket.error:
	print 'Failed to create socket'
	sys.exit()
 

#Set the search string
msg = chr(0)+chr(0)+chr(0)+chr(0xf6)

try:
	s.sendto(msg, ("192.168.1.255", port))
except socket.error, msg:
	print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
	sys.exit()
#time.sleep(4)
t1 = 0
t0 = time.clock()										# set start time
	
while (t1<3):											# loop for no more than 3 seconds
	try:
		# receive data from client (data, addr)
		d, address = s.recvfrom(128)
		packet = "".join("%02x" % ord(c) for c in d)
		if packet[6:8] == "f7":
			LTRXnode.append(address[0])
			DATA.append(packet[48:60])
			j += 1
	except Exception:
		#print "no rcv"
		s.close()
	
	time.sleep(1)
	t1 = time.clock() - t0								# find difference in time
	#print t1	

# print out results of search
print "%d devices found " % j
	
for x in LTRXnode:
	print "%s  %s " % (x,DATA[k])
	k +=1

print "\n"
		
raw_input('Press the Enter Key to Exit\n')