Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import serial
  2. import json
  3. import time
  4.  
  5. in_phase = False
  6. import os
  7. import datetime
  8.  
  9. print("Тест запущен", datetime.datetime.now())
  10.  
  11. def read_json(ser):
  12. is_work = True
  13. count = 0
  14. return_data = ''
  15. uart_answer = ser.readline().decode('cp1251')
  16. while not '"time":' in uart_answer:
  17. uart_answer = ser.readline().decode('cp1251')
  18. uart_answer = ser.readline().decode('cp1251')
  19. while is_work:
  20. uart_answer = ser.readline().decode('cp1251')
  21. if uart_answer.startswith('}'):
  22. is_work = False
  23. return_data = return_data + uart_answer
  24. count = count + 1
  25. # if count>24:
  26. # is_work = False
  27. if return_data.count('''{\n"header": ''') > 1:
  28. ff = return_data.find('''{\n"header": ''')
  29. fs = return_data[ff+1:].find('''{\n"header": ''')
  30. return return_data[fs:]
  31. return return_data
  32.  
  33.  
  34. def print_result(parsed):
  35. os.system('cls')
  36. memstat = parsed['SPI flash info'].split(' ')[0]
  37. print("Статус микросхем:", "Ок" if parsed["status_hardware"] == 7 else "Плохо")
  38. print("Статус микросхемы памяти:", 'Ок' if memstat=="1F" else "Плохо")
  39. print('Давление:', parsed['PS'])
  40. print("Main 12В");
  41. print('\tU =', parsed["12_MV"], 'В')
  42. print('\tI =', parsed["12_MC"], 'A')
  43. print("Reserve 12В");
  44. print('\tU =', parsed["12_RV"], 'В')
  45. print('\tI =', parsed["12_RC"], 'A')
  46. print()
  47. print("Main 24В");
  48. print('\tU =', parsed["24_MV"], 'В')
  49. print('\tI =', parsed["24_MC"], 'A')
  50. print("Reserve 24В");
  51. print('\tU =', parsed["24_RV"], 'В')
  52. print('\tI =', parsed["24_RC"], 'A')
  53. print()
  54. print('220В =', parsed['220_V'], "В")
  55. print()
  56. print('t SHT31', parsed["t_SHT"])
  57. print('Hum SHT31', parsed["HUM"])
  58. print("Данные от SHT31:", parsed["sht31 data"])
  59. print('Реле: ', "Вкл." if parsed["relay"]=='on' else "Выкл")
  60. print('USB отклик : ', str(parsed["uarts"]).replace('1', 'Да').replace('0','Нет'))
  61. doors = parsed["door_1"]+parsed["door_2"]+parsed["door_3"]+parsed["door_4"]
  62. print('Датчик двери:', "Не активен" if doors == 0 else "Активен")
  63.  
  64. def main():
  65. conf_file = open('pkd_conf.txt')
  66. port_name = conf_file.readline().replace(' ', '').replace('\t', '')
  67.  
  68. try:
  69. with serial.Serial(port= port_name, baudrate=19200, timeout=100) as ser:
  70. i = 0
  71. print('Первый COM Активен')
  72. while 1:
  73. #time.sleep(2)
  74. ser.write(bytes('AT R1 1\r', 'cp1251'))
  75.  
  76. if ser.in_waiting>0:
  77. i+=1
  78. readed = read_json(ser)
  79. parsed = json.loads(readed, encoding="cp1251")
  80. print_result(parsed)
  81.  
  82. time.sleep(2)
  83. ser.write(bytes('AT I\n', 'cp1251'))
  84.  
  85. time.sleep(2)
  86. except:
  87. print('Не могу связаться с ', port_name)
  88.  
  89. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement