Metadata-Version: 2.4
Name: pyfastnet
Version: 2.0.17
Summary: A Python library for decoding FastNet protocol data streams.
Author-email: Alex Salmon <alex@ivila.net>
License: MIT License
        
        Copyright (c) 2025 ghotihook
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/ghotihook/pyfastnet
Project-URL: Repository, https://github.com/ghotihook/pyfastnet
Project-URL: Issues, https://github.com/ghotihook/pyfastnet/issues
Keywords: fastnet,bandg,hydra,h2000,marine,sailing,decoder
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Communications
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Dynamic: license-file

# pyfastnet

Python library for decoding the FastNet protocol used by B&G Hydra/H2000 instruments. Developed for personal use and published for general interest.

## Purpose

Feed a raw byte stream from a FastNet bus into the library. It handles synchronisation, checksum validation, and decoding — returning structured instrument data ready for further processing.

## Installation

```
pip install pyfastnet
```

## Example usage

```python
#!/usr/bin/env python3
import serial
from fastnet_decoder import FrameBuffer

fb = FrameBuffer()

ser = serial.Serial(
    port="/dev/ttyUSB0",
    baudrate=28800,
    bytesize=serial.EIGHTBITS,
    stopbits=serial.STOPBITS_TWO,
    parity=serial.PARITY_ODD,
    timeout=0.1,
)

try:
    while True:
        data = ser.read(256)
        if not data:
            continue
        fb.add_to_buffer(data)
        fb.get_complete_frames()
        while not fb.frame_queue.empty():
            frame = fb.frame_queue.get()
            for channel, decoded in frame["values"].items():
                print(channel, decoded)
finally:
    ser.close()
```

## Output format

Each decoded frame is a dict with `to_address`, `from_address`, `command`, and `values`. Each entry in `values` is keyed by channel name:

```python
{
  "to_address":   "Entire System",
  "from_address": "Normal CPU (Wind Board in H2000)",
  "command":      "Broadcast",
  "values": {
    "Apparent Wind Speed (Knots)": {
      "channel_id":   "0x4D",
      "value":        7.0,
      "display_text": "7.0",
      "layout":       None,
    },
    "Apparent Wind Angle": {
      "channel_id":   "0x51",
      "value":        -6.0,
      "display_text": "-6.0",
      "layout":       "-[data]",
    },
    "True Wind Direction": {
      "channel_id":   "0x6D",
      "value":        213.0,
      "display_text": "213.0°M",
      "layout":       "°M",
    },
  }
}
```

### Position (LatLon) frames

Position frames (command `LatLon`) appear under the `"LatLon"` key. The raw coordinate
string (`DDMM.mmm` with hemisphere letters) is carried in `display_text`; `value` is
`None`. The originating source's marker byte is preserved in `channel_id` (e.g. `0x47`,
`0x4E`) but does not affect the key:

```python
"LatLon": {
    "channel_id":   "0x4E",
    "value":        None,
    "display_text": "3352.450S15113.920E",
    "layout":       None,
}
```

### Layout field

The `layout` field describes the indicator symbol shown on the physical display around the numeric value:

| `layout` | Meaning | Sign |
|---|---|---|
| `None` | No indicator symbol | positive |
| `"[data]="` | `=` after value (starboard) | positive |
| `"=[data]"` | `=` before value (port) | negative |
| `"[data]-"` | `-` after value (starboard) | positive |
| `"-[data]"` | `-` before value (port) | negative |
| `"H[data]"` | `H` prefix — heading | positive |
| `"°M"` | Magnetic bearing suffix | positive |
| `"u[data]"` | `u` prefix — upwind (VMG) | positive |
| `"d[data]"` | `d` prefix — downwind (VMG) | positive |
| `"L[data]"` | `L` before — leeway port | negative |
| `"[data]L"` | `L` after — AP compass target | positive |
| `"[data]°C"` | Celsius suffix | positive |
| `"[data]°F"` | Fahrenheit suffix | positive |
| `"[data]z"` / `"z[data]"` | Dog-leg symbol — AP off course | positive |
| `"TBC"` | Symbol seen but not yet identified | positive |

## Debug API

```python
from fastnet_decoder import set_log_level
import logging
set_log_level(logging.DEBUG)
```

```python
fb.get_buffer_size()      # bytes currently in buffer
fb.get_buffer_contents()  # hex string of buffer contents
```

## Companion apps

- [fastnet2ip](https://github.com/ghotihook/fastnet2ip) — reads FastNet from serial and broadcasts it over UDP as either NMEA 0183 or NMEA 2000 (selected with `--output`). Install with `pipx install fastnet2ip`.

Runs on Raspberry Pi, macOS, or Linux.

## Acknowledgments

- [trlafleur](https://github.com/trlafleur) — background research
- [Oppedijk](https://www.oppedijk.com/bandg/fastnet.html) — protocol documentation
- [timmathews](https://github.com/timmathews/bg-fastnet-driver) — C++ reference implementation
