rs2322tcp/internal/client/virtual_port.go
2026-08-10 16:20:28 +02:00

70 lines
1.6 KiB
Go

/*
Package client contains the client-side components of rs2322tcp.
Project: rs2322tcp
Module: git.lang-dieter.de/rs2322tcp
*/
package client
import "io"
///////////////////////////////////////////////////////////////////////////////
// Virtual port
///////////////////////////////////////////////////////////////////////////////
// VirtualPort represents one local virtual serial port.
//
// The visible path is the device name presented to the external application,
// for example /dev/ttyUSB100.
//
// The underlying VirtualSerial remains an internal implementation detail and
// may use a PTY such as /dev/pts/2.
type VirtualPort struct {
path string
serial VirtualSerial
}
// NewVirtualPort creates a virtual port with the specified visible path and
// underlying virtual serial device.
func NewVirtualPort(path string, serial VirtualSerial) *VirtualPort {
return &VirtualPort{
path: path,
serial: serial,
}
}
// Path returns the device path visible to the external application.
func (p *VirtualPort) Path() string {
if p == nil {
return ""
}
return p.path
}
// Read reads data from the underlying virtual serial device.
func (p *VirtualPort) Read(b []byte) (int, error) {
if p == nil || p.serial == nil {
return 0, io.ErrClosedPipe
}
return p.serial.Read(b)
}
// Write writes data to the underlying virtual serial device.
func (p *VirtualPort) Write(b []byte) (int, error) {
if p == nil || p.serial == nil {
return 0, io.ErrClosedPipe
}
return p.serial.Write(b)
}
// Close closes the underlying virtual serial device.
func (p *VirtualPort) Close() error {
if p == nil || p.serial == nil {
return nil
}
return p.serial.Close()
}