rs2322tcp/internal/client/virtual_port_open_windows.go

84 lines
2 KiB
Go

//go:build windows
/*
* ============================================================================
* Projekt.....: rs2322tcp
* Datei.......: internal/client/virtual_port_open_windows.go
* Copyright (C) 2026 Dieter Lang
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Beschreibung:
* Windows-spezifische Erzeugung eines verwalteten virtuellen rs2322tcp-Ports.
*
* Der konfigurierte virtuelle COM-Port wird über die bereits vorhandene
* serielle Infrastruktur geöffnet.
* ============================================================================
*/
package client
import (
"fmt"
bugserial "go.bug.st/serial"
)
///////////////////////////////////////////////////////////////////////////////
// Open virtual port
///////////////////////////////////////////////////////////////////////////////
// openVirtualPort creates and initializes a Windows virtual serial port.
//
// The port number must already have been reserved by the
// VirtualPortManager.
//
// The visible COM port is provided by the platform-specific PortPath()
// implementation. The actual COM-port pair configuration is managed
// externally by the Windows virtual COM-port driver.
func openVirtualPort(
manager *VirtualPortManager,
number int,
) (*ManagedVirtualPort, error) {
if manager == nil {
return nil, fmt.Errorf("virtual port manager is nil")
}
portPath := manager.PortPath(number)
if portPath == "" {
return nil, fmt.Errorf(
"virtual port path is empty for port %d",
number,
)
}
mode := &bugserial.Mode{
BaudRate: 9600,
DataBits: 8,
Parity: bugserial.NoParity,
StopBits: bugserial.OneStopBit,
}
serial, err := bugserial.Open(portPath, mode)
if err != nil {
return nil, fmt.Errorf(
"open virtual serial port %s: %w",
portPath,
err,
)
}
virtualPort := NewVirtualPort(
portPath,
&windowsSerial{
port: serial,
path: portPath,
},
)
return &ManagedVirtualPort{
port: virtualPort,
manager: manager,
number: number,
}, nil
}