-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasure-volts.py
67 lines (54 loc) · 1.6 KB
/
measure-volts.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
# Ugly code following!
import time
import math
import array
import pymodbus
import serial
import argparse
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient #initialize a serial RTU client instance
from pymodbus.transaction import ModbusRtuFramer
#import logging
#logging.basicConfig()
#log = logging.getLogger()
#log.setLevel(logging.DEBUG)
baudrate = 38400
parity = 'N'
port = "/dev/ttyUSB0"
global actualVoltage
def probe():
try:
client = ModbusClient(method = "rtu", port = port, stopbits = 1, bytesize = 8, parity = parity, baudrate = baudrate, timeout=0.5)
connection = client.connect()
result0 = client.read_coils(2000,1,unit=unit)
client.close()
except:
print ("Serial error. Is "+port+" available?")
client.close()
else:
try:
result0.string
except:
client.close()
print("Client exists.")
return True
else:
client.close()
print("Client unreachable!")
return False
def calibrate():
client = ModbusClient(method = "rtu", port = port, stopbits = 1, bytesize = 8, parity = parity, baudrate = baudrate)
connection = client.connect()
result = client.write_register(1001,38559,unit=unit) #read back value from eeprom
time.sleep(2)
oldVoltage = client.read_holding_registers(52,1,unit=unit)
print("Measured voltage according to client (millivolts): "+str(oldVoltage.registers[0]))
client.close()
# print("Failed!")
# client.close()
parser = argparse.ArgumentParser()
parser.add_argument("address", type=int, help="device's address")
args = parser.parse_args()
unit = args.address
if probe():
calibrate()