87 lines
2.1 KiB
Go
87 lines
2.1 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 einmal geöffnet und anschließend
|
|
* über dasselbe Handle für Lese- und Schreiboperationen verwendet.
|
|
* ============================================================================
|
|
*/
|
|
|
|
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.
|
|
//
|
|
// The COM port is opened exactly once. The resulting handle is used for
|
|
// parallel read and write operations.
|
|
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,
|
|
}
|
|
|
|
port, 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: port,
|
|
path: portPath,
|
|
},
|
|
)
|
|
|
|
return &ManagedVirtualPort{
|
|
port: virtualPort,
|
|
manager: manager,
|
|
number: number,
|
|
}, nil
|
|
}
|