PYTHON   20

get lmount ports py

Guest on 15th August 2022 12:37:22 PM

  1. from datetime import datetime
  2. import os
  3. import sys
  4.  
  5. from serial import Serial
  6. from serial.tools import list_ports
  7.  
  8. AUTODETECT_PORTS = False
  9. MANUAL_PORTS = ["COM8", "COM18", "COM3", "COM4"] # If AUTODETECT_PORTS is False, scan these specific ports
  10.  
  11. PORT_CONFIG = {
  12.         # Serial number -> (Config file, Axis number (0 or 1))
  13.         '19101948': (r'C:\Users\kmi\pwi4configs\instance1\Settings\PWI4.cfg', 0), # Mount1 Azimuth
  14.         '19101937': (r'C:\Users\kmi\pwi4configs\instance1\Settings\PWI4.cfg', 1), # Mount1 Altitude
  15.         '16420003': (r'C:\Users\kmi\pwi4configs\instance2\Settings\PWI4.cfg', 0), # Mount2 Azimuth
  16.         '16420004': (r'C:\Users\kmi\pwi4configs\instance2\Settings\PWI4.cfg', 1), # Mount2 Altitude
  17. }
  18.  
  19.  
  20. def usage():
  21.     print("Usage: get_lmount_ports.py [--report | --updateconfig]")
  22.     sys.exit(1)
  23.  
  24. def main():
  25.     if len(sys.argv) < 2:
  26.         usage()
  27.     if sys.argv[1] == "--report":
  28.         report_ports()
  29.     elif sys.argv[1] == "--updateconfig":
  30.         update_config()
  31.     else:
  32.         usage()
  33.  
  34.  
  35. def get_ports_to_scan():
  36.     ports = MANUAL_PORTS
  37.     if AUTODETECT_PORTS:
  38.         print("Autodetecting USB-connected ports...")
  39.         ports = []
  40.         candidate_ports = list_ports.comports()
  41.         for port in candidate_ports:
  42.             if port.vid == 0x1cbe and port.pid == 0x0267:
  43.                 ports.append(port.device)
  44.     return ports
  45.  
  46. def report_ports():
  47.     print("Report port info only (no config files will be updated)")
  48.  
  49.     ports = get_ports_to_scan()
  50.  
  51.     print("Scanning ports:", ports)
  52.  
  53.     for port in ports:
  54.         (serial_num, encoder) = get_port_info(port)
  55.         print("%s SerialNum=%s Encoder=%s" % (
  56.             port,
  57.             serial_num,
  58.             encoder
  59.             ))
  60.  
  61. def update_config():
  62.     print("Config files will be updated based on port responses")
  63.  
  64.     ports = get_ports_to_scan()
  65.     print("Scanning ports:", ports)
  66.  
  67.     axis_num_to_config_keyword = {
  68.             0: "ComPort",
  69.             1: "ComPortAxis1"
  70.         }
  71.  
  72.     for port in ports:
  73.         (serial_num, encoder) = get_port_info(port)
  74.         print("%s SerialNum=%s Encoder=%s" % (
  75.             port,
  76.             serial_num,
  77.             encoder
  78.             ))
  79.  
  80.         if serial_num in PORT_CONFIG:
  81.             config_file = PORT_CONFIG[serial_num][0]
  82.             axis_number = PORT_CONFIG[serial_num][1]
  83.  
  84.             config_keyword = axis_num_to_config_keyword[axis_number]
  85.  
  86.             replace_config_file_entry(config_file, axis_num_to_config_keyword[axis_number], port)
  87.  
  88. def replace_config_file_entry(filename, replace_keyword, replace_value):
  89.     tmp_filename = filename + ".tmp"
  90.  
  91.     f_in=open(filename, "r")
  92.     f_out=open(tmp_filename, "w")
  93.  
  94.     has_changed = False
  95.  
  96.     for line in f_in:
  97.         line = line.strip()
  98.         keyword, value = line.split('=')
  99.         keyword = keyword.strip()
  100.         value = value.strip()
  101.         if keyword == replace_keyword and value != replace_value:
  102.             print("Updating", filename, keyword, "=", replace_value)
  103.             print('%s                 = %s' % (keyword, replace_value), file=f_out)
  104.             has_changed = True
  105.         else:
  106.             print(line, file=f_out)
  107.  
  108.     f_in.close()
  109.     f_out.close()
  110.  
  111.     if has_changed:
  112.         timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
  113.         backup_filename = filename + "_backup_" + timestamp
  114.         print("Backing up", filename, "to", backup_filename)
  115.         os.replace(filename, backup_filename)
  116.  
  117.         print("Writing new", filename)
  118.         os.rename(tmp_filename, filename)
  119.     else:
  120.         print("No changes need to be made to keyword", replace_keyword, "in", filename)
  121.         os.remove(tmp_filename)
  122.  
  123.  
  124. def get_port_info(port):
  125.     ser = Serial(port)
  126.     ser.timeout=0.2
  127.  
  128.     serial_num = get_value(ser, b'SN[4]\r')
  129.     encoder = get_value(ser, b'PX\r')
  130.  
  131.     ser.close()
  132.  
  133.     return (serial_num, encoder)
  134.  
  135.  
  136. def get_value(ser, command):
  137.     ser.write(command)
  138.     response = ser.read(100).decode('ascii')
  139.  
  140.     response_fields = response.split('\r', 2)
  141.     if len(response_fields) < 2:
  142.         return ''  # Did not receive proper response
  143.     else:
  144.         response = response_fields[1].rstrip(';')
  145.         return response
  146.  
  147.  
  148.  
  149.  
  150. if __name__ == "__main__":
  151.     main()

Raw Paste


Login or Register to edit or fork this paste. It's free.