使用Python编写驱动的主要优势在于驱动不再需要编译。当前在君光303PRO-NG设备上进行测试,其他型号的FSU暂时没有添加这个支持。
隨着第三代FSU的逐步铺开,CPU性能、内存容量和硬盘容量将不再是问题,PyDriver的跨平台特性将得到更多的应用。
当生成代码时,会同时生成C++和Python的驱动代码,python驱动在PyDrivers文件夹下。
PyDriver的驱动和普通的C++驱动,在配置方式下是一样的,例如:
DTSD3366D生成的C+驱动名为:
dtsd3366d
PyDriver的驱动名为:
PyDTSD3366D
php代码也是dtsd3366d。
当在网站进行配置时,PyDriver的驱动程序需要配置为: pymodbusdevice-PyDTSD3366D
这个的意思是:使用pymodbusdevice作为主驱动,PyDTSD3366D作为2级驱动。
其他都一样,FsuOS会自动使用dtsd3366d的php代码和B接口代码。
import PyFsuos2
import numpy as np
class PyDTSD3366D(PyFsuos2.PyModbusImp):
DTSD3366D_R3_9=101
DTSD3366D_R3_366=102
DTSD3366D_R3_378=103
DTSD3366D_R3_402=104
DTSD3366D_R3_4096=105
DTSD3366D_R3_4144=106
DTSD3366D_R3_4192=107
def __init__(self,*args, **kwargs):
super().__init__(*args, **kwargs)
self.r3_9 = np.zeros(2,np.uint16)
self.r3_366 = np.zeros(12,np.uint16)
self.r3_378 = np.zeros(24,np.uint16)
self.r3_402 = np.zeros(8,np.uint16)
self.r3_4096 = np.zeros(2,np.uint16)
self.r3_4144 = np.zeros(2,np.uint16)
self.r3_4192 = np.zeros(2,np.uint16)
self.state = 100
def InitSetting(self, settingRoot):
print("InitSetting")
def RunCheckThreshold(self):
print("RunCheckThreshold")
RawData = self.r3_9.tobytes() + self.r3_366.tobytes() + self.r3_378.tobytes() + self.r3_402.tobytes() + self.r3_4096.tobytes() + self.r3_4144.tobytes() + self.r3_4192.tobytes()
self.set_cdata(RawData)
def process_payload(self, type, len):
if self.state == PyDTSD3366D.DTSD3366D_R3_9:
self.r3_9 = np.copy(self.tab_reg[0:2])
self.state = PyDTSD3366D.DTSD3366D_R3_366
self.modbus_read_registers(366, 12)
elif self.state == PyDTSD3366D.DTSD3366D_R3_366:
self.r3_366 = np.copy(self.tab_reg[0:12])
self.state = PyDTSD3366D.DTSD3366D_R3_378
self.modbus_read_registers(378, 24)
elif self.state == PyDTSD3366D.DTSD3366D_R3_378:
self.r3_378 = np.copy(self.tab_reg[0:24])
self.state = PyDTSD3366D.DTSD3366D_R3_402
self.modbus_read_registers(402, 8)
elif self.state == PyDTSD3366D.DTSD3366D_R3_402:
self.r3_402 = np.copy(self.tab_reg[0:8])
self.state = PyDTSD3366D.DTSD3366D_R3_4096
self.modbus_read_registers(4096, 2)
elif self.state == PyDTSD3366D.DTSD3366D_R3_4096:
self.r3_4096 = np.copy(self.tab_reg[0:2])
self.state = PyDTSD3366D.DTSD3366D_R3_4144
self.modbus_read_registers(4144, 2)
elif self.state == PyDTSD3366D.DTSD3366D_R3_4144:
self.r3_4144 = np.copy(self.tab_reg[0:2])
self.state = PyDTSD3366D.DTSD3366D_R3_4192
self.modbus_read_registers(4192, 2)
elif self.state == PyDTSD3366D.DTSD3366D_R3_4192:
self.r3_4192 = np.copy(self.tab_reg[0:2])
self.RoundDone()
return False
return True;
def RefreshStatus(self):
print("RefreshStatus")
self.state = PyDTSD3366D.DTSD3366D_R3_9;
self.modbus_read_registers(9, 2);
return True
def Get_Value(self, data_id, var_name):
print("Get_Value")
def DeviceIoControl(self, ioControlCode, inBuffer, inBufferSize, outBuffer, outBufferSize, bytesReturned):
print("DeviceIoControl")
明显看出python的代码要简短不少