Skip to content

Commit

Permalink
Add discover feature
Browse files Browse the repository at this point in the history
Includes example script demonstrating how to use the discover()
function and its callbacks.
  • Loading branch information
lundmar committed Nov 29, 2022
1 parent c24b2dd commit 60345e3
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 10 deletions.
27 changes: 19 additions & 8 deletions README.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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
```
42 changes: 42 additions & 0 deletions 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)

File renamed without changes.
40 changes: 38 additions & 2 deletions lxi.py
Expand Up @@ -29,7 +29,7 @@
#

#
# Python bindings for liblxi (work in progress)
# Python bindings for liblxi
#

from ctypes import *
Expand All @@ -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
Expand Down Expand Up @@ -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_()

0 comments on commit 60345e3

Please sign in to comment.