import serial import time import numpy as np timedelta = 0.02 class AG_304_FS: def __init__(self,adress): #establish connection to device try: self.inst = serial.Serial(adress, 9600, timeout=1) # Replace with your COM port and baudrate print(f'connected to {adress}') except: print(f'could not connect to {adress}') return time.sleep(1) def __del__(self): self.inst.close() print('Disconnected') def read_pressure(self): '''read the pressure from the controller (Window 224, see manual)''' hex_string = "028032323430033837" # pressure # Convert the hex string to bytes bytes_to_send = bytes.fromhex(hex_string) # Send the bytes to the COM port self.inst.write(bytes_to_send) # Read response data_b = self.inst.read(100) # read up to hundred bytes or timeout is reached val_b = data_b[6:13] try: val = float(val_b) except ValueError: val = 0 return(val) def read_temp(self): '''read the pump temperature (Window 204)''' hex_string = "028032303430033835" # temperature # Convert the hex string to bytes bytes_to_send = bytes.fromhex(hex_string) # Send the bytes to the COM port self.inst.write(bytes_to_send) # Read response data_b = self.inst.read(100) # read up to hundred bytes or timeout is reached val_b = data_b[6:12] #get rid of additional bytes try: val = float(val_b) except ValueError: val = 0 return(val) def read_status(self): '''read pump status (window 205)''' hex_string = "028032303530033834" # status # Convert the hex string to bytes bytes_to_send = bytes.fromhex(hex_string) # Send the bytes to the COM port self.inst.write(bytes_to_send) # Read response data_b = self.inst.read(100) # read up to hundred bytes or timeout is reached val_b = data_b[6:12] #get rid of additional bytes status = ['Stop', 'Waiting intlk', ' Starting', 'Autotuning', 'Braking', 'Normal', 'Fail'] try: val = int(val_b) except ValueError: val = 6 return(status[val]) def read_freq(self): '''read pump frequency [Hz] (window 203)''' hex_string = "028032303330033832" # temperature # Convert the hex string to bytes bytes_to_send = bytes.fromhex(hex_string) # Send the bytes to the COM port self.inst.write(bytes_to_send) # Read response data_b = self.inst.read(100) # read up to hundred bytes or timeout is reached val_b = data_b[6:12] #get rid of additional bytes try: val = int(val_b) except ValueError: val = 0 return(val) def read_fan_status(self): '''reads the status of the fan (window 144)''' hex_string = "028031343430033832" # Convert the hex string to bytes bytes_to_send = bytes.fromhex(hex_string) # Send the bytes to the COM port self.inst.write(bytes_to_send) # Read response data_b = self.inst.read(100) # read up to hundred bytes or timeout is reached val_b = data_b[5:7] #get rid of additional bytes (Response is different here compared to others) val = int(val_b) return(val) def set_fan_status(self,on): '''sets the status of the fan (window 144)''' if on == True or on == 'On' or on == 'ON' or on == 'on' or on == 1: hex_string = "02803134333132034236" # sets external fan activation to on # hex_string = "02803134343130034233" else: hex_string = "02803134333131034235" # sets external fan activation to off # Convert the hex string to bytes bytes_to_send = bytes.fromhex(hex_string) # Send the bytes to the COM port self.inst.write(bytes_to_send) # Read response data_b = self.inst.read(100) # read up to hundred bytes or timeout is reached print(data_b) val_b = data_b[5:7] #get rid of additional bytes (Response is different here compared to others) val = int(val_b) return(val) def start_pump(self): '''start pump (Window 000)''' hex_string = "02803030303131034233" # Convert the hex string to bytes bytes_to_send = bytes.fromhex(hex_string) # Send the bytes to the COM port self.inst.write(bytes_to_send) # Read response data_b = self.inst.read(100) # read up to hundred bytes or timeout is reached # if data_b[2:3] == b'\x06': # print('Starting Pump') # else: # print('Pump cannot be started.') def stop_pump(self): '''stop pump (Window ...)''' hex_string = "02803030303130034232" # Convert the hex string to bytes bytes_to_send = bytes.fromhex(hex_string) # Send the bytes to the COM port self.inst.write(bytes_to_send) # Read response data_b = self.inst.read(100) # read up to hundred bytes or timeout is reached # if data_b == data_b[2:3] == b'\x06': # print('Stop Pump') # else: # print('Pump cannot be stopped.') # test area # adress = 'COM2' # ag = AG_304_FS(adress) # print(ag.read_pressure()) # ag.__del__() ##Open the COM port # ser = serial.Serial('COM2', 9600, timeout=1) # Replace with your COM port and baudrate # # Define the hex string # hex_string = "028032303530033834" # pump status # hex_string = "028032323430033837" # pressure # # Convert the hex string to bytes # bytes_to_send = bytes.fromhex(hex_string) # # Send the bytes to the COM port # ser.write(bytes_to_send) # # Read response # s = ser.read(100) # read up to ten bytes (timeout) # print(s) # # Close the COM port # ser.close()