|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/tarm/serial" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + // args[0] is the exe path, which we don't need |
| 15 | + args := os.Args[1:] |
| 16 | + |
| 17 | + inputPort := "" |
| 18 | + inputBaudRate := 115200 |
| 19 | + |
| 20 | + // the first argument has to be the USB/Serial Device |
| 21 | + if len(args) > 0 { |
| 22 | + // find args[0] in serialPorts |
| 23 | + inputPort = args[0] |
| 24 | + } else { |
| 25 | + criticalError("Provide a port name") |
| 26 | + } |
| 27 | + |
| 28 | + // Optionally, the second argument can be the baud rate. |
| 29 | + // Default rate is 115200 |
| 30 | + if len(args) > 1 { |
| 31 | + i, err := strconv.Atoi(args[1]) |
| 32 | + |
| 33 | + if err != nil { |
| 34 | + fmt.Println(err) |
| 35 | + criticalError("Invalid baud rate") |
| 36 | + } |
| 37 | + |
| 38 | + if i < 0 { |
| 39 | + criticalError("Invalid baud rate") |
| 40 | + } |
| 41 | + |
| 42 | + inputBaudRate = i |
| 43 | + } |
| 44 | + |
| 45 | + begin(inputPort, inputBaudRate) |
| 46 | +} |
| 47 | + |
| 48 | +// Opens a new connection on the specified port at the specified baud rate |
| 49 | +func begin(portName string, baudRate int) { |
| 50 | + config := &serial.Config{ |
| 51 | + Baud: baudRate, |
| 52 | + Name: portName, |
| 53 | + } |
| 54 | + |
| 55 | + port, err := serial.OpenPort(config) |
| 56 | + if err != nil { |
| 57 | + criticalError("Error opening serial port: " + err.Error()) |
| 58 | + } |
| 59 | + |
| 60 | + // close the port when this method exits |
| 61 | + defer func(port *serial.Port) { |
| 62 | + err := port.Close() |
| 63 | + if err != nil { |
| 64 | + criticalError("Error closing serial port: " + err.Error() + ". The connection might stay open.") |
| 65 | + } |
| 66 | + }(port) |
| 67 | + |
| 68 | + // asynchronously read input |
| 69 | + go userInput(port) |
| 70 | + |
| 71 | + // while also receiving data from the serial device |
| 72 | + buf := make([]byte, 1024) |
| 73 | + fmt.Println("Output Ready!") |
| 74 | + |
| 75 | + for { |
| 76 | + n, err := port.Read(buf) |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + criticalError("Error reading from serial port: " + err.Error()) |
| 80 | + } |
| 81 | + |
| 82 | + fmt.Printf("%s", string(buf[:n])) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// reads the standard input until the next EOF, |
| 87 | +// or until the input line equals "exit" |
| 88 | +func userInput(port *serial.Port) { |
| 89 | + reader := bufio.NewReader(os.Stdin) |
| 90 | + fmt.Println("Input Ready!") |
| 91 | + for { |
| 92 | + input, err := reader.ReadString('\n') |
| 93 | + |
| 94 | + if err != nil { |
| 95 | + if err.Error() == "EOF" { |
| 96 | + os.Exit(0) |
| 97 | + } |
| 98 | + |
| 99 | + criticalError("Error reading input: " + err.Error()) |
| 100 | + } |
| 101 | + |
| 102 | + if len(input) == 0 { |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + input = strings.TrimSpace(input) |
| 107 | + |
| 108 | + // write the input data to the port, |
| 109 | + // after appending a newline character to it |
| 110 | + _, err = port.Write([]byte(input + "\n")) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + criticalError("Error writing to serial port: " + err.Error()) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// helper method to print a critical error and exit with code 1 |
| 119 | +func criticalError(message string) { |
| 120 | + fmt.Println(message) |
| 121 | + os.Exit(1) |
| 122 | +} |
0 commit comments