package main import ( "bufio" "fmt" "github.com/augustoroman/hexdump" "github.com/creack/pty" "os" "strconv" "time" ) func trxControl() { ptypath := "/tmp/my_virtual_serial" master, slave, err := pty.Open() if err != nil { writeDebug(fmt.Sprintf("trxControl: error opening PTY: %v", err), 0) } defer func() { err := master.Close() if err != nil { writeDebug(fmt.Sprintf("trxControl: error closing PTY: %v", err), 0) } }() defer func() { err := slave.Close() if err != nil { writeDebug(fmt.Sprintf("trxControl: error closing PTY: %v", err), 0) } }() err = os.Remove(ptypath) if err := os.Symlink(slave.Name(), ptypath); err != nil { writeDebug(fmt.Sprintf("trxControl: error creating link: %v", err), 0) } writeDebug(fmt.Sprintf("trxControl: opening PTY: %s\n", ptypath), 0) // Receive from TRX go func() { for { cmd, err := s.FromTRX.Dequeue(1024) if err == nil { writeDebug("From TRX response ("+strconv.Itoa(len(cmd))+"):\n"+hexdump.Dump(cmd), 1) _, err = master.Write(cmd) if err != nil { writeDebug(fmt.Sprintf("trxControl: error writing to PTY: %v", err), 0) } } time.Sleep(30 * time.Millisecond) } }() //scanner := bufio.NewScanner(master) //for scanner.Scan() { //t := scanner.Text() rd := bufio.NewReader(master) for { time.Sleep(100 * time.Millisecond) // looks like gpsd does not expect \n terminated lines so read what is there from the socket err := master.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) if err != nil { writeDebug(fmt.Sprintf("trxControl: error setting read deadline: %v", err), 0) } buff := make([]byte, 1024) n, err := rd.Read(buff) t := buff[:n] writeDebug("To TRX data ("+strconv.Itoa(len(t))+"):\n"+hexdump.Dump(t), 1) // Send to TRX err = s.ToTRX.Enqueue(t) if err != nil { writeDebug(fmt.Sprintf("trxControl: error enqueuing command: %v", err), 0) } } }