rs2322tcp/internal/client/virtual_serial_windows.go

114 lines
2.5 KiB
Go

//go:build windows
/*
* ============================================================================
* Projekt.....: rs2322tcp
* Datei.......: internal/client/virtual_serial_windows.go
* Copyright (C) 2026 Dieter Lang
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Beschreibung:
* Windows-spezifische Implementierung der virtuellen seriellen Schnittstelle
* über go.bug.st/serial.
*
* Der konfigurierte COM-Port wird direkt geöffnet. Die Zuordnung eines
* virtuellen COM-Port-Paares erfolgt außerhalb des rs2322tcp-Clients.
* ============================================================================
*/
package client
import (
"fmt"
"sync"
bugserial "go.bug.st/serial"
)
///////////////////////////////////////////////////////////////////////////////
// windowsSerial
///////////////////////////////////////////////////////////////////////////////
// windowsSerial implements VirtualSerial using a Windows COM port.
type windowsSerial struct {
mu sync.Mutex
port bugserial.Port
path string
}
///////////////////////////////////////////////////////////////////////////////
// Path
///////////////////////////////////////////////////////////////////////////////
// Path returns the Windows COM-port name.
func (p *windowsSerial) Path() string {
if p == nil {
return ""
}
return p.path
}
///////////////////////////////////////////////////////////////////////////////
// Read / Write
///////////////////////////////////////////////////////////////////////////////
// Read reads raw bytes from the COM port.
func (p *windowsSerial) Read(b []byte) (int, error) {
if p == nil {
return 0, fmt.Errorf("virtual serial port is nil")
}
p.mu.Lock()
port := p.port
p.mu.Unlock()
if port == nil {
return 0, fmt.Errorf("virtual serial port is closed")
}
return port.Read(b)
}
// Write writes raw bytes to the COM port.
func (p *windowsSerial) Write(b []byte) (int, error) {
if p == nil {
return 0, fmt.Errorf("virtual serial port is nil")
}
p.mu.Lock()
port := p.port
p.mu.Unlock()
if port == nil {
return 0, fmt.Errorf("virtual serial port is closed")
}
return port.Write(b)
}
///////////////////////////////////////////////////////////////////////////////
// Close
///////////////////////////////////////////////////////////////////////////////
// Close closes the Windows COM port.
func (p *windowsSerial) Close() error {
if p == nil {
return nil
}
p.mu.Lock()
if p.port == nil {
p.mu.Unlock()
return nil
}
port := p.port
p.port = nil
p.mu.Unlock()
return port.Close()
}