-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (56 loc) · 1.75 KB
/
main.py
File metadata and controls
68 lines (56 loc) · 1.75 KB
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
import time
import serial
def init_comPort( comPort:str ):
global ser
ser = serial.Serial(
port=comPort,
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1
)
def init_powerSupply( ):
idn = send_cmd( "*IDN?" )
print( idn )
send_cmd( "SYST:REM", expect_response=False ) #Set power supply in remote
def set_voltage( voltage:float ):
send_cmd( f"SOUR:VOLT {voltage}", expect_response=False )
def set_max_current( current:float ):
send_cmd( f"SOUR:CURR {current}", expect_response=False )
def turn_ON_powerSupply( ):
send_cmd( "OUTP ON", expect_response=False )
def turn_OFF_powerSupply( ):
send_cmd( "OUTP OFF", expect_response=False )
def measure_voltage( ) -> float:
voltage = send_cmd("MEAS:VOLT?")
print( f"Volatge meausre: {voltage} V" )
return voltage
def measure_current( ) -> float:
current = send_cmd("MEAS:CURR?")
print( f"Current measure: {current} A" )
return current
def measure_power( ) -> float:
power = send_cmd("MEAS:POW?")
print( f"Power measure: {power} W" )
return power
def send_cmd( command:str, expect_response=True ) -> str:
ser.write( ( command + '\n' ).encode( ) )
if expect_response:
time.sleep( 0.1 )
return ser.readline( ).decode( ).strip( )
return None
def Close_Power_Supply_COM_Port( ):
ser.close
if __name__ == '__main__':
init_comPort( "COM13" )
init_powerSupply( )
set_voltage( 13.4 )
set_max_current( 5 )
turn_ON_powerSupply( )
time.sleep( 1 )
measure_voltage( )
measure_current( )
measure_power( )
turn_OFF_powerSupply( )
Close_Power_Supply_COM_Port( )