nanovna

package module
v0.0.0-...-aeb9124 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 7 Imported by: 0

README

go-nanovna

Go Go Reference Go Report Card Codecov


Documentation: See docs/specification.md for full API and protocol details, including diagrams and flowcharts.


A comprehensive Go package for controlling and communicating with NanoVNA (Vector Network Analyzer) devices.

Features

  • Universal Hardware Support: Supports all major NanoVNA hardware variants with automatic detection
  • Hardware Abstraction: Unified interface that adapts to different hardware capabilities
  • Comprehensive Coverage: Supports V1, VH, V2, V2+, V2+4, SAA2, TinySA, and LiteVNA variants
  • Smart Detection: Automatic hardware variant detection and capability mapping
  • Frequency Validation: Hardware-aware frequency range and sweep point validation
  • Debug Support: Built-in serial debugging and hardware information display

Supported Hardware Variants

Variant Frequency Range Max Points S-Parameters Special Features
NanoVNA v1 50 kHz - 900 MHz 101 S11, S21 Basic VNA
NanoVNA-H 50 kHz - 1.5 GHz 201 S11, S21 Time domain, Generator
NanoVNA v2 50 kHz - 3 GHz 4000 S11, S21 High resolution, Spectrum
NanoVNA v2+ 50 kHz - 6 GHz 4000 S11, S21 Extended frequency
NanoVNA v2+4 50 kHz - 6 GHz 4000 S11, S21, S12, S22 4-port measurements
TinySA 100 kHz - 960 MHz 500 S11 Spectrum analyzer focus
LiteVNA 100 kHz - 6.5 GHz 4000 S11, S21 Extended range

Installation

go get github.com/VA7DBI/go-nanovna

Quick Start

package main

import (
    "fmt"
    "log"
    "github.com/VA7DBI/go-nanovna"
)

func main() {
    // Auto-detect and connect to NanoVNA
    device, err := nanovna.AutoDetect()
    if err != nil {
        log.Fatal("Failed to detect NanoVNA:", err)
    }
    defer device.Close()

    // Get hardware information
    info := device.GetHardwareInfo()
    fmt.Printf("Connected to: %s\n", info.Variant.String())
    fmt.Printf("Frequency Range: %.0f Hz - %.0f Hz\n", 
        info.FrequencyRange.MinHz, info.FrequencyRange.MaxHz)
    
    // Configure sweep
    err = device.SetSweepConfig(144000000, 146000000, 101)
    if err != nil {
        log.Fatal("Failed to configure sweep:", err)
    }
    
    // Run measurement
    data, err := device.RunSweep()
    if err != nil {
        log.Fatal("Failed to run sweep:", err)
    }
    
    fmt.Printf("Measured %d points\n", len(data.Frequencies))
}

Manual Connection

// Connect to specific port
device, err := nanovna.Open("COM3")
if err != nil {
    log.Fatal(err)
}

// Detect hardware variant
version, err := device.DetectVersion()
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Detected: %s\n", version)

Hardware-Aware Programming

// Check hardware capabilities
caps := device.GetCapabilities()
if caps.HasS21 {
    fmt.Println("S21 measurements supported")
}
if caps.HasTimeDomain {
    fmt.Println("Time domain analysis supported")
}

// Validate frequency range
freqRange := device.GetFrequencyRange()
if startFreq < freqRange.MinHz {
    log.Fatal("Start frequency too low for this hardware")
}

Testing & Coverage

Run all tests:

go test ./...

Run with coverage (CLI):

go test -cover ./...

Generate a local HTML coverage report:

go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

This will open a browser window with a detailed coverage report.

Codecov badge

The badge above is powered by Codecov. To enable it for your fork or private repo:

  1. Sign up at codecov.io and enable your repository.
  2. (If private) Add your Codecov token as a GitHub secret named CODECOV_TOKEN.
  3. Push to main or open a pull request to trigger the workflow.

API Reference

Device Management
  • AutoDetect() (*Device, error) - Auto-detect and connect to NanoVNA
  • Open(port string) (*Device, error) - Connect to specific serial port
  • OpenWithVariant(port, variant) - Force specific hardware variant
  • ListDevices() ([]string, error) - List available serial ports
Hardware Information
  • GetHardwareVariant() HardwareVariant - Get detected hardware type
  • GetHardwareInfo() HardwareInfo - Get complete hardware information
  • GetFrequencyRange() FrequencyRange - Get supported frequency range
  • GetCapabilities() HardwareCapabilities - Get hardware capabilities
Measurements
  • SetSweepConfig(start, stop, points int) error - Configure sweep parameters
  • RunSweep() (SweepData, error) - Perform measurement sweep
  • GetInfo() (DeviceInfo, error) - Get device information
Data Structures
type SweepData struct {
    Frequencies []float64      // Frequency points
    S11         []complex128   // S11 measurements
    S21         []complex128   // S21 measurements (if supported)
}

type HardwareCapabilities struct {
    HasS21           bool  // S21 transmission measurements
    HasTimeDomain    bool  // Time domain analysis
    HasCalibration   bool  // Calibration support
    HasMultiplePorts bool  // 4-port measurements
    HasGenerator     bool  // Signal generator
    HasSpectrumMode  bool  // Spectrum analyzer mode
}

License

MIT License - see LICENSE file for details.

Contributing

Contributions welcome! Please submit issues and pull requests on GitHub.

Documentation

Overview

Package nanovna provides a Go API for communicating with and controlling NanoVNA devices over a serial (USB) connection. It enables device discovery, configuration, data acquisition, calibration management, and device information retrieval.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListDevices

func ListDevices() ([]string, error)

ListDevices lists available NanoVNA serial ports (Windows only, stub).

Types

type CalibrationData

type CalibrationData struct {
}

CalibrationData holds calibration coefficients and metadata.

type CommandSet

type CommandSet struct {
	SweepCommand    string
	FreqCommand     string
	DataCommand     string
	InfoCommand     string
	VersionCommand  string
	CalibrationSave string
	CalibrationLoad string
	PromptPattern   string
}

CommandSet defines the command set for different hardware variants.

type Device

type Device struct {
	Port string
	// contains filtered or unexported fields
}

Device represents a connection to a NanoVNA device.

func AutoDetect

func AutoDetect() (*Device, error)

AutoDetect attempts to find and connect to a NanoVNA device automatically.

func Open

func Open(port string, custom ...SerialPort) (*Device, error)

Open connects to a NanoVNA on the specified serial port. Optionally accepts a custom SerialPort for debug/testing.

func OpenWithVariant

func OpenWithVariant(port string, variant HardwareVariant) (*Device, error)

OpenWithVariant opens a device and forces a specific hardware variant (useful for testing or when auto-detection fails)

func (*Device) Close

func (d *Device) Close() error

Close disconnects from the device.

func (*Device) DetectVersion

func (d *Device) DetectVersion() (string, error)

DetectVersion detects the NanoVNA version by sending CR and analyzing the response

func (*Device) GetCalibration

func (d *Device) GetCalibration() (CalibrationData, error)

GetCalibration retrieves current calibration data.

func (*Device) GetCapabilities

func (d *Device) GetCapabilities() HardwareCapabilities

GetCapabilities returns the hardware capabilities.

func (*Device) GetFrequencyRange

func (d *Device) GetFrequencyRange() FrequencyRange

GetFrequencyRange returns the supported frequency range for this hardware.

func (*Device) GetHardwareInfo

func (d *Device) GetHardwareInfo() HardwareInfo

GetHardwareInfo returns the hardware information and capabilities.

func (*Device) GetHardwareVariant

func (d *Device) GetHardwareVariant() HardwareVariant

GetHardwareVariant returns the detected hardware variant.

func (*Device) GetInfo

func (d *Device) GetInfo() (DeviceInfo, error)

GetInfo retrieves device information (model, firmware, serial number).

func (*Device) GetMaxSweepPoints

func (d *Device) GetMaxSweepPoints() int

GetMaxSweepPoints returns the maximum number of sweep points supported.

func (*Device) GetPortConfig

func (d *Device) GetPortConfig() *PortConfig

GetPortConfig returns the port configuration details (for debugging).

func (*Device) GetPortDetails

func (d *Device) GetPortDetails() string

GetPortDetails returns detailed port information as a formatted string.

func (*Device) GetPortHandle

func (d *Device) GetPortHandle() SerialPort

GetPortHandle returns the underlying serial port (for debug wrapping).

func (*Device) GetSupportedPorts

func (d *Device) GetSupportedPorts() []string

GetSupportedPorts returns the supported S-parameter ports.

func (*Device) GetVersion

func (d *Device) GetVersion() string

GetVersion returns the detected NanoVNA version.

func (*Device) IsPortSupported

func (d *Device) IsPortSupported(port string) bool

IsPortSupported checks if a specific S-parameter port is supported.

func (*Device) LoadCalibration

func (d *Device) LoadCalibration(slot int) error

LoadCalibration loads calibration data from device memory.

func (*Device) RunSweep

func (d *Device) RunSweep() (SweepData, error)

RunSweep triggers a sweep and returns measurement data. Uses hardware-specific commands and handles different port configurations.

func (*Device) SaveCalibration

func (d *Device) SaveCalibration(slot int) error

SaveCalibration saves calibration data to device memory.

func (*Device) SetCalibration

func (d *Device) SetCalibration(cal CalibrationData) error

SetCalibration applies calibration data.

func (*Device) SetPortHandle

func (d *Device) SetPortHandle(sp SerialPort)

SetPortHandle allows replacing the underlying serial port (for debug wrapping)

func (*Device) SetSweepConfig

func (d *Device) SetSweepConfig(startHz, stopHz int, points int) error

SetSweepConfig configures sweep parameters (start, stop, points).

type DeviceInfo

type DeviceInfo struct {
	Model     string
	Firmware  string
	SerialNum string
}

DeviceInfo contains information about the NanoVNA device.

type FrequencyRange

type FrequencyRange struct {
	MinHz float64
	MaxHz float64
}

FrequencyRange defines the frequency range for a hardware variant.

type HardwareCapabilities

type HardwareCapabilities struct {
	HasS21           bool
	HasTimeDomain    bool
	HasCalibration   bool
	HasMultiplePorts bool
	HasGenerator     bool
	HasSpectrumMode  bool
}

HardwareCapabilities defines what each hardware variant can do.

type HardwareInfo

type HardwareInfo struct {
	Variant        HardwareVariant
	FrequencyRange FrequencyRange
	MaxSweepPoints int
	SupportedPorts []string // S11, S21, S12, S22
	CommandSet     CommandSet
	Capabilities   HardwareCapabilities
}

HardwareInfo contains hardware-specific information and capabilities.

type HardwareVariant

type HardwareVariant int

HardwareVariant represents different NanoVNA hardware versions.

const (
	VariantUnknown HardwareVariant = iota
	VariantV1                      // Original NanoVNA v1
	VariantVH                      // NanoVNA-H (Hardware version)
	VariantV2                      // NanoVNA v2 (SAA2)
	VariantV2Plus                  // NanoVNA v2 Plus
	VariantV2Plus4                 // NanoVNA v2 Plus4
	VariantSAA2                    // Standalone SAA2
	VariantTinysa                  // TinySA variant
	VariantLiteVNA                 // LiteVNA variant
)

func (HardwareVariant) String

func (hv HardwareVariant) String() string

String returns the string representation of the hardware variant.

type PortConfig

type PortConfig struct {
	Name        string
	Baud        int
	ReadTimeout time.Duration
	Size        byte
	Parity      serial.Parity
	StopBits    serial.StopBits
}

PortConfig holds serial port configuration details for debugging.

type SerialPort

type SerialPort interface {
	Write([]byte) (int, error)
	Read([]byte) (int, error)
	Close() error
}

SerialPort is the interface for serial port operations (exported for debug wrapping).

type SweepData

type SweepData struct {
	Frequencies []float64
	S11         []complex128
	S21         []complex128
}

SweepData holds measurement data from a sweep.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL