craft-software/Legacy/Instrument_Drivers/drivers/Keysight_34461A.py
2025-07-04 15:52:40 +02:00

90 lines
3.4 KiB
Python

import socket
import time
import re
Buffer = 2048
class Keysight34461A:
def __init__(self, ip):
#establishes ethernet connection to the device with ip "ip"
try:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((ip, 5024))
time.sleep(0.2)
self.s.recv(Buffer)
except:
print(f'could not connect to device with IP: {ip}')
def __del__(self):
#closes the connection
self.s.send(b'ABOR\r\n')
self.s.close()
def configure(self,VoC = 'VOLT', DC = 'DC', meas_range = 'AUTO', trig_source = 'IMM', trig_count = 'INF'):
#configures the device to measure the desired value:
#VoC(Volt or Current): measure volt or current. Values: "VOLT";"CURR";'
#DC(DirectCureent): measure DC or AC. Values: "DC";"AC"
#meas_range:sets the measurement range. Values:'AUTO' maybe others, I cant't find them in the manual
#trig_source: sets how the device in triggered. Values: 'IMM', 'BUS', 'EXT', 'INT' (see manual p.434)
#trig_count:How many triggers are excepted before device stops measurement. Values:'INF', (1 to 1e6)
command = f'CONF:{VoC}:{DC} {meas_range}\r\n'
self.s.send(command.encode('utf-8'))
time.sleep(0.2)
command = f'CONF:{VoC}:RANG {meas_range}\r\n'
self.s.send(command.encode('utf-8'))
time.sleep(0.2)
command = f'TRIG:SOUR {trig_source}\r\n'
self.s.send(command.encode('utf-8'))
time.sleep(0.2)
command = f'TRIG:COUN {trig_count}\r\n'
self.s.send(command.encode('utf-8'))
time.sleep(0.2)
def configure_temp(self,Sensor = 'TC', Type = 'R', trig_source = 'IMM', trig_count = 'INF'):
#configures the device to measure temperature:
#Sensor: <probe_type>: {FRTD|RTD|FTHermistor|THERmistor|TCouple} (Manual p.451)
#Type: 85 (only possible value for RTD/FRTD), 5000 (only possible value for THERmistor/FTHermistor), or E, J, K, N, R, T (TCouple).
#trig_source: sets how the device in triggered. Values: 'IMM', 'BUS', 'EXT', 'INT' (see manual p.434)
#trig_count:How many triggers are excepted before device stops measurement. Values:'INF', (1 to 1e6)
command = f'CONF:TEMP {Sensor},{Type}\r\n'
print(command)
self.s.send(command.encode('utf-8'))
time.sleep(0.2)
command = f'TRIG:SOUR {trig_source}\r\n'
self.s.send(command.encode('utf-8'))
time.sleep(0.2)
command = f'TRIG:COUN {trig_count}\r\n'
self.s.send(command.encode('utf-8'))
time.sleep(0.2)
def initiate_measurement(self):
#initializes measurement
self.s.send(b'INIT\r\n')
time.sleep(0.2)
def stop_measurement(self):
#stops measurement
self.s.send(b'ABOR\r\n')
time.sleep(0.2)
def read(self):
#gets current value from device
self.s.send(b'R? 1\r\n')
time.sleep(0.2)
data = str(self.s.recv(Buffer))[14:]
try:
data = float(data.split('\\')[0])
except:
data = 0
self.s.send(b'R?\r\n')
time.sleep(0.2)
self.s.recv(Buffer)[14:]
return data