Python library for Water Linked underwater modems
Project description
wl-95046 Python library for Water Linked underwater modems
Python library for communicating with Water Linked underwater modems.
The library exposes the functionality of the modem: setting configuration, getting diagnostic, sending and receiving packets.
Resources
Requirements
- Python 2.7 or Python 3.6
- pip
Supported modems
- Water Linked Modem-M64
Setup
$ python3 -m pip install --user wlmodem
or
(venv)$ python3 -m pip install wlmodem
Quick start
Connecting to a modem and configuring the mode and channel:
$ python3
>>> from wlmodem import WlModem
>>> modem = WlModem("/dev/ttyUSB0")
>>> modem.connect()
True
>>> modem.cmd_configure("a", 4)
True
>>> modem.cmd_queue_packet(b"HelloSea")
True
Usage
The WlModem
class provides an easy interface to configure, send and receive data with a Water Linked modem.
A pair of modems must be configured on the same channel and with different roles to establish communication between them.
A WlModem
object is initialized with the serial device name:
from wlmodem import WlModem
modem = WlModem("/dev/ttyUSB0")
Call connect()
to establish communication with the device
if not modem.connect():
print("Failed connecting to modem")
sys.exit(1)
Once connected we set the same channel and different roles on the pair of modems:
# On modem 1:
success = modem.cmd_configure("a", 4)
# On modem 2:
success = modem.cmd_configure("b", 4)
If the tip of the modems are close to each other (<5cm) the modems will now link up. The link status can be seen on the LEDs or by getting the diagnostic data.
if modem.cmd_get_diagnostic().get("link_up"):
print("Link is up")
Once we have connected we can use cmd_queue_packet
function to queue data for transmission.
success = modem.cmd_queue_packet(b"HelloSea")
In order to get data which one modem has received from the other modem use the get_data_packet
function.
This function will by default wait timeout
seconds until a data packet is received before returning.
If timeout
is 0 it will immediately return with a packet (if available) or None
if no packet has been received.
pkt = modem.get_data_packet(timeout=0)
if pkt:
print("Got data:", pkt)
UDP-style transfers
The WlUDPSocket
class is a higher level abstraction which allows arbitrary sized UDP-like datagrams to be transferred.
This style of transfer is suitable for short messages and has low overhead at 3 bytes for each
datagram (1 start byte, 1 checksum and 1 end byte).
The datagram will be corrupted by any single modem packet dropped (while still taking the full time to transmit),
which means it is only suitable for short datagrams.
The Modem-M64 has a payload size of 8 bytes, so the chance of success given a chance of any packet lost and number of packets transferred is given by:
chance_of_success = ((100.0 - chance_of_packet_loss) / 100) ** (number_of_packets_sent) * 100
For example, with a 5% chance of packet loss and datagram of 77 bytes (with the 3 overhead bytes this gives 10 packets):
>>> ((100.0 - 5) / 100)**10 * 100
59.87369392383787
Ie. there is a 60% chance of successful transmission.
Example of how to use the WLUDPSocket
class for transmission and reception of data:
from wlmodem import WlModem, WlUDPSocket
modem = WlModem("/dev/ttyUSB0")
modem.connect()
wl_sock = WlUDPSocket(modem)
wl_sock.send(b"There is an art, it says, or rather, a knack to flying. The knack lies in learning how to throw yourself at the ground and miss")
received = wl_sock.receive()
Simulator
A WlModemSimulator
class can be used to simulate communication with a modem without a physical modem.
Once instantiated the object will behave similarly to a Water Linked Modem-M64.
Data packets that are queued using the simulator object is returned after a timeout.
>>> from wlmodem import WlModemSimulator
>>> modem = WlModemSimulator()
>>> modem.connect()
True
>>> modem.cmd_queue_packet(b"HelloSim")
True
>>> modem.get_data_packet()
b'HelloSim'
Examples
Multiple examples showing how to use the API is available in the example/ folder.
Development
The code in this repository is unit tested with pytest
. tox
is used to automate testing on multiple Python versions.
Run unit-tests with:
pip install tox
tox
Further details on development of the repository is described in README_maintain.md
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file wlmodem-1.3.0.tar.gz
.
File metadata
- Download URL: wlmodem-1.3.0.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b32f660d481a548925e07733392455777da8345b81c7f8d75edea13571013e5 |
|
MD5 | 967b38fc390fe7c959f27ffebff62f17 |
|
BLAKE2b-256 | ba595e037331808188dd42a34492b9062a24473356c067185b321c77c192f0b8 |
File details
Details for the file wlmodem-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: wlmodem-1.3.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46b5e604a0a8a6473e6fbb46c934bbf9fa8ad2af5a6bad519443a4b2dcae2aa6 |
|
MD5 | d680287237b038b7a1cc88dcc8ae5f82 |
|
BLAKE2b-256 | 4da1ec807e5fd431587bc6f7a80f2ade299000082a6ce191d2a2fc8c3c4371e7 |
File details
Details for the file wlmodem-1.3.0-py2-none-any.whl
.
File metadata
- Download URL: wlmodem-1.3.0-py2-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 571c8911e3443acc1b159a194cc65978c10fe7504cb86da2a7f1c20b61d1b340 |
|
MD5 | e919c7180137e1e924295e702904df74 |
|
BLAKE2b-256 | 765d1da2dda94e1407fc7d52737b40379a419f2b1f395c9d4191a4888da97def |