From d4fba9d4826c9eb7a85ea3f6924c34fc4b13396a Mon Sep 17 00:00:00 2001 From: ea4tx Date: Sat, 6 Jan 2024 00:52:55 +0100 Subject: [PATCH] Yaesu: Allow to connect to a rotator via TCP --- rotator/yaesu/yaesu.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/rotator/yaesu/yaesu.go b/rotator/yaesu/yaesu.go index 972856e..0d1cdc2 100644 --- a/rotator/yaesu/yaesu.go +++ b/rotator/yaesu/yaesu.go @@ -4,8 +4,10 @@ import ( "bufio" "fmt" "io" + "net" "regexp" "strconv" + "strings" "sync" "time" @@ -53,7 +55,7 @@ type Yaesu struct { // functional options. // Default settings are: // hasAzimuth: true, -// portname: /dev/ttyACM0, +// portname: /dev/ttyACM0 (or 127.0.0.1:6001), // pollingInterval: 5sec, // baudrate: 9600. func New(opts ...func(*Yaesu)) (*Yaesu, error) { @@ -78,23 +80,29 @@ func New(opts ...func(*Yaesu)) (*Yaesu, error) { for _, opt := range opts { opt(r) } - - config := &serial.Config{ - Name: r.spPortName, - Baud: r.spBaudrate, - ReadTimeout: time.Millisecond * 100, - Parity: serial.ParityNone, - Size: 8, - StopBits: 1, - } - - sp, err := serial.OpenPort(config) - if err != nil { - return nil, err + + if strings.Contains(r.spPortName, ":") { + tcpConn, err := net.Dial("tcp", r.spPortName) + if err != nil { + return nil, err + } + r.sp = tcpConn + } else { + spConfig := &serial.Config{ + Name: r.spPortName, + Baud: r.spBaudrate, + ReadTimeout: time.Second, + Parity: serial.ParityNone, + Size: 8, + StopBits: 1, + } + sp, err := serial.OpenPort(spConfig) + if err != nil { + return nil, err + } + r.sp = sp } - r.sp = sp - go r.start() return r, nil