Yoctopuce Yocto-IO Mode D'emploi page 41

Table des Matières

Publicité

import os,
sys
from
yocto_api
import
from
yocto_digitalio
def
usage():
scriptname
= os.path.basename(sys.argv[0])
print("Usage:")
print(scriptname +
print(scriptname +
print(scriptname +
print('Example:')
print(scriptname +
sys.exit()
def
die(msg):
sys.exit(msg +
if len(sys.argv) <
usage()
target
= sys.argv[1].upper()
# Setup the API to use local USB devices
errmsg
= YRefParam()
if YAPI.RegisterHub("usb", errmsg) !=
sys.exit("init error"
if
target
==
'ANY':
# retreive any Relay then find its serial #
io
= YDigitalIO.FirstDigitalIO()
if
io
is
None:
die('No module
m
= io.get_module()
target
= m.get_serialNumber()
print('using '
+
target)
io
=
YDigitalIO.FindDigitalIO(target +
if not
(io.isOnline()):
die('device not
# lets configure the channels direction
# bits 0..1 as output
# bits 2..3 as input
io.set_portDirection(0x03)
io.set_portPolarity(0)
io.set_portOpenDrain(0)
print("Channels 0..1 are configured as outputs and channels
print("are configured as inputs, you can connect some inputs to
print("ouputs and see what
outputdata
=
0
while
io.isOnline():
inputdata
= io.get_portState()
line
=
""
# display part state value as binary
for
i
in range(0,
if
(inputdata &
line
+=
else:
line
+=
print(" port value = "
outputdata
=
(outputdata +
io.set_portState(outputdata)
YAPI.Sleep(1000, errmsg)
print("Module
disconnected")
YAPI.FreeAPI()
www.yoctopuce.com
*
import
*
'
<serial_number>')
'
<logical_name>')
'
any')
'
any')
' (check USB
cable)')
2:
YAPI.SUCCESS:
+
errmsg.value)
connected')
'.digitalIO')
connected')
# polarity set to regular
# No open drain
happens")
# read port values
4):
(8
>> i)) >
0:
'1'
'0'
+
line)
1)
%
4
# cycle ouput 0..3
# We could have used set_bitState as well
7. Utilisation du Yocto-IO en Python
2..3")
")
35

Publicité

Table des Matières
loading

Table des Matières