6 Commits

Author SHA1 Message Date
jixishi
c841dfeae4 UP 功能建议更新 2024-06-03 20:54:23 +08:00
jixishi
88285df82b UP README.md 2024-05-30 16:43:21 +08:00
jixishi
67e2f5a1c8 UP README.md 2024-05-30 16:35:29 +08:00
jixishi
17950c05dc UP releaser 2024-05-30 16:23:53 +08:00
jixishi
9b374fc42d Merge branch 'dev' 2024-05-30 16:04:55 +08:00
jixishi
82ec65958e 时间戳 文件传输 支持 2024-05-30 15:56:52 +08:00
10 changed files with 178 additions and 62 deletions

View File

@@ -23,6 +23,15 @@ builds:
- linux
- windows
- darwin
ldflags:
- -s -w
upx:
- enabled: true
goos:
- windows
goarch:
- amd64
archives:
- format: tar.gz

View File

@@ -15,7 +15,7 @@
* [x] TCP数据转发
* [x] 参数交互配置
* [x] Ctrl组合键
* [ ] 文件接收发送
* [x] 文件接收发送(trzsz lrzsz都支持)
## 运行示例
@@ -42,3 +42,11 @@
7. Ctrl组合键发送指令.ctrl `.ctrl c`
![img7.png](image/img7.png)
8. 文件上传演示 `index.html`
![img8.png](image/img8.png)
内容对比
![img11.png](image/img11.png)
9. 时间戳 `./COM -p COM8 -t`
![img9.png](image/img9.png)
10. 格式修改 `./COM -p COM11 -t='<2006-01-02 15:04:05>'`
![img10.png](image/img10.png)

View File

@@ -1,8 +1,11 @@
package main
import (
"fmt"
"log"
"net"
"os"
"time"
)
type Config struct {
@@ -18,6 +21,8 @@ type Config struct {
logFilePath string
forWard int
frameSize int
timesTamp bool
timesFmt string
address string
}
type FoeWardMode int
@@ -49,3 +54,14 @@ func setForWardClient() (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)
}
}

80
flag.go
View File

@@ -1,7 +1,6 @@
package main
import (
"flag"
"fmt"
"github.com/charmbracelet/bubbles/key"
inf "github.com/fzdwx/infinite"
@@ -12,6 +11,7 @@ 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"
@@ -24,6 +24,7 @@ type ptrVal struct {
*bool
*float64
*float32
ext *string
}
type Val struct {
string
@@ -31,6 +32,7 @@ type Val struct {
bool
float64
float32
extdef string
}
type Flag struct {
v ptrVal
@@ -41,20 +43,20 @@ type Flag struct {
}
var (
portName = Flag{ptrVal{string: &config.portName}, "p", "port", Val{string: ""}, "要连接的串口\t(/dev/ttyUSB0、COMx)"}
baudRate = Flag{ptrVal{int: &config.baudRate}, "b", "baud", Val{int: 115200}, "波特率"}
dataBits = Flag{ptrVal{int: &config.dataBits}, "d", "data", Val{int: 8}, "数据位"}
stopBits = Flag{ptrVal{int: &config.stopBits}, "s", "stop", Val{int: 0}, "停止位停止位(0: 1停止 1:1.5停止 2:2停止)"}
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"}, "终端换行符"}
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, enableLog, logFilePath, forWard, address, frameSize, parityBit}
portName = Flag{ptrVal{string: &config.portName}, "p", "port", Val{string: ""}, "要连接的串口\t(/dev/ttyUSB0、COMx)"}
baudRate = Flag{ptrVal{int: &config.baudRate}, "b", "baud", Val{int: 115200}, "波特率"}
dataBits = Flag{ptrVal{int: &config.dataBits}, "d", "data", Val{int: 8}, "数据位"}
stopBits = Flag{ptrVal{int: &config.stopBits}, "s", "stop", Val{int: 0}, "停止位停止位(0: 1停止 1:1.5停止 2:2停止)"}
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{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}
)
var (
@@ -74,6 +76,7 @@ const (
stringVal
intVal
boolVal
extVal
)
func printUsage(ports []string) {
@@ -93,6 +96,9 @@ func flagFindValue(v ptrVal) ValType {
if v.int != nil {
return intVal
}
if v.ext != nil {
return extVal
}
return notVal
}
func flagprint(f Flag) {
@@ -103,25 +109,35 @@ 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 {
flag.StringVar(f.v.string, f.sStr, f.dv.string, "")
flag.StringVar(f.v.string, f.lStr, f.dv.string, f.help)
pflag.StringVarP(f.v.string, f.lStr, f.sStr, f.dv.string, f.help)
}
if f.v.bool != nil {
flag.BoolVar(f.v.bool, f.sStr, f.dv.bool, "")
flag.BoolVar(f.v.bool, f.lStr, f.dv.bool, f.help)
pflag.BoolVarP(f.v.bool, f.lStr, f.sStr, f.dv.bool, f.help)
}
if f.v.int != nil {
flag.IntVar(f.v.int, f.sStr, f.dv.int, "")
flag.IntVar(f.v.int, f.lStr, f.dv.int, f.help)
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
}
}
func flagExt() {
if config.logFilePath != "" {
config.enableLog = true
}
if config.timesFmt != "" {
config.timesTamp = true
}
}
func getCliFlag() {
ports, err := serial.GetPortsList()
if err != nil {
@@ -178,6 +194,24 @@ 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(logExt.dv.extdef),
).Display()
config.timesFmt = b
}
v, _ = inf.NewConfirmWithSelection(
confirm.WithPrompt("启用高级配置"),
@@ -260,7 +294,7 @@ func getCliFlag() {
t, _ = inf.NewText(
text.WithPrompt("Path:"),
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
text.WithDefaultValue(logFilePath.dv.string),
text.WithDefaultValue("./%s-$s.txt"),
).Display()
config.logFilePath = t
}

33
go.mod
View File

@@ -3,35 +3,50 @@ module COM
go 1.22
require (
github.com/charmbracelet/bubbles v0.18.0
github.com/fzdwx/infinite v0.12.1
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/bubbles v0.16.1 // indirect
github.com/charmbracelet/bubbletea v0.24.2 // indirect
github.com/charmbracelet/lipgloss v0.7.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/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/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.14 // indirect
github.com/mattn/go-runewidth v0.0.15 // 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.1 // indirect
github.com/rivo/uniseg v0.4.4 // 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/rotisserie/eris v0.5.4 // indirect
github.com/sahilm/fuzzy v0.1.0 // 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
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/term v0.19.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

BIN
image/img10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
image/img11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1001 KiB

BIN
image/img8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 335 KiB

BIN
image/img9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

92
main.go
View File

@@ -2,15 +2,20 @@ package main
import (
"bufio"
"flag"
"fmt"
"github.com/spf13/pflag"
"github.com/trzsz/trzsz-go/trzsz"
"github.com/zimolab/charsetconv"
"go.bug.st/serial"
"golang.org/x/term"
"io"
"log"
"net"
"os"
"os/signal"
"runtime"
"strings"
"time"
)
var (
@@ -20,10 +25,15 @@ var (
)
var (
in io.Reader = os.Stdin
out io.Writer = os.Stdout
ins = []io.Reader{os.Stdin}
outs = []io.Writer{os.Stdout}
in io.Reader = os.Stdin
out io.Writer = os.Stdout
ins = []io.Reader{os.Stdin}
outs = []io.Writer{os.Stdout}
trzszFilter *trzsz.TrzszFilter
clientIn *io.PipeReader
stdoutPipe *io.PipeReader
stdinPipe *io.PipeWriter
clientOut *io.PipeWriter
)
func checkPortAvailability(name string) ([]string, error) {
@@ -50,15 +60,6 @@ func init() {
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()
}
@@ -76,11 +77,11 @@ func input(in io.Reader) {
}
}
if !ok {
_, err := io.WriteString(serialPort, input.Text())
_, err := io.WriteString(stdinPipe, input.Text())
if err != nil {
log.Fatal(err)
}
_, err = io.WriteString(serialPort, config.endStr)
_, err = io.WriteString(stdinPipe, config.endStr)
if err != nil {
log.Fatal(err)
}
@@ -101,21 +102,32 @@ func strout(out io.Writer, cs, str string) {
func output() {
if strings.Compare(config.inputCode, "hex") == 0 {
b := make([]byte, 16)
r, _ := io.LimitReader(serialPort, int64(config.frameSize)).Read(b)
b := make([]byte, config.frameSize)
r, _ := io.LimitReader(stdoutPipe, int64(config.frameSize)).Read(b)
if r != 0 {
strout(out, config.outputCode, fmt.Sprintf("% X %q \n", b, b))
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 {
err = charsetconv.ConvertWith(serialPort, charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
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)
}
}
if err != nil {
log.Fatal(err)
}
}
func main() {
flag.Parse()
pflag.Parse()
flagExt()
if config.portName == "" {
getCliFlag()
}
@@ -141,6 +153,33 @@ func main() {
log.Fatal(err)
}
}(serialPort)
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)
ch := make(chan os.Signal, 1)
go func() {
for range ch {
width, _, err := term.GetSize(fd)
if err != nil {
fmt.Printf("term get size failed: %s\n", err)
continue
}
trzszFilter.SetTerminalColumns(int32(width))
}
}()
defer func() { signal.Stop(ch); close(ch) }()
if FoeWardMode(config.forWard) != NOT {
conn := setForWardClient()
@@ -153,18 +192,13 @@ func main() {
}
}(conn)
}
checkLogOpen()
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...)
}