autumnrd

Get UPS APC status plugged on serial port

Jul 15th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. # http://scratch-tales.blogspot.ru/
  2. #!/usr/local/bin/python2.7
  3. import sys
  4. import os
  5. import time
  6. import termios
  7.  
  8. #------------------------------VAR------------------
  9. ups_port = '/dev/tty00'
  10.  
  11.  
  12. #--------------Open and setting UPS port------------
  13. ups_ghost = open(ups_port, 'r+', 0)
  14.  
  15. com_port_attr = termios.tcgetattr(ups_ghost)
  16.  
  17. #iflag
  18. com_port_attr[0] = termios.IGNBRK
  19. #oflag
  20. com_port_attr[1] = 0x0
  21. #cflag - ignore CTS/RTS | enable receiver | 2400 |   8n1  (n and 1 are 0x0)
  22. com_port_attr[2] = termios.CLOCAL | termios.CREAD | termios.B2400 | termios.CS8
  23. #lflag
  24. com_port_attr[3] = 0x0
  25. #ispeed
  26. com_port_attr[4] = termios.B2400
  27. #ospeed
  28. com_port_attr[5] = termios.B2400
  29. # Do the dastardly deed
  30. termios.tcsetattr(ups_ghost, termios.TCSANOW, com_port_attr)
  31.  
  32. ups_ghost.write('Y')
  33. ups_ghost.flush()
  34. ret = ups_ghost.readline().strip()
  35. print "UPS status: ", ret
  36.  
  37. ups_ghost.write('P')
  38. ups_ghost.flush()
  39. ret = ups_ghost.readline().strip()
  40. print "UPS load: ", ret
  41.  
  42. ups_ghost.write('L')
  43. ups_ghost.flush()
  44. ret = ups_ghost.readline().strip()
  45. print "Line voltage: ", ret
  46.  
  47. ups_ghost.write('O')
  48. ups_ghost.flush()
  49. ret = ups_ghost.readline().strip()
  50. print "Output voltage: ", ret
  51.  
  52. ups_ghost.write('B')
  53. ups_ghost.flush()
  54. ret = ups_ghost.readline().strip()
  55. print "Battery voltage: ", ret
  56.  
  57. ups_ghost.write('f')
  58. ups_ghost.flush()
  59. ret = ups_ghost.readline().strip()
  60. print "Battery status: ", ret
  61.  
  62. ups_ghost.close()
Advertisement
Add Comment
Please, Sign In to add comment