Advertisement
Guest User

OTDR

a guest
Nov 19th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.52 KB | None | 0 0
  1. import sys
  2. import glob
  3. import serial
  4. import time
  5. import ftplib
  6. import re
  7. import csv
  8. from ftplib import FTP
  9.  
  10. COM_PORT = "COM8"
  11.  
  12. QUERY_STATE = "STATE?"
  13. QUERY_EVENT_LINE = "EVENT? LINE"
  14. QUERY_EVENT_PARAM = "7"
  15. QUERY_EVENT_TOP = "EVENT? TOP"
  16. RESP_STATE = "STATE 0\r\n"
  17. RESP_ERR0 = "ERR0\r\n"
  18. CMD_SPARA = "SPARA"
  19. PARAMS_SPARA = "125,10000,5,0"
  20. CMD_ACQT = "ACQT"
  21. PARAMS_ACQT = "10"
  22. CMD_UIOR = "UIOR"
  23. PARAMS_UIOR = "1.469000"
  24. CMD_TREF = "TREF"
  25. PARAMS_TREF = "-60.0"
  26. CMD_TLOS = "TLOS"
  27. PARAMS_TLOS = "0.30"
  28. CMD_TEOF = "TEOF"
  29. PARAMS_TEOF = "3"
  30. CMD_DATE = "DATE 2019,11,7"
  31. CMD_TIME = "TIME 18,47,14"
  32. CMD_SCAN = "SCAN"
  33.  
  34. OTDR_FTP_USERNAME = "eotdr"
  35. OTDR_FTP_USERPASS = "002281"
  36. OTDR_FTP_FILENAME = "otdr.sor"
  37.  
  38.  
  39. class TopResponse:
  40.     pass
  41.  
  42.  
  43. class LineResponse:
  44.     pass
  45.  
  46.  
  47. eventsLine = []
  48. scan_number = 1
  49.  
  50.  
  51. def serial_ports():
  52.     """ Lists serial port names
  53.  
  54.        :raises EnvironmentError:
  55.            On unsupported or unknown platforms
  56.        :returns:
  57.            A list of the serial ports available on the system
  58.    """
  59.     if sys.platform.startswith('win'):
  60.         ports = ['COM%s' % (i + 1) for i in range(256)]
  61.     elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
  62.         # this excludes your current terminal "/dev/tty"
  63.         ports = glob.glob('/dev/tty[A-Za-z]*')
  64.     elif sys.platform.startswith('darwin'):
  65.         ports = glob.glob('/dev/tty.*')
  66.     else:
  67.         raise EnvironmentError('Unsupported platform')
  68.  
  69.     result = []
  70.     for port in ports:
  71.         try:
  72.             s = serial.Serial(port)
  73.             s.close()
  74.             result.append(port)
  75.         except (OSError, serial.SerialException):
  76.             pass
  77.     return result
  78.  
  79.  
  80. def parse_event_top(event_top_resp):
  81.     event_top_resp_splitted = re.split(',| ', event_top_resp.decode('utf-8'))
  82.  
  83.     top_response.totalEvents = event_top_resp_splitted[2]
  84.     top_response.fiberLength = event_top_resp_splitted[3]
  85.     top_response.totalLoss = event_top_resp_splitted[4]
  86.     top_response.totalReturnLoss = event_top_resp_splitted[5]
  87.  
  88.     write_events_top_to_csv_file(top_response)
  89.  
  90.  
  91. def parse_event_line(event_line_resp):
  92.     event_line_resp_splitted = re.split(',| ', event_line_resp.decode('utf-8'))
  93.  
  94.     line_response.eventNumber = event_line_resp_splitted[2]
  95.     line_response.eventLocation = event_line_resp_splitted[3]
  96.     line_response.eventLoss = event_line_resp_splitted[4]
  97.     line_response.reflectance = event_line_resp_splitted[5]
  98.     line_response.totalLoss = event_line_resp_splitted[6]
  99.     line_response.eventType = event_line_resp_splitted[7]
  100.  
  101.     eventsLine.append(line_response)
  102.  
  103.     # print(line_response)
  104.     write_event_to_csv_file(scan_number, line_response)
  105.  
  106.  
  107. def write_command(cmd, params):
  108.     s.reset_input_buffer()
  109.     if params == "":
  110.         s.write((cmd+'\n').encode())
  111.         print(cmd)
  112.     else:
  113.         s.write((cmd + ' ' + params+'\n').encode())
  114.         print(cmd + ' ' + params)
  115.  
  116.     res = s.readline()
  117.     print(res.decode('utf-8'))
  118.     if cmd == QUERY_STATE:
  119.         if res == RESP_STATE.encode('utf-8'):
  120.             # print("Good")
  121.             return 1
  122.         else:
  123.             # print("Bad")
  124.             return 0
  125.     elif cmd == QUERY_EVENT_TOP:
  126.         parse_event_top(res)
  127.         return 1
  128.     elif cmd == QUERY_EVENT_LINE:
  129.         parse_event_line(res)
  130.         return 1
  131.     else:
  132.         if res == RESP_ERR0.encode('utf-8'):
  133.             # print("OK")
  134.             return 1
  135.         else:
  136.             # print("ERROR")
  137.             return 0
  138.  
  139.  
  140. def write_command_till_ok(cmd, params=""):
  141.     retry_count = 1
  142.  
  143.     while write_command(cmd, params) == 0:
  144.         print("retry " + format(retry_count))
  145.         retry_count = retry_count + 1
  146.         time.sleep(1)
  147.  
  148.  
  149. def init_scan():
  150.     write_command_till_ok(CMD_SPARA, PARAMS_SPARA)
  151.     write_command_till_ok(CMD_ACQT, PARAMS_ACQT)
  152.     write_command_till_ok(CMD_UIOR, PARAMS_UIOR)
  153.     write_command_till_ok(CMD_TREF, PARAMS_TREF)
  154.     write_command_till_ok(CMD_TLOS, PARAMS_TLOS)
  155.     write_command_till_ok(CMD_TEOF, PARAMS_TEOF)
  156.     write_command_till_ok(CMD_SCAN)
  157.     write_command_till_ok(QUERY_EVENT_TOP)
  158.     if (int(top_response.totalEvents)> 0):
  159.         for event_idx in range(int(top_response.totalEvents)):
  160.             write_command_till_ok(QUERY_EVENT_LINE, format(event_idx+1))
  161.  
  162.  
  163. def wait_for_scan_end():
  164.     write_command_till_ok(QUERY_STATE)
  165.  
  166.  
  167. def grab_file():
  168.  
  169.     filename = OTDR_FTP_FILENAME
  170.  
  171.     local_file = open("c:/temp/" + filename, 'wb')
  172.     ftp.retrbinary('RETR ' + filename, local_file.write)
  173.  
  174.     ftp.quit()
  175.     local_file.close()
  176.  
  177.  
  178. def open_csv_file(scan_num):
  179.     row = ['event number', ' location', 'loss', 'reflectance', 'total loss', 'event type']
  180.     with open('otdr_events_'+str(scan_num)+'.csv', 'w', newline='') as csvFile:
  181.         writer = csv.writer(csvFile)
  182.         writer.writerow(row)
  183.  
  184.     csvFile.close()
  185.  
  186.  
  187. def open_top_csv_file():
  188.     row = ['scan number', 'total events', 'fiber length', 'total loss', 'total return loss']
  189.     with open('otdr_tops.csv', 'w', newline='') as csvFile:
  190.         writer = csv.writer(csvFile)
  191.         writer.writerow(row)
  192.  
  193.     csvFile.close()
  194.  
  195.  
  196. def write_event_to_csv_file(scan_num, event_data):
  197.     row = [event_data.eventNumber, event_data.eventLocation, event_data.eventLoss, event_data.reflectance, event_data.totalLoss, event_data.eventType]
  198.     with open('otdr_events_'+str(scan_num)+'.csv', 'a', newline='') as csvFile:
  199.         writer = csv.writer(csvFile)
  200.         writer.writerow(row)
  201.  
  202.     csvFile.close()
  203.  
  204.  
  205. def write_events_top_to_csv_file(top_data):
  206.     row = [scan_number, top_data.totalEvents, top_data.fiberLength, top_data.totalLoss, top_data.totalReturnLoss]
  207.     with open('otdr_tops.csv', 'a', newline='') as csvFile:
  208.         writer = csv.writer(csvFile)
  209.         writer.writerow(row)
  210.  
  211.     csvFile.close()
  212.  
  213.  
  214. if __name__ == '__main__':
  215.  
  216.     top_response = TopResponse()
  217.     line_response = LineResponse()
  218.  
  219.     open_top_csv_file()
  220.  
  221.     print(serial_ports())
  222.  
  223.     s = serial.Serial()
  224.     s.baudrate = 115200
  225.     s.port = COM_PORT
  226.     s.timeout = 1
  227.     s.open()
  228.  
  229.     print('OTDR is loading')
  230.  
  231.     count = 1
  232.  
  233.     write_command_till_ok(QUERY_STATE)
  234.     print('OTDR is UP\r\n')
  235.  
  236.     open_csv_file(scan_number)
  237.     init_scan()
  238.     wait_for_scan_end()
  239.  
  240.     for(x) in eventsLine:
  241.         print(x.eventLocation)
  242.  
  243.     eventsLine.clear()
  244.  
  245.     ftp = ftplib.FTP('localhost', OTDR_FTP_USERNAME, OTDR_FTP_USERPASS)
  246.     ftp.cwd('disk')
  247.     files = ftp.dir()
  248.     print(files)
  249.  
  250.     grab_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement