bionicapi module¶
filename: | bionicapi.py |
---|---|
author: | Per Brosstad Tordivel AS, per@tordivel.no |
version: | 2.0 |
copyright: | 2000-2015 Tordivel AS |
license: | Tordivel AS’ Scorpion Python Module License |
interface class to pybionic2x.pyd tested on USB-4750, USB-4711A, PCI-1730, Advantech DemoDevice
supports: bit read/write, byte read/write, analog read/write
Revision history:
1.0.0.4 - 14 March 2013: added do_readbit and do_readbyte
1.0.0.3 - 24 August 2011: edited do_writebit to set only one bit at a time (0-7)
1.0.0.2 - 05 August 2011: start/stop snap test functions added
1.0.0.1 - 28 July 2011: initial version
1. open a device (default device=0) - true for most setups, print device features, then close
try:
import bionicapi
io=bionicapi.BionicAPI(1)
if io.okdevice==bionicapi.R_OK:
print " device ok"
PropFeatures=io.DEVICEFEATURES
for feature in PropFeatures:
print feature,PropFeatures[feature]
except:
print 'install advantech module and sw'
2. read isolated digital inputs
# read bit 0 isolated input
ok,txt,err_or_val = io.di_readbit(bionicapi.PORT_ID, 0)
if ok==0:
print 'di_readbit: isolated input 0 is %d' % err_or_val
else:
print "di_readbit: error %s, subcode %d when reading" % (io.OPCODES[ok], err_or_val)
# read bit 8 isolated input
ok,txt,err_or_val = io.di_readbit(bionicapi.PORT_ID+1, 0)
if ok==0:
print 'di_readbit: isolated input 8 is %d' % err_or_val
else:
print "di_readbit: error %s, subcode %d when reading" % (io.OPCODES[ok], err_or_val)
#read byte 0 isolated input
ok,txt,err_or_val = io.di_readbyte(bionicapi.PORT_ID)
if ok==0:
print 'di_readbyte: isolated input byte 0 is %d' % err_or_val
else:
print "di_readbyte: error %s, subcode %d when reading" % (io.OPCODES[ok], err_or_val)
#read byte 1 isolated input
ok,txt,err_or_val = io.di_readbyte(bionicapi.PORT_ID+1)
if ok==0:
print 'di_readbyte: isolated input byte 1 is %d' % err_or_val
else:
print "di_readbyte: error %s, subcode %d when reading" % (io.OPCODES[ok], err_or_val)
2.write isolated outputs
# write bit 0 isolated output
ok,txt,err_or_val = io.do_writebit(bionicapi.PORT_ID, 1, 1)
if ok==0:
print 'do_writebit: isolated output 0 is %d' % 1
else:
print "do_writebit: error %s, subcode %d when writing" % (io.OPCODES[ok], err_or_val)
# write byte 0 isolated output
ok,txt,err_or_val = io.do_writebyte( bionicapi.PORT_ID,0xff,1)
if ok==0:
print 'do_writebyte: isolated output byte (bits 0..7) is %d' % 1
else:
print "do_writebyte: error %s, subcode %d when writing" % (io.OPCODES[ok], err_or_val)
# write byte 1 isolated output
ok,txt,err_or_val = io.do_writebyte( bionicapi.PORT_ID+1,0xff,1)
if ok==0:
print 'do_writebyte: isolated output byte (bits 8..15) is %d' % 1
else:
print "do_writebyte: error %s, subcode %d when writing" % (io.OPCODES[ok], err_or_val)
3. Analog IO
# read analog input value from 0. channel
ok,txt,err_or_val = io.ad_readanalog(0)
if ok==0:
print 'ad_readanalog: 0. channel analog input value is %d' % err_or_val
else:
print "ad_readanalog: error %s, subcode %d when reading" % (io.OPCODES[ok], err_or_val)
# read analog input value from 2. channel
ok,txt,err_or_val = io.ad_readanalog(2)
if ok==0:
print 'ad_readanalog: 2. channel analog input value is %d' % err_or_val
else:
print "ad_readanalog: error %s, subcode %d when reading" % (io.OPCODES[ok], err_or_val)
# write analog input value to 0. output channel
ok,txt,err_or_val = io.da_writeanalog(0, 1)
if ok==0:
print 'da_writeanalog: 0. channel analog output value is %f' % 1
else:
print "da_writeanalog: error %s, subcode %d when reading" % (io.OPCODES[ok], err_or_val)
# write analog input value to 1. output channel
ok,txt,err_or_val = io.da_writeanalog(1, 5.5)
if ok==0:
print 'da_writeanalog: 1. channel analog output value is %f' % 5.5
else:
print "da_writeanalog: error %s, subcode %d when reading" % (io.OPCODES[ok], err_or_val)
4. Close device
io.close()