LS350.py 1.92 KB
from abstract_instrument import abstract_instrument
import socket

#==============================================================================

ALL_VAL_TYPE = "TEMP, RES"

#==============================================================================

class LS350(abstract_instrument):
	def __init__(self, adress="192.168.0.12", vtype="TEMP"):
		self.adress = adress
		self.port = 7777
		self.vtype = vtype

	def model(self):
		self.send("*IDN?")
		return self.read()

	def connect(self):
		try:
			print('Connecting to device @%s:%s...' %(self.adress, self.port))
			self.sock = socket.socket(socket.AF_INET,
								 socket.SOCK_STREAM,
								 socket.IPPROTO_TCP)
			self.sock.settimeout(10.0)	# Don't hang around forever
			self.sock.connect((self.adress, self.port))
			print('  --> Ok')

			print(self.model())

			if self.vtype == "TEMP":
				1
			elif self.vtype == "RES":
				1
			else:
				print("Wrong -v argument")
				raise

		except socket.timeout:
			print("Socket timeout error during connection.")
			raise
		except socket.error as er:
			print("Socket error during connection: " + str(er))
			raise
		except Exception as er:
			print("Unexpected error during connection: " + str(er))
			raise

	def getValue(self):
		if self.vtype == 'TEMP':
			self.send('krdg? a;krdg? b;krdg? c;krdg? d')
			return self.read()
		elif self.vtype == 'RES':
			self.send('srdg? a;srdg? b;srdg? c;srdg? d')
			return self.read()

	def read(self):
		self.send("++read eoi")
		ans = ''
		nb_data_list = []
		nb_data = ''
		try:
			while ans != '\n':
				ans = self.sock.recv(1)
				nb_data_list.append(ans) # Return the number of data
			list_size = len(nb_data_list)
			for j in range (0, list_size):
				nb_data = nb_data+nb_data_list[j]
			return nb_data
		except socket.timeout:
			print "Socket timeout error when reading."
			raise

	def disconnect(self):
		self.send('MODE0')
		self.sock.close()

	def send(self, command):
		self.sock.send("%s\n"%command)