Compare commits
No commits in common. "main" and "v0.9.3" have entirely different histories.
|
@ -2,4 +2,3 @@
|
|||
.idea
|
||||
dist/
|
||||
/go.sum
|
||||
/view/*
|
||||
|
|
|
@ -23,15 +23,6 @@ builds:
|
|||
- linux
|
||||
- windows
|
||||
- darwin
|
||||
ldflags:
|
||||
- -s -w
|
||||
|
||||
upx:
|
||||
- enabled: true
|
||||
goos:
|
||||
- windows
|
||||
goarch:
|
||||
- amd64
|
||||
|
||||
archives:
|
||||
- format: tar.gz
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
16
README.md
16
README.md
|
@ -11,11 +11,11 @@
|
|||
* [x] 活动端口探测
|
||||
* [x] 数据日志保存
|
||||
* [x] Hex断帧设置
|
||||
* [x] UDP数据转发(支持多服)
|
||||
* [x] TCP数据转发(支持多服)
|
||||
* [x] UDP数据转发
|
||||
* [x] TCP数据转发
|
||||
* [x] 参数交互配置
|
||||
* [x] Ctrl组合键
|
||||
* [x] 文件接收发送(trzsz lrzsz都支持)
|
||||
* [ ] 文件接收发送
|
||||
|
||||
## 运行示例
|
||||
|
||||
|
@ -42,13 +42,3 @@
|
|||
7. Ctrl组合键发送指令.ctrl `.ctrl c`
|
||||
|
||||

|
||||
8. 文件上传演示 `index.html`
|
||||

|
||||
内容对比
|
||||

|
||||
9. 时间戳 `./COM -p COM8 -t`
|
||||

|
||||
10. 格式修改 `./COM -p COM11 -t='<2006-01-02 15:04:05>'`
|
||||

|
||||
11. 多服同步转发 `./COM -p COM11 -f 1 -a 127.0.0.1:23456 -f 1 -a 127.0.0.1:23457`
|
||||

|
12
command.go
12
command.go
|
@ -14,10 +14,7 @@ type Command struct {
|
|||
function func()
|
||||
}
|
||||
|
||||
var (
|
||||
commands []Command
|
||||
args []string
|
||||
)
|
||||
var commands []Command
|
||||
|
||||
func cmdhelp() {
|
||||
var page = 0
|
||||
|
@ -27,8 +24,6 @@ func cmdhelp() {
|
|||
}
|
||||
}
|
||||
func cmdexit() {
|
||||
CloseTrzsz()
|
||||
CloseSerial()
|
||||
os.Exit(0)
|
||||
}
|
||||
func cmdargs() {
|
||||
|
@ -36,11 +31,12 @@ func cmdargs() {
|
|||
strout(out, config.outputCode, fmt.Sprintf("%q\n", args[1:]))
|
||||
}
|
||||
func cmdctrl() {
|
||||
var err error
|
||||
b := []byte(args[1])
|
||||
x := []byte{b[0] & 0x1f}
|
||||
_, err = serialPort.Write(x)
|
||||
ErrorF(err)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
strout(out, config.outputCode, fmt.Sprintf("Ctrl+%s\n", b))
|
||||
}
|
||||
func cmdhex() {
|
||||
|
|
29
config.go
29
config.go
|
@ -1,11 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
|
@ -19,11 +16,9 @@ type Config struct {
|
|||
endStr string
|
||||
enableLog bool
|
||||
logFilePath string
|
||||
forWard []int
|
||||
forWard int
|
||||
frameSize int
|
||||
timesTamp bool
|
||||
timesFmt string
|
||||
address []string
|
||||
address string
|
||||
}
|
||||
type FoeWardMode int
|
||||
|
||||
|
@ -35,18 +30,17 @@ const (
|
|||
|
||||
var config Config
|
||||
|
||||
func setForWardClient(mode FoeWardMode, add string) (conn net.Conn) {
|
||||
var err error
|
||||
switch mode {
|
||||
func setForWardClient() (conn net.Conn) {
|
||||
switch FoeWardMode(config.forWard) {
|
||||
case NOT:
|
||||
|
||||
case TCPC:
|
||||
conn, err = net.Dial("tcp", add)
|
||||
conn, err = net.Dial("tcp", config.address)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
case UDPC:
|
||||
conn, err = net.Dial("udp", add)
|
||||
conn, err = net.Dial("udp", config.address)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -55,14 +49,3 @@ func setForWardClient(mode FoeWardMode, add string) (conn net.Conn) {
|
|||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
func checkLogOpen() {
|
||||
if config.enableLog {
|
||||
path := fmt.Sprintf(config.logFilePath, config.portName, time.Now().Format("2006_01_02T150405"))
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
outs = append(outs, f)
|
||||
}
|
||||
}
|
||||
|
|
77
flag.go
77
flag.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
inf "github.com/fzdwx/infinite"
|
||||
|
@ -11,7 +12,6 @@ import (
|
|||
"github.com/fzdwx/infinite/components/selection/singleselect"
|
||||
"github.com/fzdwx/infinite/style"
|
||||
"github.com/fzdwx/infinite/theme"
|
||||
"github.com/spf13/pflag"
|
||||
"go.bug.st/serial"
|
||||
"log"
|
||||
"strconv"
|
||||
|
@ -20,13 +20,10 @@ import (
|
|||
|
||||
type ptrVal struct {
|
||||
*string
|
||||
sl *[]string
|
||||
*int
|
||||
il *[]int
|
||||
*bool
|
||||
*float64
|
||||
*float32
|
||||
ext *string
|
||||
}
|
||||
type Val struct {
|
||||
string
|
||||
|
@ -34,7 +31,6 @@ type Val struct {
|
|||
bool
|
||||
float64
|
||||
float32
|
||||
extdef string
|
||||
}
|
||||
type Flag struct {
|
||||
v ptrVal
|
||||
|
@ -52,13 +48,13 @@ var (
|
|||
outputCode = Flag{ptrVal{string: &config.outputCode}, "o", "out", Val{string: "UTF-8"}, "输出编码"}
|
||||
inputCode = Flag{ptrVal{string: &config.inputCode}, "i", "in", Val{string: "UTF-8"}, "输入编码"}
|
||||
endStr = Flag{ptrVal{string: &config.endStr}, "e", "end", Val{string: "\n"}, "终端换行符"}
|
||||
logExt = Flag{v: ptrVal{ext: &config.logFilePath}, sStr: "l", lStr: "log", dv: Val{extdef: "./%s-$s.txt", string: ""}, help: "日志保存路径"}
|
||||
timeExt = Flag{v: ptrVal{ext: &config.timesFmt}, sStr: "t", lStr: "time", dv: Val{extdef: "[06-01-02 15:04:05.000]", string: ""}, help: "时间戳格式化字段"}
|
||||
forWard = Flag{ptrVal{il: &config.forWard}, "f", "forward", Val{int: 0}, "转发模式(0: 无 1:TCP-C 2:UDP-C 支持多次传入)"}
|
||||
address = Flag{ptrVal{sl: &config.address}, "a", "address", Val{string: "127.0.0.1:12345"}, "转发服务地址(支持多次传入)"}
|
||||
enableLog = Flag{ptrVal{bool: &config.enableLog}, "l", "log", Val{bool: false}, "是否启用日志保存"}
|
||||
logFilePath = Flag{ptrVal{string: &config.logFilePath}, "P", "Path", Val{string: "./Log.txt"}, "日志保存路径"}
|
||||
forWard = Flag{ptrVal{int: &config.forWard}, "f", "forward", Val{int: 0}, "转发模式(0: 无 1:TCP-C 2:UDP-C)"}
|
||||
address = Flag{ptrVal{string: &config.address}, "a", "address", Val{string: "127.0.0.1:12345"}, "转发服务地址"}
|
||||
frameSize = Flag{ptrVal{int: &config.frameSize}, "F", "Frame", Val{int: 16}, "帧大小"}
|
||||
parityBit = Flag{ptrVal{int: &config.parityBit}, "v", "verify", Val{int: 0}, "奇偶校验(0:无校验、1:奇校验、2:偶校验、3:1校验、4:0校验)"}
|
||||
flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, forWard, address, frameSize, parityBit, logExt, timeExt}
|
||||
flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, enableLog, logFilePath, forWard, address, frameSize, parityBit}
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -78,7 +74,6 @@ const (
|
|||
stringVal
|
||||
intVal
|
||||
boolVal
|
||||
extVal
|
||||
)
|
||||
|
||||
func printUsage(ports []string) {
|
||||
|
@ -98,9 +93,6 @@ func flagFindValue(v ptrVal) ValType {
|
|||
if v.int != nil {
|
||||
return intVal
|
||||
}
|
||||
if v.ext != nil {
|
||||
return extVal
|
||||
}
|
||||
return notVal
|
||||
}
|
||||
func flagprint(f Flag) {
|
||||
|
@ -111,41 +103,25 @@ func flagprint(f Flag) {
|
|||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", f.sStr, f.lStr, f.dv.int, f.help, f.dv.int)
|
||||
case boolVal:
|
||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", f.sStr, f.lStr, f.dv.bool, f.help, f.dv.bool)
|
||||
case extVal:
|
||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", f.sStr, f.lStr, f.dv.extdef, f.help, f.dv.extdef)
|
||||
default:
|
||||
panic("unhandled default case")
|
||||
}
|
||||
}
|
||||
func flagInit(f *Flag) {
|
||||
if f.v.string != nil {
|
||||
pflag.StringVarP(f.v.string, f.lStr, f.sStr, f.dv.string, f.help)
|
||||
flag.StringVar(f.v.string, f.sStr, f.dv.string, "")
|
||||
flag.StringVar(f.v.string, f.lStr, f.dv.string, f.help)
|
||||
}
|
||||
if f.v.bool != nil {
|
||||
pflag.BoolVarP(f.v.bool, f.lStr, f.sStr, f.dv.bool, f.help)
|
||||
flag.BoolVar(f.v.bool, f.sStr, f.dv.bool, "")
|
||||
flag.BoolVar(f.v.bool, f.lStr, f.dv.bool, f.help)
|
||||
}
|
||||
if f.v.int != nil {
|
||||
pflag.IntVarP(f.v.int, f.lStr, f.sStr, f.dv.int, f.help)
|
||||
}
|
||||
if f.v.ext != nil {
|
||||
pflag.StringVarP(f.v.ext, f.lStr, f.sStr, f.dv.string, f.help)
|
||||
pflag.Lookup(f.lStr).NoOptDefVal = f.dv.extdef
|
||||
}
|
||||
if f.v.sl != nil {
|
||||
pflag.StringArrayVarP(f.v.sl, f.lStr, f.sStr, []string{f.dv.string}, f.help)
|
||||
}
|
||||
if f.v.il != nil {
|
||||
pflag.IntSliceVarP(f.v.il, f.lStr, f.sStr, []int{f.dv.int}, f.help)
|
||||
}
|
||||
}
|
||||
func flagExt() {
|
||||
if config.logFilePath != "" {
|
||||
config.enableLog = true
|
||||
}
|
||||
if config.timesFmt != "" {
|
||||
config.timesTamp = true
|
||||
flag.IntVar(f.v.int, f.sStr, f.dv.int, "")
|
||||
flag.IntVar(f.v.int, f.lStr, f.dv.int, f.help)
|
||||
}
|
||||
}
|
||||
|
||||
func getCliFlag() {
|
||||
ports, err := serial.GetPortsList()
|
||||
if err != nil {
|
||||
|
@ -202,24 +178,6 @@ func getCliFlag() {
|
|||
).Display()
|
||||
if v {
|
||||
config.inputCode = "hex"
|
||||
b, _ := inf.NewText(
|
||||
text.WithPrompt("Frames:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue("16"),
|
||||
).Display()
|
||||
config.frameSize, _ = strconv.Atoi(b)
|
||||
}
|
||||
v, _ = inf.NewConfirmWithSelection(
|
||||
confirm.WithPrompt("启用时间戳"),
|
||||
).Display()
|
||||
config.timesTamp = v
|
||||
if v {
|
||||
b, _ := inf.NewText(
|
||||
text.WithPrompt("格式化字段:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue(timeExt.dv.extdef),
|
||||
).Display()
|
||||
config.timesFmt = b
|
||||
}
|
||||
v, _ = inf.NewConfirmWithSelection(
|
||||
confirm.WithPrompt("启用高级配置"),
|
||||
|
@ -276,7 +234,7 @@ func getCliFlag() {
|
|||
).Display()
|
||||
config.outputCode = t
|
||||
}
|
||||
G_F_mode:
|
||||
|
||||
s, _ = inf.NewSingleSelect(
|
||||
forwards,
|
||||
singleselect.WithKeyBinding(selectKeymap),
|
||||
|
@ -284,14 +242,13 @@ func getCliFlag() {
|
|||
singleselect.WithFilterInput(inputs),
|
||||
).Display("选择转发模式")
|
||||
if s != 0 {
|
||||
config.forWard = append(config.forWard, s)
|
||||
config.forWard = s
|
||||
t, _ = inf.NewText(
|
||||
text.WithPrompt("地址:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue(address.dv.string),
|
||||
).Display()
|
||||
config.address = append(config.address, t)
|
||||
goto G_F_mode
|
||||
config.address = t
|
||||
}
|
||||
|
||||
e, _ := inf.NewConfirmWithSelection(
|
||||
|
@ -303,7 +260,7 @@ func getCliFlag() {
|
|||
t, _ = inf.NewText(
|
||||
text.WithPrompt("Path:"),
|
||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||
text.WithDefaultValue("./%s-$s.txt"),
|
||||
text.WithDefaultValue(logFilePath.dv.string),
|
||||
).Display()
|
||||
config.logFilePath = t
|
||||
}
|
||||
|
|
36
go.mod
36
go.mod
|
@ -3,53 +3,35 @@ module COM
|
|||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/charmbracelet/bubbles v0.18.0
|
||||
github.com/fzdwx/infinite v0.12.1
|
||||
github.com/gobwas/ws v1.4.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
github.com/trzsz/trzsz-go v1.1.7
|
||||
github.com/zimolab/charsetconv v0.1.2
|
||||
go.bug.st/serial v1.6.2
|
||||
golang.org/x/term v0.19.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/UserExistsError/conpty v0.1.2 // indirect
|
||||
github.com/akavel/rsrc v0.10.2 // indirect
|
||||
github.com/alexflint/go-scalar v1.2.0 // indirect
|
||||
github.com/atotto/clipboard v0.1.4 // indirect
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/charmbracelet/bubbletea v0.25.0 // indirect
|
||||
github.com/charmbracelet/lipgloss v0.9.1 // indirect
|
||||
github.com/chzyer/readline v1.5.1 // indirect
|
||||
github.com/charmbracelet/bubbles v0.16.1 // indirect
|
||||
github.com/charmbracelet/bubbletea v0.24.2 // indirect
|
||||
github.com/charmbracelet/lipgloss v0.7.1 // indirect
|
||||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
|
||||
github.com/creack/goselect v0.1.2 // indirect
|
||||
github.com/creack/pty v1.1.21 // indirect
|
||||
github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f // indirect
|
||||
github.com/duke-git/lancet/v2 v2.2.1 // indirect
|
||||
github.com/fzdwx/iter v0.0.0-20230511075109-0afee9319312 // indirect
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/josephspurrier/goversioninfo v1.4.0 // indirect
|
||||
github.com/klauspost/compress v1.17.4 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||
github.com/muesli/reflow v0.3.0 // indirect
|
||||
github.com/muesli/termenv v0.15.2 // indirect
|
||||
github.com/ncruces/zenity v0.10.10 // indirect
|
||||
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect
|
||||
github.com/rivo/uniseg v0.4.6 // indirect
|
||||
github.com/muesli/termenv v0.15.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.4 // indirect
|
||||
github.com/rotisserie/eris v0.5.4 // indirect
|
||||
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
|
||||
github.com/trzsz/go-arg v1.5.3 // indirect
|
||||
github.com/trzsz/promptui v0.10.5 // indirect
|
||||
github.com/sahilm/fuzzy v0.1.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
|
||||
golang.org/x/image v0.14.0 // indirect
|
||||
golang.org/x/sync v0.2.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/term v0.19.0 // indirect
|
||||
golang.org/x/text v0.9.0 // indirect
|
||||
)
|
||||
|
|
BIN
image/img10.png
BIN
image/img10.png
Binary file not shown.
Before Width: | Height: | Size: 49 KiB |
BIN
image/img11.png
BIN
image/img11.png
Binary file not shown.
Before Width: | Height: | Size: 1001 KiB |
BIN
image/img12.png
BIN
image/img12.png
Binary file not shown.
Before Width: | Height: | Size: 186 KiB |
BIN
image/img8.png
BIN
image/img8.png
Binary file not shown.
Before Width: | Height: | Size: 335 KiB |
BIN
image/img9.png
BIN
image/img9.png
Binary file not shown.
Before Width: | Height: | Size: 73 KiB |
160
main.go
160
main.go
|
@ -1,24 +1,121 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/zimolab/charsetconv"
|
||||
"go.bug.st/serial"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
serialPort serial.Port
|
||||
err error
|
||||
args []string
|
||||
)
|
||||
|
||||
var (
|
||||
in io.Reader = os.Stdin
|
||||
out io.Writer = os.Stdout
|
||||
ins = []io.Reader{os.Stdin}
|
||||
outs = []io.Writer{os.Stdout}
|
||||
)
|
||||
|
||||
func checkPortAvailability(name string) ([]string, error) {
|
||||
ports, err := serial.GetPortsList()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if len(ports) == 0 {
|
||||
return nil, fmt.Errorf("无串口")
|
||||
}
|
||||
if name == "" {
|
||||
return ports, fmt.Errorf("串口未指定")
|
||||
}
|
||||
for _, port := range ports {
|
||||
if strings.Compare(port, name) == 0 {
|
||||
return ports, nil
|
||||
}
|
||||
}
|
||||
return ports, fmt.Errorf("串口 " + name + " 未在线")
|
||||
}
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile | log.Lmsgprefix)
|
||||
for _, f := range flags {
|
||||
flagInit(&f)
|
||||
}
|
||||
flag.Func("h", "获取帮助", func(s string) error {
|
||||
ports, err := checkPortAvailability(s)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
printUsage(ports)
|
||||
os.Exit(0)
|
||||
}
|
||||
return err
|
||||
})
|
||||
cmdinit()
|
||||
}
|
||||
|
||||
func input(in io.Reader) {
|
||||
input := bufio.NewScanner(in)
|
||||
var ok = false
|
||||
for {
|
||||
input.Scan()
|
||||
ok = false
|
||||
args = strings.Split(input.Text(), " ")
|
||||
for _, cmd := range commands {
|
||||
if strings.Compare(strings.TrimSpace(args[0]), cmd.name) == 0 {
|
||||
cmd.function()
|
||||
ok = true
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
_, err := io.WriteString(serialPort, input.Text())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
_, err = io.WriteString(serialPort, config.endStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
err = serialPort.Drain()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func strout(out io.Writer, cs, str string) {
|
||||
err = charsetconv.EncodeWith(strings.NewReader(str), out, charsetconv.Charset(cs), false)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func output() {
|
||||
if strings.Compare(config.inputCode, "hex") == 0 {
|
||||
b := make([]byte, 16)
|
||||
r, _ := io.LimitReader(serialPort, int64(config.frameSize)).Read(b)
|
||||
if r != 0 {
|
||||
strout(out, config.outputCode, fmt.Sprintf("% X %q \n", b, b))
|
||||
}
|
||||
} else {
|
||||
err = charsetconv.ConvertWith(serialPort, charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
pflag.Parse()
|
||||
flagExt()
|
||||
flag.Parse()
|
||||
|
||||
if config.portName == "" {
|
||||
getCliFlag()
|
||||
}
|
||||
|
@ -28,25 +125,46 @@ func main() {
|
|||
printUsage(ports)
|
||||
os.Exit(0)
|
||||
}
|
||||
mode := &serial.Mode{
|
||||
BaudRate: config.baudRate,
|
||||
StopBits: serial.StopBits(config.stopBits),
|
||||
DataBits: config.dataBits,
|
||||
Parity: serial.Parity(config.parityBit),
|
||||
}
|
||||
serialPort, err = serial.Open(config.portName, mode)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer func(port serial.Port) {
|
||||
err := port.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}(serialPort)
|
||||
|
||||
// 日志文件输出检测
|
||||
checkLogOpen()
|
||||
|
||||
//串口设备开启
|
||||
OpenSerial()
|
||||
|
||||
defer CloseSerial()
|
||||
// 打开文件服务
|
||||
OpenTrzsz()
|
||||
|
||||
defer CloseTrzsz()
|
||||
|
||||
//开启转发
|
||||
OpenForwarding()
|
||||
|
||||
// 获取终端输入
|
||||
go input(in)
|
||||
|
||||
if FoeWardMode(config.forWard) != NOT {
|
||||
conn := setForWardClient()
|
||||
ins = append(ins, conn)
|
||||
outs = append(outs, conn)
|
||||
defer func(conn net.Conn) {
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}(conn)
|
||||
}
|
||||
if len(ins) != 0 {
|
||||
for _, reader := range ins {
|
||||
go input(reader)
|
||||
}
|
||||
}
|
||||
if config.enableLog {
|
||||
f, err := os.OpenFile(config.logFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
outs = append(outs, f)
|
||||
}
|
||||
if len(outs) != 1 {
|
||||
out = io.MultiWriter(outs...)
|
||||
}
|
||||
|
|
85
mutual.go
85
mutual.go
|
@ -1,85 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/trzsz/trzsz-go/trzsz"
|
||||
"github.com/zimolab/charsetconv"
|
||||
"go.bug.st/serial"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
serialPort serial.Port
|
||||
in io.Reader = os.Stdin
|
||||
out io.Writer = os.Stdout
|
||||
outs = []io.Writer{os.Stdout}
|
||||
trzszFilter *trzsz.TrzszFilter
|
||||
clientIn *io.PipeReader
|
||||
stdoutPipe *io.PipeReader
|
||||
stdinPipe *io.PipeWriter
|
||||
clientOut *io.PipeWriter
|
||||
)
|
||||
|
||||
func input(in io.Reader) {
|
||||
var err error
|
||||
input := bufio.NewScanner(in)
|
||||
var ok = false
|
||||
for {
|
||||
input.Scan()
|
||||
ok = false
|
||||
args = strings.Split(input.Text(), " ")
|
||||
for _, cmd := range commands {
|
||||
if strings.Compare(strings.TrimSpace(args[0]), cmd.name) == 0 {
|
||||
cmd.function()
|
||||
ok = true
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
_, err := io.WriteString(stdinPipe, input.Text())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
_, err = io.WriteString(stdinPipe, config.endStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
err = serialPort.Drain()
|
||||
ErrorF(err)
|
||||
}
|
||||
}
|
||||
|
||||
func strout(out io.Writer, cs, str string) {
|
||||
err := charsetconv.EncodeWith(strings.NewReader(str), out, charsetconv.Charset(cs), false)
|
||||
ErrorF(err)
|
||||
}
|
||||
|
||||
func output() {
|
||||
var err error
|
||||
if strings.Compare(config.inputCode, "hex") == 0 {
|
||||
b := make([]byte, config.frameSize)
|
||||
r, _ := io.LimitReader(stdoutPipe, int64(config.frameSize)).Read(b)
|
||||
if r != 0 {
|
||||
if config.timesTamp {
|
||||
strout(out, config.outputCode, fmt.Sprintf("%v % X %q \n", time.Now().Format(config.timesFmt), b, b))
|
||||
} else {
|
||||
strout(out, config.outputCode, fmt.Sprintf("% X %q \n", b, b))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if config.timesTamp {
|
||||
line, _, _ := bufio.NewReader(stdoutPipe).ReadLine()
|
||||
if line != nil {
|
||||
strout(out, config.outputCode, fmt.Sprintf("%v %s\n", time.Now().Format(config.timesFmt), line))
|
||||
}
|
||||
} else {
|
||||
err = charsetconv.ConvertWith(stdoutPipe, charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
|
||||
}
|
||||
}
|
||||
ErrorP(err)
|
||||
}
|
125
utils.go
125
utils.go
|
@ -1,125 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/trzsz/trzsz-go/trzsz"
|
||||
"go.bug.st/serial"
|
||||
"golang.org/x/term"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func checkPortAvailability(name string) ([]string, error) {
|
||||
ports, err := serial.GetPortsList()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if len(ports) == 0 {
|
||||
return nil, fmt.Errorf("无串口")
|
||||
}
|
||||
if name == "" {
|
||||
return ports, fmt.Errorf("串口未指定")
|
||||
}
|
||||
for _, port := range ports {
|
||||
if strings.Compare(port, name) == 0 {
|
||||
return ports, nil
|
||||
}
|
||||
}
|
||||
return ports, fmt.Errorf("串口 " + name + " 未在线")
|
||||
}
|
||||
|
||||
func OpenSerial() {
|
||||
var err error
|
||||
mode := &serial.Mode{
|
||||
BaudRate: config.baudRate,
|
||||
StopBits: serial.StopBits(config.stopBits),
|
||||
DataBits: config.dataBits,
|
||||
Parity: serial.Parity(config.parityBit),
|
||||
}
|
||||
serialPort, err = serial.Open(config.portName, mode)
|
||||
ErrorF(err)
|
||||
return
|
||||
}
|
||||
|
||||
func CloseSerial() {
|
||||
err := serialPort.Close()
|
||||
ErrorF(err)
|
||||
return
|
||||
}
|
||||
|
||||
var termch chan os.Signal
|
||||
|
||||
// OpenTrzsz create a TrzszFilter to support trzsz ( trz / tsz ).
|
||||
//
|
||||
// ┌────────┐ stdinPipe ┌────────┐ ClientIn ┌─────────────┐ SerialIn ┌────────┐
|
||||
// │ ├─────────────►│ ├─────────────►│ ├─────────────►│ │
|
||||
// │ mutual │ │ Client │ │ TrzszFilter │ │ Serial │
|
||||
// │ │◄─────────────│ │◄─────────────┤ │◄─────────────┤ │
|
||||
// └────────┘ stdoutPipe └────────┘ ClientOut └─────────────┘ SerialOut └────────┘
|
||||
func OpenTrzsz() {
|
||||
fd := int(os.Stdin.Fd())
|
||||
width, _, err := term.GetSize(fd)
|
||||
if err != nil {
|
||||
if runtime.GOOS != "windows" {
|
||||
fmt.Printf("term get size failed: %s\n", err)
|
||||
return
|
||||
}
|
||||
width = 80
|
||||
}
|
||||
|
||||
clientIn, stdinPipe = io.Pipe()
|
||||
stdoutPipe, clientOut = io.Pipe()
|
||||
trzszFilter = trzsz.NewTrzszFilter(clientIn, clientOut, serialPort, serialPort,
|
||||
trzsz.TrzszOptions{TerminalColumns: int32(width), EnableZmodem: true})
|
||||
trzsz.SetAffectedByWindows(false)
|
||||
termch = make(chan os.Signal, 1)
|
||||
go func() {
|
||||
for range termch {
|
||||
width, _, err := term.GetSize(fd)
|
||||
if err != nil {
|
||||
fmt.Printf("term get size failed: %s\n", err)
|
||||
continue
|
||||
}
|
||||
trzszFilter.SetTerminalColumns(int32(width))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func CloseTrzsz() {
|
||||
signal.Stop(termch)
|
||||
close(termch)
|
||||
}
|
||||
|
||||
func OpenForwarding() {
|
||||
for i, mode := range config.forWard {
|
||||
if FoeWardMode(mode) != NOT {
|
||||
conn := setForWardClient(FoeWardMode(mode), config.address[i])
|
||||
outs = append(outs, conn)
|
||||
go func() {
|
||||
defer func(conn net.Conn) {
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}(conn)
|
||||
input(conn)
|
||||
}()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ErrorP(err error) {
|
||||
if err != nil {
|
||||
fmt.Fprint(os.Stderr, err)
|
||||
}
|
||||
}
|
||||
func ErrorF(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue