Skip to content

Commit 4d25969

Browse files
committed
Initial Commit
0 parents  commit 4d25969

7 files changed

Lines changed: 214 additions & 0 deletions

File tree

.github/workflows/go-release.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Build Go Project & Attach exe to release
2+
3+
on:
4+
push:
5+
tags: [ 'v*.*.*' ]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
publish:
12+
name: Publish for ${{ matrix.os }}
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
matrix:
16+
include:
17+
- os: ubuntu-24.04
18+
asset_name: ssc-linux-amd64
19+
- os: ubuntu-24.04-arm
20+
asset_name: ssc-linux-arm64
21+
- os: windows-2025
22+
asset_name: ssc-windows-amd64.exe
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
- name: Set up Go
27+
uses: actions/setup-go@v4
28+
with:
29+
go-version: '1.24'
30+
check-latest: true
31+
cache-dependency-path: "**/*.sum"
32+
33+
- name: Build
34+
run: go build -v -o ssc
35+
- name: Upload binaries to release
36+
uses: svenstaro/upload-release-action@v2
37+
with:
38+
repo_token: ${{ github.token }}
39+
file: ssc
40+
asset_name: ${{ matrix.asset_name }}
41+
tag: ${{ github.ref }}

.github/workflows/go.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v4
17+
- name: Set up Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: '1.24'
21+
check-latest: true
22+
cache-dependency-path: "**/*.sum"
23+
24+
- name: Build
25+
run: go build -v .

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
simple-serial-console
2+
simple-serial-console.exe

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Simple Serial Console
2+
_replace `ssc` with the name of your binary, e.g. `ssc-windows-amd64`_
3+
4+
## Usage
5+
```
6+
ssc <port> [<baud rate>]
7+
```
8+
### `<port>`
9+
#### Windows
10+
A COM Port `COMX` where X is a number
11+
12+
#### Linux
13+
A USB TTY, usually something like `/dev/ttyUSBX` where X is a number

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module jonathanbout/simple-serial-console
2+
3+
go 1.18
4+
5+
require github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
6+
7+
require golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
2+
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
3+
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
4+
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

main.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)