SCPI只是程控仪器,还需要主机端驱动。因为是通过USB/CDC连接,所以Python的PySerial驱动它最简单。以下是一个循环发射"PING"字符串的过程。
#!/usr/bin/env python
import serial
import time
import perference
import listport
ports = listport.serial_ports()
if len(ports):
print "{0} ports are available".format(len(ports))
port = ports[0]
print "port {0} is selected".format(port)
ser = serial.Serial(port, 9600, timeout = 0.5)
print("Port is opened\r\n")
setups = [
"RA:MOD LORA",
"RA:FREQ 434",
"RA:PAS PA_BOOST",
"RA:OCP 170",
"RA:BGR 7",
"RA:POW 15",
"RA:OP STB",
]
queries = [
"*IDN?",
"RA:MOD?",
"RA:FREQ?",
"RA:PAS?",
"RA:OCP?",
"RA:BGR?",
"RA:POW?",
"RA:OP?",
]
actions = [
"RA:LORA:TXC 1",
"RA:FI \"PING\"",
"RA:OP STB",
]
for q in setups:
ser.write(q+'\r\n')
print(ser.readline().strip())
time.sleep(0.1)
ser.write('\r\n')
time.sleep(1)
ser.flush()
for q in queries:
ser.write(q+'\r\n')
print(ser.readline().strip())
print(ser.readline().strip())
time.sleep(0.1)
ser.write('\r\n')
time.sleep(1)
while True:
for q in actions:
ser.write(q+'\r\n')
print(ser.readline().strip())
time.sleep(0.5)
ser.close()
可改进的地方
- time.sleep()是堵塞型的,需要改成生成器;
- 不是OOP的,有重复代码;
- SCPI比AT指令集要简单,但是发现SCPI接收端丢失字符串现象,不知道是接收固件有问题,还是哪里有问题,需要LA去跟踪。