diff --git a/README.md b/README.md index 577c340..c8921e7 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,6 @@ Python bindings for liblxi. -## Features - -The python bindings implements all of the liblxi API except the discover -functions. - ## Requirements Make sure you have python3 and liblxi installed. @@ -35,17 +30,33 @@ pkg install liblxi python3 ## Run the example code +Note: + +If it fails to find the liblxi library you can try preload it with the specific +path to the library. For example: + +``` +LD_PRELOAD=$HOME/opt/lib/liblxi.so python3 send_receive.py 192.168.0.157 +``` + +### Send and receive example + Run the example test script with the IP of your instrument as argument: ``` -$ python3 test.py 192.168.0.157 +$ PYTHONPATH=. python3 ./examples/send_receive.py 192.168.0.157 Sent 5 bytes Sent command: *IDN? Received 49 bytes Received message: Rohde&Schwarz,RTB2004,1333.1005k04/113192,02.400 ``` -If it fails to find the liblxi library you can try preload it like so: +### Discover example +Search for instruments: ``` -LD_PRELOAD=/usr/lib/liblxi.so python3 test.py 192.168.0.157 +PYTHONPATH=. python3 ./examples/discover.py +Broadcasting on lo using address 127.0.0.1 +Broadcasting on enxe4b97a86fdad using address 192.168.0.255 + Found Rohde&Schwarz,NGM202,3638.4472k03/101403,03.068 00A8F863604 on address 192.168.0.107 +Broadcasting on virbr0 using address 192.168.122.255 ``` diff --git a/examples/discover.py b/examples/discover.py new file mode 100644 index 0000000..dbf6e26 --- /dev/null +++ b/examples/discover.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# liblxi-python discover example script + +import lxi + + +# Callback functions which are called during discovery + +def broadcast(address, interface): + address = str(address, 'ascii') + interface = str(interface, 'ascii') + print("Broadcasting on " + interface + " using address " + address) + return + +def device(address, id): + address = str(address, 'ascii') + id = str(id, 'ascii') + print(" Found " + id + " on address " + address) + return + +def service(address, id, service, port): + address = str(address, 'ascii') + id = str(id, 'ascii') + service = str(service, 'ascii') + port = str(port) + print("Found " + id + " on address " + address) + print(" " + service + " service on port " + port) + +# Initialize library +lxi.init() + +# Search for devices +timeout = 1000 +info = lxi.lxi_info_class() +info.broadcast = broadcast +info.device = device +info.service = service + +device = lxi.discover(info, timeout, lxi.discover_protocol.DISCOVER_VXI11) +# device = lxi.discover(info, timeout, lxi.discover_protocol.DISCOVER_MDNS) + diff --git a/test.py b/examples/send_receive.py similarity index 100% rename from test.py rename to examples/send_receive.py diff --git a/lxi.py b/lxi.py index 762f80b..394ae5c 100644 --- a/lxi.py +++ b/lxi.py @@ -29,7 +29,7 @@ # # -# Python bindings for liblxi (work in progress) +# Python bindings for liblxi # from ctypes import * @@ -56,11 +56,40 @@ # Load the library lib = cdll.LoadLibrary(liblxi) - + # Define functions def init(): lib.lxi_init() +# Define types +class LXI_INFO(Structure): + _fields_ = [("broadcast", CFUNCTYPE(None, c_char_p, c_char_p )), + ("device", CFUNCTYPE(None, c_char_p, c_char_p)), + ("service", CFUNCTYPE(None, c_char_p, c_char_p, c_char_p, c_int))] + +class lxi_info_class: + def broadcast(self, address, interface): + return + + def device(self, address, id): + return + + def service(self, address, id, service, port): + return + +def discover(info: lxi_info_class, timeout: int, type: int): + lib.lxi_discover.argtypes = c_void_p, c_int, c_int + lib.lxi_discover.restype = c_int + BROADCAST_FUNC = CFUNCTYPE(None, c_char_p, c_char_p) + broadcast_func = BROADCAST_FUNC(info.broadcast) + DEVICE_FUNC = CFUNCTYPE(None, c_char_p, c_char_p) + device_func = DEVICE_FUNC(info.device) + SERVICE_FUNC = CFUNCTYPE(None, c_char_p, c_char_p, c_char_p, c_int) + service_func = SERVICE_FUNC(info.service) + c_info_p = pointer(LXI_INFO(broadcast_func, device_func, service_func)) + status = lib.lxi_discover(c_info_p, c_int(timeout), c_int(type)) + return status + def connect(address, port: int, name, timeout: int, protocol: int): lib.lxi_connect.argtypes = c_char_p, c_int, c_char_p, c_int, c_int lib.lxi_connect.restype = c_int @@ -96,3 +125,10 @@ def __init__(self): self.RAW = 1 protocol = _protocol_() + +class _discover_protocol_: + def __init__(self): + self.DISCOVER_VXI11 = 0 + self.DISCOVER_MDNS = 1 + +discover_protocol = _discover_protocol_()