# 使用pyserial控制ITECH电压电流输出
列出当前机器所有的串口,并选择合适的端口
```python
import serial, time
```
设置串口,baudrate需从ITECH中的configuration中读取到。timeout主要是防止读取的时候程序被阻塞,建立发送的方法
```python
ser = serial.Serial()
ser.baudrate = 4800
ser.timeout=2
ser.port = 'COM4'
def send(cmd):
"""
Send out a command, will automaticlly add \n
at the end of command
"""
if not cmd.endswith("\n"):
cmd += "\n"
return ser.write(cmd.encode())
```
建立连接
```python
ser.open()
ser.is_open
```
下面的命令将会使得机器发出风鸣声,如果听到则表明连接成功。
```python
send("SYSTem:BEEPer")
```
此命令用来控制电源单路输出状态,使用此命令之前,您必须先切换至相应的通道
```python
send("SOUR:CHAN:OUTP:STAT ON") #send("SOUR:CHAN:OUTP:STAT OFF")
```
这条命令用来设置SOURCE METER为远程控制模式。(近程模式)
```python
send("SYST:REM") # send("SYSTem:LOCal")
```
询问程序版本号
```python
out = b''
send("SYSTem:VERSion?")
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != b'':
print(out)
```
选择通道2, 并询问当前电压值
```python
send("INST:SEL CH2")
time.sleep(1)
out = b''
send("SOUR:VOLT:LEV?")
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != b'':
print(out)
```
更改通道2的当前电压为1V, 并询问当前电压值
```python
send("VOLT:LEV 1")
time.sleep(1)
out = b''
send("SOUR:VOLT?")
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != b'':
print(out)
```
询问当前步进并设置步进值, 按照步进调低电压(按照步进调高电压)
```python
out = b''
send("VOLTage:STEP?")
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != b'':
print(out)
out = b''
send("VOLTage:STEP 0.001")
time.sleep(1)
send("VOLT:DOWN") #(send("VOLT:UP"))
```