這陣子需要寫一些 Python 程式用儀器量測訊號,因此將常用的部分模組化,下面就是程式模組,因為只用了 COM 及 USB-TMC,所以僅將這兩部分模組化。
INSTRUMENT.py
# (C) 2017, Bridan Wang, CC BY-NC-SA 3.0 TW http://4rdp.blogspot.tw/
from __future__ import division, unicode_literals, print_function, absolute_import
import serial
import visa
import time
class COM:
def __init__(self):
global inst1
inst1 = serial.Serial()
def OPEN(self, pp, rate = 9600):
global inst1
inst1.close()
inst1.port = "COM"+str(pp)
inst1.baudrate = rate
inst1.timeout = 5
inst1.open()
time.sleep(1)
self.stat = 2
def CLOSE(self):
global inst1
inst1.close()
def SEND(self, ss):
global inst1
ss = str(ss) + b"\r\n"
inst1.write(ss)
def QUERY(self, ss=""):
global inst1
if ss != "":
self.SEND(ss)
#time.sleep(1)
qqx = inst1.readline()
return qqx
def QUERY_VALUE(self, q):
a = self.QUERY(q)
return float(a)
class USB:
def __init__(self):
global usb
try:
usb = visa.get_instruments_list()
#print(usb)
except:
usb = []
def OPEN(self, sel=0):
global usb
global inst
try:
inst = visa.Instrument(usb[sel], term_chars=b"\n", timeout=5)
self.stat = 2
except:
self.stat = 0
return self.stat
def CLOSE(self):
global inst
inst.close()
def SEND(self, ss):
global inst
inst.write(ss)
def QUERY(self, ss=""):
global inst
if ss != "":
self.SEND(ss)
#time.sleep(1)
qqx = inst.read()
return qqx
def QUERY_VALUE(self, q):
a = self.QUERY(q)
return float(a)
2017年7月17日 星期一
儀器 Python 模組
MAIN.py
from __future__ import division, unicode_literals, print_function, absolute_import
import INSTRUMENT
dmm = INSTRUMENT.COM()
dmm.OPEN(4)
print(dmm.QUERY('*IDN?'))
hybrid = INSTRUMENT.USB()
hybrid.OPEN()
print(hybrid.QUERY('*IDN?'))
dmm.CLOSE()
hybrid.CLOSE()
Python 因為容易模組化程式,使得程式設計可以精簡,歡迎大家將它改良。
標籤:
Python 程式
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言