Simple Python Script To Mimic APC UPS

Print Friendly, PDF & Email

Just written this script to simulate a APC SMART UPS for one of my ongoing projects.

#! /usr/bin/env python
"""\
The purpose of this program is to simulate an APC UPS
It will respond to only 'basic' UPS commands
Presently only following commands are supported
YBFLOPR
"""
import datetime
import serial
import glob
import sys
name="/dev/ttyUSB0" # change to suit your needs
ser = serial.Serial(name, 2400, timeout=1)
print "Trying to open" + name
if (ser.isOpen()) :
	print "Sucessfully opened port " + name
	print "Waiting for command on serial port"
	#loop start
	while 1	:
		timenow= datetime.datetime.now().strftime("%I:%M:%S") 
		cmd=ser.read() #read a single byte
		if len(cmd) != 0 :
			
			print timenow +  "--> Received command-> " + cmd
			#change output based on received command
			if cmd == 'Y':
				print timenow +  "--> Sending Reply SM"
				ser.write("SM\r\n")
			elif cmd == 'B':
				print timenow +  "--> Sending Reply 27.87"
				ser.write("27.87\r\n")
			elif cmd == 'F':
				print timenow +  "--> Sending Reply 50.00"
				ser.write("50.00\r\n")
			elif cmd == 'L':
				print timenow +  "--> Sending Reply 118.3"
				ser.write("118.3\r\n")
			elif cmd == 'O':
				print timenow +  "--> Sending Reply 118.3"
				ser.write("118.3\r\n")
			elif cmd == 'P':
				print timenow +  "--> Sending Reply 023.5"
				ser.write("023.5\r\n")
			elif cmd == 'f':
				print timenow +  "--> Sending Reply 099.0"
				ser.write("099.0\r\n")
			elif cmd == 'j':
				print timenow +  "--> Sending Reply 00327:"
				ser.write("0327:")
			elif cmd == 'R':
				print timenow +  "--> Sending Reply BYE"
				ser.write("BYE\r\n")
			print "-----------------------------------------------------"
		
		
	ser.close()
Ishan Karve

About Ishan Karve

Ishan Karve is just an every day normal guy next door who happens to be an Electronics Engineer by profession and dabbles with PHP, Javascript, C++ and python. His interests vary as seasons change.. they change from astronomy to soul searching. This site is just a reflection of what he does to keep his mind engaged when he is not occupied by work and family. He is an extremely objective guy and is always ready for some good arguments.. of course over a glass of 40% proof alcohol.
This entry was posted in DIY, Electronics, PIC, Projects, Python and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *