# cnxy/visainstrument architecture

> Architecture diagram of the GitHub repository cnxy/visainstrument, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-03-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cnxy/visainstrument
- Repository: https://github.com/cnxy/visainstrument

## Overview

Summary of project type and purpose
- This is a Windows desktop instrument-control application written in C# (.NET, Visual Studio solution). It is a single-process Windows Forms app (FrmMain) that provides a UI for communicating with measurement instruments via NI-VISA. It supports four physical connection types: RS232 (serial), USB, GPIB, and LAN/TCPIP.
- The app depends on the NI‑VISA runtime and (for GPIB) the GPIB card driver. It wraps VISA-based communication in a Ports subsystem with operator classes for each transport.

Key evidence (from file tree + README)
- Visual Studio solution and .csproj: VISAInstrument.sln and VISAInstrument/VISAInstrument.csproj
- UI entrypoints: Program.cs, FrmMain.cs / FrmMain.Designer.cs
- Ports subsystem: Ports/*.cs (GpibPortOperator, LanPortOperator, UsbPortOperator, GpibPortOperator, PortOperatorBase, IPortOperator, PortEventArgs, SerialDataReceivedEventArgs)
- Configuration: Configuration/VisaInformation.cs
- Utility/helpers: Utility/PortHelper.cs, PortType.cs, extension helpers (ByteEx, StringEx, RegexEx), and UninstallInfo helpers
- README explicitly states NI‑VISA dependency, supported addresses/formats per transport, and shows a screenshot of the UI.

- UI Layer (FrmMain)
- Interacts with the Ports subsystem via an abstraction IPortOperator / VISAInstrument.Port
- Receives async events (PortEventArgs, SerialDataReceivedEventArgs)
- Ports subsystem
- Interface: IPortOperator (defines common API: Open, Close, Write, Read, Events)
- Base implementation: PortOperatorBase (common logic, error handling, threading model)
- Concrete operators: SerialPortOperator (RS232), UsbPortOperator, GpibPortOperator, LanPortOperator
- Port factory or PortHelper (PortHelper / PortType) to select and instantiate correct operator based on configured address/type
- Configuration module
- VisaInformation (stores/loads instrument addresses and settings)
- Settings (Properties/Settings.settings) and resources used by UI
- Utility/Extensions
- ByteEx, StringEx, RegexEx (data parsing/formatting)
- UI extension helpers (ComboBoxEx, TextBoxEx, Pair types)
- Uninstall/installer related helper modules (UninstallInfo/UninstallInfoHelper) — auxiliary but include in components

- Relationships:
- FrmMain → uses → IPortOperator instances
- IPortOperator implementations → call → NI‑VISA via Pinvoke or .NET wrapper (represent as "VISA API adapter" inside ports)
- Ports → raise → PortEventArgs/SerialDataReceivedEventArgs → consumed by UI
- Configuration → provided to Ports subsystem on open

- User selects instrument in UI → UI loads VisaInformation and asks PortHelper to create appropriate IPortOperator
- IPortOperator.Open(): PortOperatorBase initializes and calls NI‑VISA viOpen / resource manager
- UI sends command → IPortOperator.Write() → wraps & calls viWrite
- Instrument responds → NI‑VISA signals data available → Port operator receives/reads via viRead and raises SerialDataReceivedEventArgs/PortEventArgs to UI
- UI updates display or logs response
- IPortOperator.Close() → viClose

##### Important architectural patterns and design principles present
- Abstraction via interface (IPortOperator) and base class (PortOperatorBase): allows swapping transport implementations.
- Single-process, modular components—separation of UI and transport logic.
- Event-driven communication from transport layer back to UI (PortEventArgs).
- Helper/Factory pattern implied (PortHelper/PortType) to translate addresses into operator instances.

- NI‑VISA runtime (explicit minimum versions listed in README)
- GPIB hardware and GPIB driver (NI-GPIB recommended)
- Windows OS and its device stacks (Serial/USB/TCPIP)
- .NET Framework / Visual Studio environment (development)
- Visual elements: WinForms (FrmMain) — show as UI framework

##### Level-of-detail guidance
- The IPortOperator interface methods and events (Open/Close/Write/Read, DataReceived event)
- PortOperatorBase responsibilities (error handling, threading, common parsing)
- Concrete operators and any transport-specific differences (e.g., Serial uses SerialDataReceivedEventArgs)
- Connection lifecycle (open/close)
- Command/response (write/read + async event)
- Error/retry flow (how exceptions propagate)

##### Additional notes specific to this project
- GPIB controller driver separately from NI‑VISA because both are required and interact with hardware differently.
- Settings/Resources under the application as persistent configuration used to restore recent instruments.
- That any changes to transport must implement IPortOperator and hook into PortOperatorBase to maintain consistency.

##### Recommended tools and export formats
- Export PNG/SVG for README and PDF for architecture docs.

- Top-level: Windows host, Application, NI‑VISA runtime, GPIB driver, Instruments and physical connections.
- Logical: UI, Ports subsystem (interface/base/concrete operators), Config, Utilities.
- Tech stack: .NET/WinForms, NI‑VISA, drivers, physical protocols (RS232/USB/GPIB/TCPIP).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top-level Deployment (vertical)
    subgraph "Windows Host (OS: Windows 7..11)"
        direction TB
        App["Application Process: VISAInstrument.exe\\n(.NET WinForms)"]:::app
        subgraph "Application Components"
            direction TB
            UI["UI: FrmMain (WinForms)"]:::ui
            Ports["Ports Subsystem"]:::core
            Config["Configuration:\\nVisaInformation / Settings"]:::core
            Utils["Utilities & Extensions"]:::core
            Resources["Resources / Assembly Info"]:::core
        end
    end

    %% External dependencies (stacked vertically to the right)
    subgraph "Host Middleware & Drivers"
        direction TB
        VISA["NI-VISA Runtime\\n(VISA API: viOpen/viWrite/viRead/viClose)"]:::middleware
        GPIBDrv["GPIB Driver (NI-GPIB)"]:::middleware
        OSStack["Windows Device Stacks\\n(Serial/USB/TCPIP)"]:::middleware
    end

    %% Instruments and physical transports (left, vertical)
    subgraph "Instruments / Physical Layer"
        direction TB
        Instruments["Lab Instruments (1..N)"]:::device
        GPIBCard["GPIB Controller Card (in host)"]:::device
        USBDevice["USB Instrument"]:::device
        SerialDevice["RS232 Instrument"]:::device
        LANDevice["LAN / TCPIP Instrument"]:::device
    end

    %% Connections (arrows)
    App -->|"uses (UI interactions)"| UI
    App -->|"hosts"| Ports
    App -->|"loads/stores"| Config
    App -->|"util functions"| Utils
    Ports -->|"calls VISA API"| VISA
    VISA -->|"interfaces with"| OSStack
    OSStack -->|"talks to (driver/hardware)"| GPIBDrv
    GPIBDrv -->|"controls"| GPIBCard
    GPIBCard -->|"GPIB bus"| Instruments

    %% Physical transports to instruments
    OSStack -->|"USB cable"| USBDevice
    OSStack -->|"Serial (COM/ASRL)"| SerialDevice
    OSStack -->|"TCP/IP (LAN)"| LANDevice
    OSStack -->|"GPIB via controller"| GPIBCard

    %% Logical Component: Ports subsystem expanded (vertical)
    subgraph "Ports Subsystem (inside App)"
        direction TB
        IPort["IPortOperator (interface)\\n(Open/Close/Write/Read, DataReceived event)"]:::core
        Base["PortOperatorBase (common logic, threading, error handling)"]:::core
        Factory["PortHelper / PortType (factory)"]:::core
        subgraph "Concrete Operators"
            direction TB
            SerialOp["SerialPortOperator (RS232)\\nuses SerialDataReceivedEventArgs"]:::core
            UsbOp["UsbPortOperator (USB)"]:::core
            GpibOp["GpibPortOperator (GPIB)"]:::core
            LanOp["LanPortOperator (TCP/IP)"]:::core
        end
        Events["PortEventArgs / SerialDataReceivedEventArgs"]:::core
    end

    %% Links inside Ports subsystem
    UI -->|"creates/uses via IPortOperator"| IPort
    Factory -->|"instantiates concrete"| SerialOp
    Factory -->|"instantiates concrete"| UsbOp
    Factory -->|"instantiates concrete"| GpibOp
    Factory -->|"instantiates concrete"| LanOp
    IPort -->|"base behavior implemented by"| Base
    SerialOp -->|"inherits/overrides"| Base
    UsbOp -->|"inherits/overrides"| Base
    GpibOp -->|"inherits/overrides"| Base
    LanOp -->|"inherits/overrides"| Base

    %% Event flows (dashed)
    SerialOp -->|"- - - DataReceived (async) - - -\\nPortEventArgs/SerialDataReceivedEventArgs"| Events
    Events -->|"- - - raised to UI (async callback) - - -"| UI
    UI -->|"synchronous calls (Write/Open/Close)"| IPort

    %% Configuration usage
    Config -->|"provides addresses/settings to"| Factory
    Config -->|"persisted in Settings/resources"| Resources

    %% Notes and labels (nodes then links to keep valid syntax)
    middleware_note["Note:\\nNI-VISA runtime required (min versions in README)"]:::note
    gpib_note["Note:\\nGPIB driver required for GPIB hardware"]:::note
    VISA --- middleware_note
    GPIBDrv --- gpib_note

    %% Click Events for mapped components (paths only)
    click UI "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Program.cs"
    click UI "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/FrmMain.cs"
    click UI "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/FrmMain.Designer.cs"
    click UI "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/FrmMain.resx"

    click App "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument.sln"
    click App "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/VISAInstrument.csproj"

    click IPort "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/IPortOperator.cs"
    click Base "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/PortOperatorBase.cs"
    click Events "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/PortEventArgs.cs"
    click Events "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/SerialDataReceivedEventArgs.cs"
    click GpibOp "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/GpibPortOperator.cs"
    click UsbOp "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/UsbPortOperator.cs"
    click LanOp "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/LanPortOperator.cs"
    click Ports "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/VISAInstrument.Port.cs"
    click Factory "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Ports/IPortType.cs"

    click Config "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Configuration/VisaInformation.cs"
    click Resources "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Properties/Settings.settings"

    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/PortHelper.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/PortType.cs"

    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/Extension/ByteEx.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/Extension/StringEx.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/Extension/RegexEx.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/Extension/UI/ComboBoxEx.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/Extension/UI/TextBoxEx.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/Extension/UI/Pair.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/Extension/UI/Pair2.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/Extension/UI/PairCollection.cs"

    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/UninstallInfo.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/UninstallInfoHelper.cs"
    click Utils "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Utility/UninstallInfoOption.cs"

    click Resources "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Properties/Resources.resx"
    click Resources "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Properties/Resources.Designer.cs"
    click Resources "https://github.com/cnxy/visainstrument/blob/master/VISAInstrument/Properties/AssemblyInfo.cs"

    click VISA "https://github.com/cnxy/visainstrument/blob/master/README.md"

    %% Styling / Classes
    classDef ui fill:#cfe7ff,stroke:#2b6cb0,color:#042a4a;
    classDef app fill:#dbeffd,stroke:#2b6cb0,color:#042a4a;
    classDef core fill:#e6f8e9,stroke:#2b9a3e,color:#07380f;
    classDef middleware fill:#ffe8cc,stroke:#d97706,color:#4a2b00;
    classDef device fill:#e6e6e6,stroke:#6b6b6b,color:#111111;
    classDef note fill:#fff7cc,stroke:#c08f00,color:#5b3d00;

    class UI,App ui
    class Ports,Config,Utils,Resources,IPort,Base,Factory,SerialOp,UsbOp,GpibOp,LanOp,Events core
    class VISA,GPIBDrv,OSStack middleware
    class Instruments,GPIBCard,USBDevice,SerialDevice,LANDevice device
    class middleware_note,gpib_note note

    %% Legend (vertical)
    subgraph "Legend"
        direction TB
        L1["Blue = UI Layer"]:::ui
        L2["Green = Core / App Libraries"]:::core
        L3["Orange = Middleware / Drivers"]:::middleware
        L4["Gray = Physical Devices / Network"]:::device
        L5["Dashed arrows = async events"]:::note
    end
```
