setting variables in OPC server
Posted: Thu Mar 11, 2010 2:37 am
For my final work at university I am making a way to connect a dsPIC to SCADA over OPC server, so I want to share with everyone my proof of concept code in python...
for this you need a python (32 bit) and this (I used 2.5 version)
Pyserial - http://pyserial.sourceforge.net/
OpenOPC - http://openopc.sourceforge.net/
Pyro - http://pyro.sourceforge.net/
Python for windows extensions - http://sourceforge.net/projects/pywin32/
Code: Select all
import serial, binascii, struct,time,OpenOPC
ser = serial.Serial(0, 38400) #for com1: use 0, for com2 use 1 etc...38400 is speed of com port
opc = OpenOPC.client()
opc.connect('Matrikon.OPC.Simulation')
buff=25
sampletime=0.2
while 1:
s = ser.read(buff)
prvi=s.find('\x05')
print '1. kanal:'
print struct.unpack('<H',s[prvi+1]+s[prvi+2])[0]
opc['Bucket Brigade.data1'] = struct.unpack('<H',s[prvi+1]+s[prvi+2])[0]
prvi=s.find('\x15')
print '2. kanal:'
print struct.unpack('<H',s[prvi+1]+s[prvi+2])[0]
opc['Bucket Brigade.data2'] = struct.unpack('<H',s[prvi+1]+s[prvi+2])[0]
print
print
time.sleep(sampletime)
ser.close()
opc.close()
Pyserial - http://pyserial.sourceforge.net/
OpenOPC - http://openopc.sourceforge.net/
Pyro - http://pyro.sourceforge.net/
Python for windows extensions - http://sourceforge.net/projects/pywin32/