usbdiscover.py
Go to the documentation of this file.
1 import serial, os, time, atexit
2 
3 lockfilepath = None
4 portnum = 0
5 
6 
8  os.system("rm -f /tmp/dev_ttyUSB*")
9 
11  if os.path.exists(lockfilepath):
12  os.remove(lockfilepath)
13 
14 atexit.register(removelockfile)
15 
16 
17 def checkBoardId(idstring, ser):
18  ser.reset_input_buffer()
19  ser.write("x\n") # check board id
20  line = ""
21  time.sleep(0.1)
22  while ser.inWaiting() > 0:
23  line = ser.readline().strip()
24 
25  if not line == idstring:
26  print("incorrect board id: "+line)
27  return False
28 
29  print("connected to: "+line)
30  return True
31 
32 
33 def usbdiscover(idstring, TIMEOUT):
34  global lockfilepath, portnum
35 
36  while portnum <= 6:
37  port = '/dev/ttyUSB'+str(portnum)
38 
39  print("trying port: "+port)
40 
41  lockfilepath = "/tmp/dev_ttyUSB"+str(portnum)
42  tries = 0
43  while tries < 5:
44  if os.path.exists(lockfilepath):
45  print("port busy: "+port)
46  time.sleep(1)
47  tries += 1
48  else:
49  break
50 
51  if tries == 5:
52  print("giving up on port: "+port)
53  portnum += 1
54  continue
55 
56  open(lockfilepath, 'w') # creates lockfile
57 
58  try:
59  ser = serial.Serial(port, 115200, timeout=TIMEOUT)
60  except serial.SerialException:
61  print("port exception: "+port)
62  os.remove(lockfilepath)
63  portnum += 1
64  time.sleep(1)
65  continue
66 
67  time.sleep(2)
68 
69  if checkBoardId(idstring, ser):
70  break
71 
72  ser.close()
73  if os.path.exists(lockfilepath):
74  os.remove(lockfilepath)
75  time.sleep(1)
76  portnum += 1
77 
78  if not ser.is_open:
79  print("device not found")
80  sys.exit(0)
81 
82  return ser
83 
def usbdiscover(idstring, TIMEOUT)
Definition: usbdiscover.py:33
def removelockfiles()
Definition: usbdiscover.py:7
def checkBoardId(idstring, ser)
Definition: usbdiscover.py:17
def removelockfile()
Definition: usbdiscover.py:10


xaxxon_openlidar
Author(s): Colin Adamson
autogenerated on Fri May 22 2020 04:05:28