advantechapi module

filename:advantechapi.py
author:per@tordivel.no
copyright:2000-2015 Tordivel AS
license:Tordivel AS’ Scorpion Python Module License

interface class to pyadvantechpci2x.pyd supports Advantech PCI card 1730, and USB series

supports: bit read/write, byte read/write, analog read/write (not 1730)

Revision history:

1.0.0.3, 27jun2014, RL: added Python27
1.0.0.2, 16may2011, RL: added version and header
1.0.0.1, 23mar2011, PB: initial version
1. open a device (default device=0) - true for most setups, print device features, then close

  import advantechapi
  io=advantechapi.AdvantechAPI()

  if io.okdevice==advantechapi.R_OK:
     print " device ok"
     PropFeatures=io.DEVICEFEATURES
     for feature in prop:
         print feature,Propfeature[feature]
     io.close()

2. read isolated digital inputs and ttl inputs

  # read bit 0 isolated input
  ok,txt,err_or_val = io.di_readbit( advantechapi.PORT_ID,0)
  if ok==0:
     print 'isolated input 0 is %d' % err_or_val
  else:
    print "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( advantechapi.PORT_ID+1,0)
  if ok==0:
     print 'isolated input 8 is %d' % err_or_val
  else:
    print "error %s, subcode %d when reading", % (io.OPCODES[ok],err_or_val)

  # read bit 0 ttl input
  ok,txt,err_or_val = io.di_readbit( advantechapi.PORT_TTL,0)
  if ok==0:
     print 'ttl input 0 is %d' % err_or_val
  else:
    print "error %s, subcode %d when reading", % (io.OPCODES[ok],err_or_val)

  # read bit 8 ttl input
  ok,txt,err_or_val = io.di_readbit( advantechapi.PORT_TTL+1,0)
  if ok==0:
     print 'ttl input 8 is %d' % err_or_val
  else:
    print "error %s, subcode %d when reading", % (io.OPCODES[ok],err_or_val)



  ok,txt,err_or_val = io.di_readbyte( advantechapi.PORT_ID)
  if ok==0:
     print 'isolated input byte 0 is %d' % err_or_val
  else:
    print "error %s, subcode %d when reading", % (io.OPCODES[ok],err_or_val)


  ok,txt,err_or_val = io.di_readbyte( advantechapi.PORT_ID+1)
  if ok==0:
     print 'isolated input byte 1 is %d' % err_or_val
  else:
    print "error %s, subcode %d when reading", % (io.OPCODES[ok],err_or_val)


  ok,txt,err_or_val = io.di_readbyte( advantechapi.PORT_TTL)
  if ok==0:
     print 'ttl input byte 0 is %d' % err_or_val
  else:
    print "error %s, subcode %d when reading", % (io.OPCODES[ok],err_or_val)


  ok,txt,err_or_val = io.di_readbyte( advantechapi.PORT_TTL+1)
  if ok==0:
     print 'isolated input byte 1 is %d' % err_or_val
  else:
    print "error %s, subcode %d when reading", % (io.OPCODES[ok],err_or_val)

3.write isolated outputs and ttl outputs

  # write bit 0 isolated output
  ok,txt,err_or_val = io.do_writebit( advantechapi.PORT_ID,0,1)
  if ok==0:
     print 'isolated output 0 is %d' % 1
  else:
    print "error %s, subcode %d when writing, % (io.OPCODES[ok],err_or_val)

  # write bit 8 isolated output
  ok,txt,err_or_val = io.do_writebit( advantechapi.PORT_ID+1,0,1)
  if ok==0:
     print 'isolated output 8 is %d' % 1
  else:
    print "error %s, subcode %d when writing, % (io.OPCODES[ok],err_or_val)

  # write bit 0 ttl output
  ok,txt,err_or_val = io.do_writebit( advantechapi.PORT_TTL,0,1)
  if ok==0:
     print 'ttl output 0 is %d' % 1
  else:
    print "error %s, subcode %d when writing, % (io.OPCODES[ok],err_or_val)

  # write bit 8 ttl output
  ok,txt,err_or_val = io.do_writebit( advantechapi.PORT_TTL+1,0,1)
  if ok==0:
     print 'ttl output 8 is %d' % 1
  else:
    print "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( advantechapi.PORT_ID,0xff,1)
  if ok==0:
     print 'isolated output byte (bits 0..7) is %d (00000001)' % 1
  else:
    print "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( advantechapi.PORT_ID+1,0xff,1)
  if ok==0:
    print 'isolated output byte (bits 8..15) is %d (00000001)' % 1
  else:
    print "error %s, subcode %d when writing, % (io.OPCODES[ok],err_or_val)

  # write byte 0 ttl output
  ok,txt,err_or_val = io.do_writebyte( advantechapi.PORT_TTL,0xff,1)
  if ok==0:
     print 'ttl output byte (bits 0..7) is %d (00000001)' % 1
  else:
    print "error %s, subcode %d when writing, % (io.OPCODES[ok],err_or_val)

  # write byte 1 ttl output
  ok,txt,err_or_val = io.do_writebyte( advantechapi.PORT_ID+1,0xff,1)
  if ok==0:
    print 'ttl output byte (bits 8..15) is %d (00000001)' % 1
  else:
    print "error %s, subcode %d when writing, % (io.OPCODES[ok],err_or_val)
class advantechapi.AdvantechAPI(deviceno=0)
close()

ok=pci.close() - close device

da_writeanalog(channel, value)

ok,txt,error = pci.da_writeanalog(channel,value) - write analog value to specified channel

ad_readanalog(channel)

ok,txt,error|value = pci.ad_readanalog(channel) - read analog value from channel

do_writebit(port, bit, value)

ok,txt,error = pci.do_writebit(port,bit,value) - write bitvalue to specified port and bit

do_writebyte(port, mask, value)

ok,txt,error = pci.do_writbyte(port,mask,value) - write masked bitpattern in bytevalue to specified port

di_readbit(port, bit)

ok,txt,error|value = pci.do_readbit(port,bit) - read specified bit from specified port

di_readbyte(port)

ok,txt,error|value = pci.do_readbyte(port,bit) - read specified byte from specified port