-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathups2_Interface.py
120 lines (107 loc) · 3.5 KB
/
ups2_Interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Module containimg functions for serial communication with UPS-2 power supply.
This is the command interface Raspberry Pi to UPS-2 power supply
Module ups2_serial.py must be active in background.
"""
import serial
import fcntl
def ecInitSerial(device = '/dev/serial0'):
'''Opens serial device and returns a handle'''
ser = serial.Serial(
port= device,
baudrate = 38400,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=3)
if ser.isOpen(): ser.close()
ser.open()
return ser
def ecReadline(ser):
"""Returns a string from serial line until a <CR> character."""
rxLine = ""
charCount = 0
while True:
rxChar = ser.read(1)
if (rxChar == b"\n"): #end of string
charcount = 0
return rxLine
else:
if (rxChar != b"\r"): #ignore \r EOL characer
charCount +=1
rxLine +=rxChar.decode("ascii")
def ecFormatAnalog(analogStr):
'''Add units to analogStr 'main,batt,temp' and return as a list.'''
units = ('V ','V ','°C')
l = []
v = analogStr.split(',')
for i in range(3):
l.append(v[i] + units[i])
return l
def ecGetUPSValues(ser):
'''Requests values from UPS-2.'''
UPS_version = "?"
try:
request = "r?status\n"
fcntl.flock(ser, fcntl.LOCK_EX)
ser.write(str(request).encode())
status = ecReadline(ser) #UPS returns hex coded power status
# print(status)
request = "r?analog\n"
ser.write(str(request).encode())
analog = ecReadline(ser)
analog = analog[2:]
fcntl.flock(ser, fcntl.LOCK_UN)
#decompose status message
statusHex =""
statusHex = status[2:8] #extract hex status
# ups2Version = status[10:]
# return {'statusHex' : statusHex, 'analog': analog, 'ups2Ver': ups2Version}
return(statusHex, analog)
except:
print("exception happened @ ecGetUPSValues()")
pass
def ecGetUPSVersion(ser):
'''Requests firmware ersion from UPS-2.'''
try:
request = "r?version\n"
fcntl.flock(ser, fcntl.LOCK_EX)
ser.write(str(request).encode())
UPS_version = ecReadline(ser)[2:]
fcntl.flock(ser, fcntl.LOCK_UN)
return UPS_version
except:
print("exception happened @ ecGetUPSVersion")
pass
def ecReqUPSPowerDown(ser):
'''Initiates a Pi shutdown and executes power off after Pi is down'''
ack =''
try:
request = "r?shutdown -P\n"
fcntl.flock(ser, fcntl.LOCK_EX)
ser.write(str(request).encode())
ack = ecReadline(ser)
print('Response UPS = ', ack)
fcntl.flock(ser, fcntl.LOCK_UN)
return(ack)
except:
print("exception happened @ ecReqUPSPowerDown()")
pass
def ecReqBootloader(ser):
try:
request ="r?boot"
fcntl.flock(ser, fcntl.LOCK_EX)
ser.write(str(request).encode())
fcntl.flock(ser, fcntl.LOCK_UN)
#
# command = 'sudo pkill -f ups2_serial.py'
# os.system(str(command).encode())
# fcntl.flock(ser, fcntl.LOCK_UN)
# ser.close()
# command = 'stm32flash -w UPS-2_G030.bin -v -g 0x0 /dev/serial0'
# os.system(str(command).encode())
# command = 'python3 ups2_serial.py'
# os.system(str(command).encode())
return('UPS-2 now in bootloader')
except:
print("exception happened @ ecReqBootloader")
pass