parent
88285df82b
commit
195fc59d01
|
@ -48,5 +48,5 @@
|
||||||

|

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

|

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

|

|
|
@ -1,9 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -55,7 +57,8 @@ func setForWardClient() (conn net.Conn) {
|
||||||
|
|
||||||
func checkLogOpen() {
|
func checkLogOpen() {
|
||||||
if config.enableLog {
|
if config.enableLog {
|
||||||
f, err := os.OpenFile(config.logFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
50
flag.go
50
flag.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/charmbracelet/bubbles/key"
|
"github.com/charmbracelet/bubbles/key"
|
||||||
inf "github.com/fzdwx/infinite"
|
inf "github.com/fzdwx/infinite"
|
||||||
|
@ -12,6 +11,7 @@ import (
|
||||||
"github.com/fzdwx/infinite/components/selection/singleselect"
|
"github.com/fzdwx/infinite/components/selection/singleselect"
|
||||||
"github.com/fzdwx/infinite/style"
|
"github.com/fzdwx/infinite/style"
|
||||||
"github.com/fzdwx/infinite/theme"
|
"github.com/fzdwx/infinite/theme"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
"go.bug.st/serial"
|
"go.bug.st/serial"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -24,6 +24,7 @@ type ptrVal struct {
|
||||||
*bool
|
*bool
|
||||||
*float64
|
*float64
|
||||||
*float32
|
*float32
|
||||||
|
ext *string
|
||||||
}
|
}
|
||||||
type Val struct {
|
type Val struct {
|
||||||
string
|
string
|
||||||
|
@ -31,6 +32,7 @@ type Val struct {
|
||||||
bool
|
bool
|
||||||
float64
|
float64
|
||||||
float32
|
float32
|
||||||
|
extdef string
|
||||||
}
|
}
|
||||||
type Flag struct {
|
type Flag struct {
|
||||||
v ptrVal
|
v ptrVal
|
||||||
|
@ -48,15 +50,13 @@ var (
|
||||||
outputCode = Flag{ptrVal{string: &config.outputCode}, "o", "out", Val{string: "UTF-8"}, "输出编码"}
|
outputCode = Flag{ptrVal{string: &config.outputCode}, "o", "out", Val{string: "UTF-8"}, "输出编码"}
|
||||||
inputCode = Flag{ptrVal{string: &config.inputCode}, "i", "in", 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"}, "终端换行符"}
|
endStr = Flag{ptrVal{string: &config.endStr}, "e", "end", Val{string: "\n"}, "终端换行符"}
|
||||||
enableLog = Flag{ptrVal{bool: &config.enableLog}, "l", "log", Val{bool: false}, "是否启用日志保存"}
|
logExt = Flag{v: ptrVal{ext: &config.logFilePath}, sStr: "l", lStr: "log", dv: Val{extdef: "./%s-$s.txt", string: ""}, help: "日志保存路径"}
|
||||||
logFilePath = Flag{ptrVal{string: &config.logFilePath}, "P", "Path", Val{string: "./Log.txt"}, "日志保存路径"}
|
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)"}
|
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"}, "转发服务地址"}
|
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}, "帧大小"}
|
frameSize = Flag{ptrVal{int: &config.frameSize}, "F", "Frame", Val{int: 16}, "帧大小"}
|
||||||
timesTamp = Flag{ptrVal{bool: &config.timesTamp}, "t", "tamp", Val{bool: false}, "是否启用接收时间戳"}
|
|
||||||
timesFmt = Flag{ptrVal{string: &config.timesFmt}, "T", "TimeFmt", Val{string: "[06-01-02 15:04:05]"}, "时间戳格式化字段"}
|
|
||||||
parityBit = Flag{ptrVal{int: &config.parityBit}, "v", "verify", Val{int: 0}, "奇偶校验(0:无校验、1:奇校验、2:偶校验、3:1校验、4:0校验)"}
|
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, timesTamp, timesFmt, parityBit}
|
flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, forWard, address, frameSize, parityBit, logExt, timeExt}
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -76,6 +76,7 @@ const (
|
||||||
stringVal
|
stringVal
|
||||||
intVal
|
intVal
|
||||||
boolVal
|
boolVal
|
||||||
|
extVal
|
||||||
)
|
)
|
||||||
|
|
||||||
func printUsage(ports []string) {
|
func printUsage(ports []string) {
|
||||||
|
@ -95,6 +96,9 @@ func flagFindValue(v ptrVal) ValType {
|
||||||
if v.int != nil {
|
if v.int != nil {
|
||||||
return intVal
|
return intVal
|
||||||
}
|
}
|
||||||
|
if v.ext != nil {
|
||||||
|
return extVal
|
||||||
|
}
|
||||||
return notVal
|
return notVal
|
||||||
}
|
}
|
||||||
func flagprint(f Flag) {
|
func flagprint(f Flag) {
|
||||||
|
@ -105,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)
|
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:
|
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)
|
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:
|
default:
|
||||||
panic("unhandled default case")
|
panic("unhandled default case")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func flagInit(f *Flag) {
|
func flagInit(f *Flag) {
|
||||||
if f.v.string != nil {
|
if f.v.string != nil {
|
||||||
flag.StringVar(f.v.string, f.sStr, f.dv.string, "")
|
pflag.StringVarP(f.v.string, f.lStr, f.sStr, f.dv.string, f.help)
|
||||||
flag.StringVar(f.v.string, f.lStr, f.dv.string, f.help)
|
|
||||||
}
|
}
|
||||||
if f.v.bool != nil {
|
if f.v.bool != nil {
|
||||||
flag.BoolVar(f.v.bool, f.sStr, f.dv.bool, "")
|
pflag.BoolVarP(f.v.bool, f.lStr, f.sStr, f.dv.bool, f.help)
|
||||||
flag.BoolVar(f.v.bool, f.lStr, f.dv.bool, f.help)
|
|
||||||
}
|
}
|
||||||
if f.v.int != nil {
|
if f.v.int != nil {
|
||||||
flag.IntVar(f.v.int, f.sStr, f.dv.int, "")
|
pflag.IntVarP(f.v.int, f.lStr, f.sStr, f.dv.int, f.help)
|
||||||
flag.IntVar(f.v.int, f.lStr, 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() {
|
func getCliFlag() {
|
||||||
ports, err := serial.GetPortsList()
|
ports, err := serial.GetPortsList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -190,14 +204,14 @@ func getCliFlag() {
|
||||||
v, _ = inf.NewConfirmWithSelection(
|
v, _ = inf.NewConfirmWithSelection(
|
||||||
confirm.WithPrompt("启用时间戳"),
|
confirm.WithPrompt("启用时间戳"),
|
||||||
).Display()
|
).Display()
|
||||||
|
config.timesTamp = v
|
||||||
if v {
|
if v {
|
||||||
config.inputCode = "hex"
|
|
||||||
b, _ := inf.NewText(
|
b, _ := inf.NewText(
|
||||||
text.WithPrompt("Frames:"),
|
text.WithPrompt("格式化字段:"),
|
||||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||||
text.WithDefaultValue("16"),
|
text.WithDefaultValue(logExt.dv.extdef),
|
||||||
).Display()
|
).Display()
|
||||||
config.frameSize, _ = strconv.Atoi(b)
|
config.timesFmt = b
|
||||||
}
|
}
|
||||||
v, _ = inf.NewConfirmWithSelection(
|
v, _ = inf.NewConfirmWithSelection(
|
||||||
confirm.WithPrompt("启用高级配置"),
|
confirm.WithPrompt("启用高级配置"),
|
||||||
|
@ -280,7 +294,7 @@ func getCliFlag() {
|
||||||
t, _ = inf.NewText(
|
t, _ = inf.NewText(
|
||||||
text.WithPrompt("Path:"),
|
text.WithPrompt("Path:"),
|
||||||
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||||
text.WithDefaultValue(logFilePath.dv.string),
|
text.WithDefaultValue("./%s-$s.txt"),
|
||||||
).Display()
|
).Display()
|
||||||
config.logFilePath = t
|
config.logFilePath = t
|
||||||
}
|
}
|
||||||
|
|
7
go.mod
7
go.mod
|
@ -4,12 +4,12 @@ go 1.22
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/charmbracelet/bubbles v0.18.0
|
github.com/charmbracelet/bubbles v0.18.0
|
||||||
github.com/charmbracelet/bubbletea v0.25.0
|
|
||||||
github.com/fzdwx/infinite v0.12.1
|
github.com/fzdwx/infinite v0.12.1
|
||||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
github.com/spf13/pflag v1.0.5
|
||||||
github.com/trzsz/trzsz-go v1.1.7
|
github.com/trzsz/trzsz-go v1.1.7
|
||||||
github.com/zimolab/charsetconv v0.1.2
|
github.com/zimolab/charsetconv v0.1.2
|
||||||
go.bug.st/serial v1.6.2
|
go.bug.st/serial v1.6.2
|
||||||
|
golang.org/x/term v0.19.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
@ -18,6 +18,7 @@ require (
|
||||||
github.com/alexflint/go-scalar v1.2.0 // indirect
|
github.com/alexflint/go-scalar v1.2.0 // indirect
|
||||||
github.com/atotto/clipboard v0.1.4 // indirect
|
github.com/atotto/clipboard v0.1.4 // indirect
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // 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/charmbracelet/lipgloss v0.9.1 // indirect
|
||||||
github.com/chzyer/readline v1.5.1 // indirect
|
github.com/chzyer/readline v1.5.1 // indirect
|
||||||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
|
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
|
||||||
|
@ -25,7 +26,6 @@ require (
|
||||||
github.com/creack/pty v1.1.21 // indirect
|
github.com/creack/pty v1.1.21 // indirect
|
||||||
github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f // indirect
|
github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f // indirect
|
||||||
github.com/duke-git/lancet/v2 v2.2.1 // indirect
|
github.com/duke-git/lancet/v2 v2.2.1 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
|
||||||
github.com/fzdwx/iter v0.0.0-20230511075109-0afee9319312 // indirect
|
github.com/fzdwx/iter v0.0.0-20230511075109-0afee9319312 // indirect
|
||||||
github.com/josephspurrier/goversioninfo v1.4.0 // indirect
|
github.com/josephspurrier/goversioninfo v1.4.0 // indirect
|
||||||
github.com/klauspost/compress v1.17.4 // indirect
|
github.com/klauspost/compress v1.17.4 // indirect
|
||||||
|
@ -48,6 +48,5 @@ require (
|
||||||
golang.org/x/image v0.14.0 // indirect
|
golang.org/x/image v0.14.0 // indirect
|
||||||
golang.org/x/sync v0.2.0 // indirect
|
golang.org/x/sync v0.2.0 // indirect
|
||||||
golang.org/x/sys v0.19.0 // indirect
|
golang.org/x/sys v0.19.0 // indirect
|
||||||
golang.org/x/term v0.19.0 // indirect
|
|
||||||
golang.org/x/text v0.14.0 // indirect
|
golang.org/x/text v0.14.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
5
main.go
5
main.go
|
@ -2,8 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
"github.com/trzsz/trzsz-go/trzsz"
|
"github.com/trzsz/trzsz-go/trzsz"
|
||||||
"github.com/zimolab/charsetconv"
|
"github.com/zimolab/charsetconv"
|
||||||
"go.bug.st/serial"
|
"go.bug.st/serial"
|
||||||
|
@ -126,7 +126,8 @@ func output() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
pflag.Parse()
|
||||||
|
flagExt()
|
||||||
if config.portName == "" {
|
if config.portName == "" {
|
||||||
getCliFlag()
|
getCliFlag()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue