mirror of
https://github.com/jixishi/SerialTerminalForWindowsTerminal.git
synced 2025-09-16 22:22:21 +00:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
68f285b5ed | ||
|
d2f8c8a268 | ||
|
8d4273df77 | ||
|
92c92e67e1 | ||
|
604e5bb4ad | ||
|
e3415ae05a | ||
|
d19c09e4cd | ||
|
5bf90d1b63 | ||
|
dffb269247 |
27
README.md
27
README.md
@@ -1,10 +1,21 @@
|
|||||||
# SerialTerminalForWindowsTerminal
|
# SerialTerminalForWindowsTerminal
|
||||||
在编写这个项目之前在windows Terminal中对串口设备的支持不是很棒,
|
在开始这个项目之前,我发现Windows Terminal对串口设备的支持并不理想。
|
||||||
|
|
||||||
用过一段时间的
|
我试用了一段时间[Zhou-zhi-peng的SerialPortForWindowsTerminal](https://github.com/Zhou-zhi-peng/SerialPortForWindowsTerminal/)项目。
|
||||||
[SerialPortForWindowsTerminal](https://github.com/Zhou-zhi-peng/SerialPortForWindowsTerminal/)
|
|
||||||
项目。
|
然而,这个项目存在着编码转换的问题,导致数据显示乱码,并且作者目前并没有进行后续支持。因此,我决定创建了这个项目。
|
||||||
此项目没有编码转换能力在使用过程中有乱码的问题,并且作者截至目前并没有进行后续支持,于是我便编写了此项目
|
|
||||||
|
## 功能进展
|
||||||
|
* [x] Hex接收发送(大写hex与原文同显)
|
||||||
|
* [x] 双向编码转换
|
||||||
|
* [x] 活动端口探测
|
||||||
|
* [x] 数据日志保存
|
||||||
|
* [x] Hex断帧设置
|
||||||
|
* [x] UDP数据转发
|
||||||
|
* [x] TCP数据转发
|
||||||
|
* [x] 参数交互配置
|
||||||
|
* [x] Ctrl组合键
|
||||||
|
* [ ] 文件接收发送
|
||||||
|
|
||||||
## 运行示例
|
## 运行示例
|
||||||
|
|
||||||
@@ -25,3 +36,9 @@
|
|||||||
5. Hex发送 `./COM -p COM8 -b 115200`
|
5. Hex发送 `./COM -p COM8 -b 115200`
|
||||||
|
|
||||||

|

|
||||||
|
6. 交互配置 `./COM`
|
||||||
|
|
||||||
|

|
||||||
|
7. Ctrl组合键发送指令.ctrl `.ctrl c`
|
||||||
|
|
||||||
|

|
||||||
|
23
command.go
23
command.go
@@ -18,21 +18,30 @@ var commands []Command
|
|||||||
|
|
||||||
func cmdhelp() {
|
func cmdhelp() {
|
||||||
var page = 0
|
var page = 0
|
||||||
fmt.Printf(">-------Help(%v)-------<\n", page)
|
strout(out, config.outputCode, fmt.Sprintf(">-------Help(%v)-------<\n", page))
|
||||||
for i := 0; i < len(commands); i++ {
|
for i := 0; i < len(commands); i++ {
|
||||||
strout(config.outputCode, fmt.Sprintf(" %-10v --%v\n", commands[i].name, commands[i].description))
|
strout(out, config.outputCode, fmt.Sprintf(" %-10v --%v\n", commands[i].name, commands[i].description))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func cmdexit() {
|
func cmdexit() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
func cmdargs() {
|
func cmdargs() {
|
||||||
fmt.Printf(">-------Args(%v)-------<\n", len(args)-1)
|
strout(out, config.outputCode, fmt.Sprintf(">-------Args(%v)-------<\n", len(args)-1))
|
||||||
fmt.Printf("%q\n", args[1:])
|
strout(out, config.outputCode, fmt.Sprintf("%q\n", args[1:]))
|
||||||
|
}
|
||||||
|
func cmdctrl() {
|
||||||
|
b := []byte(args[1])
|
||||||
|
x := []byte{b[0] & 0x1f}
|
||||||
|
_, err = serialPort.Write(x)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf("Ctrl+%s\n", b))
|
||||||
}
|
}
|
||||||
func cmdhex() {
|
func cmdhex() {
|
||||||
fmt.Printf(">-----Hex Send-----<\n")
|
strout(out, config.outputCode, fmt.Sprintf(">-----Hex Send-----<\n"))
|
||||||
fmt.Printf("%q\n", args[1:])
|
strout(out, config.outputCode, fmt.Sprintf("%q\n", args[1:]))
|
||||||
s := strings.Join(args[1:], "")
|
s := strings.Join(args[1:], "")
|
||||||
b, err := hex.DecodeString(s)
|
b, err := hex.DecodeString(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -45,7 +54,7 @@ func cmdhex() {
|
|||||||
}
|
}
|
||||||
func cmdinit() {
|
func cmdinit() {
|
||||||
commands = append(commands, Command{name: ".help", description: "帮助信息", function: cmdhelp})
|
commands = append(commands, Command{name: ".help", description: "帮助信息", function: cmdhelp})
|
||||||
commands = append(commands, Command{name: ".args", description: "参数信息", function: cmdargs})
|
commands = append(commands, Command{name: ".ctrl", description: "发送Ctrl组合键", function: cmdctrl})
|
||||||
commands = append(commands, Command{name: ".hex", description: "发送Hex", function: cmdhex})
|
commands = append(commands, Command{name: ".hex", description: "发送Hex", function: cmdhex})
|
||||||
commands = append(commands, Command{name: ".exit", description: "退出终端", function: cmdexit})
|
commands = append(commands, Command{name: ".exit", description: "退出终端", function: cmdexit})
|
||||||
}
|
}
|
||||||
|
37
config.go
37
config.go
@@ -1,16 +1,51 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
portName string
|
portName string
|
||||||
baudRate int
|
baudRate int
|
||||||
dataBits int
|
dataBits int
|
||||||
stopBits int
|
stopBits int
|
||||||
|
parityBit int
|
||||||
outputCode string
|
outputCode string
|
||||||
inputCode string
|
inputCode string
|
||||||
endStr string
|
endStr string
|
||||||
enableLog bool
|
enableLog bool
|
||||||
logFilePath string
|
logFilePath string
|
||||||
parityBit int
|
forWard int
|
||||||
|
frameSize int
|
||||||
|
address string
|
||||||
}
|
}
|
||||||
|
type FoeWardMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
NOT FoeWardMode = iota
|
||||||
|
TCPC
|
||||||
|
UDPC
|
||||||
|
)
|
||||||
|
|
||||||
var config Config
|
var config Config
|
||||||
|
|
||||||
|
func setForWardClient() (conn net.Conn) {
|
||||||
|
switch FoeWardMode(config.forWard) {
|
||||||
|
case NOT:
|
||||||
|
|
||||||
|
case TCPC:
|
||||||
|
conn, err = net.Dial("tcp", config.address)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
case UDPC:
|
||||||
|
conn, err = net.Dial("udp", config.address)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("未知模式设置")
|
||||||
|
}
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
173
flag.go
173
flag.go
@@ -3,6 +3,18 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/charmbracelet/bubbles/key"
|
||||||
|
inf "github.com/fzdwx/infinite"
|
||||||
|
"github.com/fzdwx/infinite/color"
|
||||||
|
"github.com/fzdwx/infinite/components"
|
||||||
|
"github.com/fzdwx/infinite/components/input/text"
|
||||||
|
"github.com/fzdwx/infinite/components/selection/confirm"
|
||||||
|
"github.com/fzdwx/infinite/components/selection/singleselect"
|
||||||
|
"github.com/fzdwx/infinite/style"
|
||||||
|
"github.com/fzdwx/infinite/theme"
|
||||||
|
"go.bug.st/serial"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,8 +50,21 @@ var (
|
|||||||
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}, "是否启用日志保存"}
|
enableLog = Flag{ptrVal{bool: &config.enableLog}, "l", "log", Val{bool: false}, "是否启用日志保存"}
|
||||||
logFilePath = Flag{ptrVal{string: &config.logFilePath}, "P", "Path", Val{string: "./Log.txt"}, "日志保存路径"}
|
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校验)"}
|
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, parityBit}
|
flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, enableLog, logFilePath, forWard, address, frameSize, parityBit}
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
bauds = []string{"自定义", "300", "600", "1200", "2400", "4800", "9600",
|
||||||
|
"14400", "19200", "38400", "56000", "57600", "115200", "128000",
|
||||||
|
"256000", "460800", "512000", "750000", "921600", "1500000"}
|
||||||
|
datas = []string{"5", "6", "7", "8"}
|
||||||
|
stops = []string{"1", "1.5", "2"}
|
||||||
|
paritys = []string{"无校验", "奇校验", "偶校验", "1校验", "0校验"}
|
||||||
|
forwards = []string{"No", "TCP-C", "UDP-C"}
|
||||||
)
|
)
|
||||||
|
|
||||||
type ValType int
|
type ValType int
|
||||||
@@ -96,3 +121,149 @@ func flagInit(f *Flag) {
|
|||||||
flag.IntVar(f.v.int, f.lStr, f.dv.int, f.help)
|
flag.IntVar(f.v.int, f.lStr, f.dv.int, f.help)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCliFlag() {
|
||||||
|
ports, err := serial.GetPortsList()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputs := components.NewInput()
|
||||||
|
inputs.Prompt = "Filtering: "
|
||||||
|
inputs.PromptStyle = style.New().Bold().Italic().Fg(color.LightBlue)
|
||||||
|
|
||||||
|
selectKeymap := singleselect.DefaultSingleKeyMap()
|
||||||
|
selectKeymap.Confirm = key.NewBinding(
|
||||||
|
key.WithKeys("enter"),
|
||||||
|
key.WithHelp("enter", "finish select"),
|
||||||
|
)
|
||||||
|
selectKeymap.Choice = key.NewBinding(
|
||||||
|
key.WithKeys("enter"),
|
||||||
|
key.WithHelp("enter", "finish select"),
|
||||||
|
)
|
||||||
|
selectKeymap.NextPage = key.NewBinding(
|
||||||
|
key.WithKeys("right"),
|
||||||
|
key.WithHelp("->", "next page"),
|
||||||
|
)
|
||||||
|
selectKeymap.PrevPage = key.NewBinding(
|
||||||
|
key.WithKeys("left"),
|
||||||
|
key.WithHelp("<-", "prev page"),
|
||||||
|
)
|
||||||
|
|
||||||
|
s, _ := inf.NewSingleSelect(
|
||||||
|
ports,
|
||||||
|
singleselect.WithKeyBinding(selectKeymap),
|
||||||
|
singleselect.WithPageSize(4),
|
||||||
|
singleselect.WithFilterInput(inputs),
|
||||||
|
).Display("选择串口")
|
||||||
|
config.portName = ports[s]
|
||||||
|
|
||||||
|
s, _ = inf.NewSingleSelect(
|
||||||
|
bauds,
|
||||||
|
singleselect.WithKeyBinding(selectKeymap),
|
||||||
|
singleselect.WithPageSize(4),
|
||||||
|
).Display("选择波特率")
|
||||||
|
if s != 0 {
|
||||||
|
config.baudRate, _ = strconv.Atoi(bauds[s])
|
||||||
|
} else {
|
||||||
|
b, _ := inf.NewText(
|
||||||
|
text.WithPrompt("BaudRate:"),
|
||||||
|
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||||
|
text.WithDefaultValue("115200"),
|
||||||
|
).Display()
|
||||||
|
config.baudRate, _ = strconv.Atoi(b)
|
||||||
|
}
|
||||||
|
v, _ := inf.NewConfirmWithSelection(
|
||||||
|
confirm.WithPrompt("启用Hex"),
|
||||||
|
).Display()
|
||||||
|
if v {
|
||||||
|
config.inputCode = "hex"
|
||||||
|
}
|
||||||
|
v, _ = inf.NewConfirmWithSelection(
|
||||||
|
confirm.WithPrompt("启用高级配置"),
|
||||||
|
).Display()
|
||||||
|
if v {
|
||||||
|
s, _ = inf.NewSingleSelect(
|
||||||
|
datas,
|
||||||
|
singleselect.WithKeyBinding(selectKeymap),
|
||||||
|
singleselect.WithPageSize(4),
|
||||||
|
singleselect.WithFilterInput(inputs),
|
||||||
|
).Display("选择数据位")
|
||||||
|
config.dataBits, _ = strconv.Atoi(datas[s])
|
||||||
|
|
||||||
|
s, _ = inf.NewSingleSelect(
|
||||||
|
stops,
|
||||||
|
singleselect.WithKeyBinding(selectKeymap),
|
||||||
|
singleselect.WithPageSize(4),
|
||||||
|
singleselect.WithFilterInput(inputs),
|
||||||
|
).Display("选择停止位")
|
||||||
|
config.stopBits = s
|
||||||
|
|
||||||
|
s, _ = inf.NewSingleSelect(
|
||||||
|
paritys,
|
||||||
|
singleselect.WithKeyBinding(selectKeymap),
|
||||||
|
singleselect.WithPageSize(4),
|
||||||
|
singleselect.WithFilterInput(inputs),
|
||||||
|
).Display("选择校验位")
|
||||||
|
config.parityBit = s
|
||||||
|
|
||||||
|
t, _ := inf.NewText(
|
||||||
|
text.WithPrompt("换行符:"),
|
||||||
|
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||||
|
text.WithDefaultValue(endStr.dv.string),
|
||||||
|
).Display()
|
||||||
|
config.endStr = t
|
||||||
|
|
||||||
|
v, _ = inf.NewConfirmWithSelection(
|
||||||
|
confirm.WithDefaultYes(),
|
||||||
|
confirm.WithPrompt("启用编码转换"),
|
||||||
|
).Display()
|
||||||
|
|
||||||
|
if v {
|
||||||
|
t, _ = inf.NewText(
|
||||||
|
text.WithPrompt("输入编码:"),
|
||||||
|
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||||
|
text.WithDefaultValue(inputCode.dv.string),
|
||||||
|
).Display()
|
||||||
|
config.inputCode = t
|
||||||
|
|
||||||
|
t, _ = inf.NewText(
|
||||||
|
text.WithPrompt("输出编码:"),
|
||||||
|
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||||
|
text.WithDefaultValue(outputCode.dv.string),
|
||||||
|
).Display()
|
||||||
|
config.outputCode = t
|
||||||
|
}
|
||||||
|
|
||||||
|
s, _ = inf.NewSingleSelect(
|
||||||
|
forwards,
|
||||||
|
singleselect.WithKeyBinding(selectKeymap),
|
||||||
|
singleselect.WithPageSize(3),
|
||||||
|
singleselect.WithFilterInput(inputs),
|
||||||
|
).Display("选择转发模式")
|
||||||
|
if s != 0 {
|
||||||
|
config.forWard = s
|
||||||
|
t, _ = inf.NewText(
|
||||||
|
text.WithPrompt("地址:"),
|
||||||
|
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||||
|
text.WithDefaultValue(address.dv.string),
|
||||||
|
).Display()
|
||||||
|
config.address = t
|
||||||
|
}
|
||||||
|
|
||||||
|
e, _ := inf.NewConfirmWithSelection(
|
||||||
|
confirm.WithDefaultYes(),
|
||||||
|
confirm.WithPrompt("启用日志"),
|
||||||
|
).Display()
|
||||||
|
config.enableLog = e
|
||||||
|
if e {
|
||||||
|
t, _ = inf.NewText(
|
||||||
|
text.WithPrompt("Path:"),
|
||||||
|
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
|
||||||
|
text.WithDefaultValue(logFilePath.dv.string),
|
||||||
|
).Display()
|
||||||
|
config.logFilePath = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
27
go.mod
27
go.mod
@@ -3,12 +3,35 @@ module COM
|
|||||||
go 1.22
|
go 1.22
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/fzdwx/infinite v0.12.1
|
||||||
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
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
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/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
|
||||||
github.com/creack/goselect v0.1.2 // indirect
|
github.com/creack/goselect v0.1.2 // indirect
|
||||||
golang.org/x/sys v0.4.0 // indirect
|
github.com/duke-git/lancet/v2 v2.2.1 // indirect
|
||||||
golang.org/x/text v0.6.0 // indirect
|
github.com/fzdwx/iter v0.0.0-20230511075109-0afee9319312 // 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/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/rotisserie/eris v0.5.4 // indirect
|
||||||
|
github.com/sahilm/fuzzy v0.1.0 // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // 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
|
||||||
)
|
)
|
||||||
|
BIN
image/img4.png
BIN
image/img4.png
Binary file not shown.
Before Width: | Height: | Size: 55 KiB After Width: | Height: | Size: 41 KiB |
BIN
image/img6.png
Normal file
BIN
image/img6.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 633 KiB |
BIN
image/img7.png
Normal file
BIN
image/img7.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 55 KiB |
79
main.go
79
main.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"go.bug.st/serial"
|
"go.bug.st/serial"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -18,6 +19,13 @@ var (
|
|||||||
args []string
|
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) {
|
func checkPortAvailability(name string) ([]string, error) {
|
||||||
ports, err := serial.GetPortsList()
|
ports, err := serial.GetPortsList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -42,13 +50,23 @@ func init() {
|
|||||||
for _, f := range flags {
|
for _, f := range flags {
|
||||||
flagInit(&f)
|
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()
|
cmdinit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func input() {
|
func input(in io.Reader) {
|
||||||
input := bufio.NewScanner(os.Stdin)
|
input := bufio.NewScanner(in)
|
||||||
var ok = false
|
var ok = false
|
||||||
for input.Scan() {
|
for {
|
||||||
|
input.Scan()
|
||||||
ok = false
|
ok = false
|
||||||
args = strings.Split(input.Text(), " ")
|
args = strings.Split(input.Text(), " ")
|
||||||
for _, cmd := range commands {
|
for _, cmd := range commands {
|
||||||
@@ -67,26 +85,29 @@ func input() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err := serialPort.Drain()
|
err = serialPort.Drain()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func strout(cs, str string) {
|
func strout(out io.Writer, cs, str string) {
|
||||||
err = charsetconv.EncodeWith(strings.NewReader(str), os.Stdout, charsetconv.Charset(cs), false)
|
err = charsetconv.EncodeWith(strings.NewReader(str), out, charsetconv.Charset(cs), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func output(out io.Writer) {
|
func output() {
|
||||||
if strings.Compare(config.inputCode, "hex") == 0 {
|
if strings.Compare(config.inputCode, "hex") == 0 {
|
||||||
b, _ := bufio.NewReader(io.LimitReader(serialPort, 16)).Peek(16)
|
b := make([]byte, 16)
|
||||||
_, err = fmt.Fprintf(out, "% X %q \n", b, b)
|
r, _ := io.LimitReader(serialPort, int64(config.frameSize)).Read(b)
|
||||||
|
if r != 0 {
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf("% X %q \n", b, b))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err = charsetconv.ConvertWith(io.LimitReader(serialPort, 1024), charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
|
err = charsetconv.ConvertWith(serialPort, charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -94,6 +115,10 @@ func output(out io.Writer) {
|
|||||||
}
|
}
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
if config.portName == "" {
|
||||||
|
getCliFlag()
|
||||||
|
}
|
||||||
ports, err := checkPortAvailability(config.portName)
|
ports, err := checkPortAvailability(config.portName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -113,23 +138,37 @@ func main() {
|
|||||||
defer func(port serial.Port) {
|
defer func(port serial.Port) {
|
||||||
err := port.Close()
|
err := port.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}(serialPort)
|
}(serialPort)
|
||||||
|
|
||||||
go input()
|
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 {
|
if config.enableLog {
|
||||||
f, err := os.OpenFile(config.logFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
f, err := os.OpenFile(config.logFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
out := io.MultiWriter(os.Stdout, f)
|
outs = append(outs, f)
|
||||||
for {
|
}
|
||||||
output(out)
|
if len(outs) != 1 {
|
||||||
}
|
out = io.MultiWriter(outs...)
|
||||||
} else {
|
}
|
||||||
for {
|
for {
|
||||||
output(os.Stdout)
|
output()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user