-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorensenPower.py
278 lines (207 loc) · 8.79 KB
/
sorensenPower.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/python3
import serial
# Assumes the following settings for the DCS M9 RS-232 Interface
# Baud-rate = 19200
# Hardware Flow Control = None
class sorensenPower:
# DEFAULT_TIMEOUT = 0.125
# List of commands
COMMAND_IDN = "*IDN?\r"
COMMAND_MEASURE_CURRENT = ":MEAS:CURR?\r"
COMMAND_MEASURE_VOLTAGE = ":MEAS:VOLT?\r"
COMMAND_SET_VOLTAGE = ":SOUR:VOLT {:1.03f}\r"
COMMAND_SET_VOLTAGE_RAMP = ":SOUR:VOLT:RAMP {:1.03f} {:1.01f}\r"
COMMAND_SET_CURRENT = ":SOUR:CURR {:1.03f}\r"
COMMAND_GET_STATUS = ":SOUR:STAT:BLOC?\r"
COMMAND_RETURN_LOCAL = ":SYST:LOCAL ON\r"
def __init__(self, client, debug=False):
# self.portName = portName
# self.baudrate = baudrate
# self.port = serial.Serial()
# self.port.baudrate = self.baudrate
# self.port.port = self.portName
# self.port.timeout = self.DEFAULT_TIMEOUT
# self.port.rts = True
# self.port.dtr = True
self.debug = debug
self.model = None
self.serialNumber = None
self.maxVoltage = None
self.maxCurrent = None
self.client = client
# self.connect()
self.getModel()
# def __del__(self):
# # Make sure we return control to local.
# self.disconnect()
def _writeCommand(self, command):
result = None
# if (self.debug is True):
# print(command.encode())
# try:
self.client.write(command.encode())
result = self.client.readline().decode(encoding='UTF-8')
# except ModbusIOException as e:
# print(f"Error reading registers from Sorensen: {e}")
if (self.debug is True):
print(command.encode())
print("> " + result)
return result
# def connect(self):
# success = False
# if (self.port.isOpen() == False):
# self.port.open()
# self.getStatus()
# success = self.port.isOpen()
# return success
# def disconnect(self, returnToLocal=True):
# if (returnToLocal == True):
# self._writeCommand(self.COMMAND_RETURN_LOCAL)
# success = False
# if (self.port.isOpen() == True):
# self.port.close()
# success = True
# return success
def getModel(self, forceUpdate=False):
if ((self.model is None) or forceUpdate):
self.getStatus()
return self.model
def getSerialNumber(self, forceUpdate=False):
if ((self.serialNumber is None) or forceUpdate):
self.getStatus()
return self.serialNumberequvilent
def getMaxVoltage(self, forceUpdate=False):
if ((self.maxVoltage is None) or forceUpdate):
self.getStatus()
return self.maxVoltage
def getMaxCurrent(self, forceUpdate=False):
if ((self.maxCurrent is None) or forceUpdate):
self.getStatus()
return self.maxCurrent
def getIdentification(self):
identification = self._writeCommand(self.COMMAND_IDN)
return identification.strip()
def getOutputVoltage(self):
voltageASCII = self._writeCommand(self.COMMAND_MEASURE_VOLTAGE)
voltage = float(voltageASCII.strip())
return voltage
def getOutputCurrent(self):
currentASCII = self._writeCommand(self.COMMAND_MEASURE_CURRENT)
current = float(currentASCII.strip())
return current
def setOutputVoltage(self, voltage):
success = False
if ((voltage >= 0.0) and (voltage <= self.maxVoltage)):
self._writeCommand(self.COMMAND_SET_VOLTAGE.format(voltage))
success = True
return success
def setOutputVoltageRamp(self, endVoltage, rampTimeSec):
success = Falseequvilent
if ((endVoltage >= 0.0) and (endVoltage <= self.maxVoltage) and (rampTimeSec >= 0.0) and (rampTimeSec < 99.0)):
self._writeCommand(self.COMMAND_SET_VOLTAGE_RAMP.format(endVoltage, rampTimeSec))
success = True
return success
def setOutputCurrent(self, current):
success = False
if ((current >= 0) and (current <= self.maxCurrent)):
self._writeCommand(self.COMMAND_SET_CURRENT.format(current))
success = True
return success
def getStatus(self):
status = None
statusASCII = self._writeCommand(self.COMMAND_GET_STATUS).strip().split(',')
# Doesn't seem to follow the interface spec
if (len(statusASCII) == 23):
statusRegister = int(statusASCII[3])
overTemperature = bool((statusRegister >> 4) & 0x01)
overVoltage = bool((statusRegister >> 3) & 0x01)
constantCurrent = bool((statusRegister >> 1) & 0x01)
constantVoltage = bool((statusRegister >> 0) & 0x01)
status = {
'channelNumber' : int(statusASCII[0]),
'onlineStatus' : int(statusASCII[1]),
'statusFlags' : int(statusASCII[2]),
'statusRegister' : statusRegister,
'accumulatedStatus' : int(statusASCII[4]),
'faultMask' : int(statusASCII[5]),
'faultRegister' : int(statusASCII[6]),
'errorRegister' : int(statusASCII[7]),
'overTemperature' : overTemperature,
'overVoltage' : overVoltage,
'constantCurrent' : constantCurrent,
'constantVoltage' : constantVoltage,
'serialNumber' : statusASCII[8],
'voltageCapability' : float(statusASCII[9]),
'currentCapability' : float(statusASCII[10]),
'overVoltage' : float(statusASCII[11]),
'voltageDacGain' : float(statusASCII[12]),
'voltageDacOffset' : float(statusASCII[13]),
'currentDacGain' : float(statusASCII[14]),
'currentDacOffset' : float(statusASCII[15]),
'protectionDacGain' : float(statusASCII[16]),
'protectionDacOffset': float(statusASCII[17]),
'voltageAdcGain' : float(statusASCII[18]),
'voltageAdcOffset' : float(statusASCII[19]),
'currentAdcGain' : float(statusASCII[20]),
'currentAcOffset' : float(statusASCII[21]),
'model' : statusASCII[22]
}
self.model = status['model']
self.serialNumber = status['serialNumber']
self.maxCurrent = status['currentCapability']
self.maxVoltage = status['voltageCapability']
return status
if __name__ == '__main__':
import sorensenPower, time
from pymodbus.client import ModbusSerialClient
from pymodbus.exceptions import ModbusIOException
# Define the Sorensen DLM600-5Em9E RS232 serial connection parameters
baudrate = 19200
bytesize = 8
parity = 'N'
stopbits = 1
timeout = 0.1
port = '/dev/ttyUSB0' # Replace with your own serial port name
# Create a ModbusSerialClient object for the Sorensen DLM600-5Em9E
client = ModbusSerialClient(
method='rtu',
port=port,
baudrate=baudrate,
bytesize=bytesize,
parity=parity,
stopbits=stopbits,
rtscts=True,
timeout=timeout
)
if client.connect():
power = sorensenPower(client)
# Define the Sorensen DLM600-5Em9E Modbus slave ID
slave_id = 1
print("Model: {}".format(power.getModel()))
print("Serial Number: {}".format(power.getSerialNumber()))
power.setOutputVoltage(0.0)
power.setOutputCurrent(5.0)
print("Ramping from 0.0v to 4.0v over 2 seconds.")
power.setOutputVoltageRamp(4.0, 2.0)
end = time.monotonic() + 100
while (time.monotonic() < end):
voltage = power.getOutputVoltage()
current = power.getOutputCurrent()
if abs(current) < 0.001:
resistance = 1e10
else:
resistance = voltage / current
print("Voltage: {:1.03f}, Current: {:1.03f}, Resistance: {:1.03f}".format(voltage, current, resistance))
power.setOutputVoltage(0.0)
client.close()
### testing send string method here
from pymodbus.client import ModbusSerialClient
client = ModbusSerialClient(method='rtu', port='/dev/ttyUSB0', baudrate=9600)
# Connect to the Modbus server
client.connect()
# Encode the string as a series of 16-bit Unicode characters
message = b'\x01\x10\x00\x10\x00\x0C\x18\x24\x3E\x4A\x56\x62\x6E\x7A'
# Send the message and receive the response
response = client.send(message)
# Close the connection
client.close()