mirror of
https://github.com/jixishi/SerialTerminalForWindowsTerminal.git
synced 2025-09-17 06:52:20 +00:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
604e5bb4ad | ||
|
e3415ae05a | ||
|
d19c09e4cd | ||
|
5bf90d1b63 | ||
|
dffb269247 | ||
|
d450a8a019 | ||
|
8b1e5bfb06 | ||
|
437f309ab9 | ||
|
1ca7e23911 | ||
|
a4004a1d05 | ||
|
0f2fe50b04 | ||
|
03b3cf3793 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,4 @@
|
|||||||
/build/
|
/build/
|
||||||
|
.idea
|
||||||
|
dist/
|
||||||
|
/go.sum
|
||||||
|
52
.goreleaser.yaml
Normal file
52
.goreleaser.yaml
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#file: noinspection YAMLSchemaValidation
|
||||||
|
# This is an example .goreleaser.yml file with some sensible defaults.
|
||||||
|
# Make sure to check the documentation at https://goreleaser.com
|
||||||
|
|
||||||
|
# The lines below are called `modelines`. See `:help modeline`
|
||||||
|
# Feel free to remove those if you don't want/need to use them.
|
||||||
|
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
||||||
|
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||||
|
|
||||||
|
version: 1
|
||||||
|
|
||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
# You may remove this if you don't use go modules.
|
||||||
|
# - go mod tidy
|
||||||
|
# you may remove this if you don't need go generate
|
||||||
|
# - go generate ./...
|
||||||
|
|
||||||
|
builds:
|
||||||
|
- env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- windows
|
||||||
|
- darwin
|
||||||
|
|
||||||
|
archives:
|
||||||
|
- format: tar.gz
|
||||||
|
# this name template makes the OS and Arch compatible with the results of `uname`.
|
||||||
|
name_template: >-
|
||||||
|
{{ .ProjectName }}_
|
||||||
|
{{- title .Os }}_
|
||||||
|
{{- if eq .Arch "amd64" }}x86_64
|
||||||
|
{{- else if eq .Arch "386" }}i386
|
||||||
|
{{- else }}{{ .Arch }}{{ end }}
|
||||||
|
{{- if .Arm }}v{{ .Arm }}{{ end }}
|
||||||
|
# use zip for windows archives
|
||||||
|
format_overrides:
|
||||||
|
- goos: windows
|
||||||
|
format: zip
|
||||||
|
checksum:
|
||||||
|
name_template: 'checksums.txt'
|
||||||
|
|
||||||
|
snapshot:
|
||||||
|
name_template: 'v1.0.0-snapshot'
|
||||||
|
|
||||||
|
changelog:
|
||||||
|
sort: asc
|
||||||
|
filters:
|
||||||
|
exclude:
|
||||||
|
- "^docs:"
|
||||||
|
- "^test:"
|
29
README.md
29
README.md
@@ -1,10 +1,19 @@
|
|||||||
# 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数据转发
|
||||||
|
* [ ] 文件接收发送
|
||||||
|
|
||||||
## 运行示例
|
## 运行示例
|
||||||
|
|
||||||
@@ -15,3 +24,13 @@
|
|||||||
2. 输入设备输出UTF8 终端输出GBK `./COM -p COM8 -b 115200 -o GBK`
|
2. 输入设备输出UTF8 终端输出GBK `./COM -p COM8 -b 115200 -o GBK`
|
||||||
|
|
||||||

|

|
||||||
|
3. 彩色终端输出
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
4. Hex接收 `./COM -p COM8 -b 115200 -i hex`
|
||||||
|
|
||||||
|

|
||||||
|
5. Hex发送 `./COM -p COM8 -b 115200`
|
||||||
|
|
||||||
|

|
||||||
|
51
command.go
Normal file
51
command.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Command struct {
|
||||||
|
name string
|
||||||
|
description string
|
||||||
|
function func()
|
||||||
|
}
|
||||||
|
|
||||||
|
var commands []Command
|
||||||
|
|
||||||
|
func cmdhelp() {
|
||||||
|
var page = 0
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf(">-------Help(%v)-------<\n", page))
|
||||||
|
for i := 0; i < len(commands); i++ {
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf(" %-10v --%v\n", commands[i].name, commands[i].description))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func cmdexit() {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
func cmdargs() {
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf(">-------Args(%v)-------<\n", len(args)-1))
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf("%q\n", args[1:]))
|
||||||
|
}
|
||||||
|
func cmdhex() {
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf(">-----Hex Send-----<\n"))
|
||||||
|
strout(out, config.outputCode, fmt.Sprintf("%q\n", args[1:]))
|
||||||
|
s := strings.Join(args[1:], "")
|
||||||
|
b, err := hex.DecodeString(s)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
_, err = serialPort.Write(b)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func cmdinit() {
|
||||||
|
commands = append(commands, Command{name: ".help", description: "帮助信息", function: cmdhelp})
|
||||||
|
commands = append(commands, Command{name: ".args", description: "参数信息", function: cmdargs})
|
||||||
|
commands = append(commands, Command{name: ".hex", description: "发送Hex", function: cmdhex})
|
||||||
|
commands = append(commands, Command{name: ".exit", description: "退出终端", function: cmdexit})
|
||||||
|
}
|
51
config.go
Normal file
51
config.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
portName string
|
||||||
|
baudRate int
|
||||||
|
dataBits int
|
||||||
|
stopBits int
|
||||||
|
parityBit int
|
||||||
|
outputCode string
|
||||||
|
inputCode string
|
||||||
|
endStr string
|
||||||
|
enableLog bool
|
||||||
|
logFilePath string
|
||||||
|
forWard int
|
||||||
|
frameSize int
|
||||||
|
address string
|
||||||
|
}
|
||||||
|
type FoeWardMode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
NOT FoeWardMode = iota
|
||||||
|
TCPC
|
||||||
|
UDPC
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
101
flag.go
Normal file
101
flag.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ptrVal struct {
|
||||||
|
*string
|
||||||
|
*int
|
||||||
|
*bool
|
||||||
|
*float64
|
||||||
|
*float32
|
||||||
|
}
|
||||||
|
type Val struct {
|
||||||
|
string
|
||||||
|
int
|
||||||
|
bool
|
||||||
|
float64
|
||||||
|
float32
|
||||||
|
}
|
||||||
|
type Flag struct {
|
||||||
|
v ptrVal
|
||||||
|
sStr string
|
||||||
|
lStr string
|
||||||
|
dv Val
|
||||||
|
help string
|
||||||
|
}
|
||||||
|
|
||||||
|
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}
|
||||||
|
)
|
||||||
|
|
||||||
|
type ValType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
notVal ValType = iota
|
||||||
|
stringVal
|
||||||
|
intVal
|
||||||
|
boolVal
|
||||||
|
)
|
||||||
|
|
||||||
|
func printUsage(ports []string) {
|
||||||
|
fmt.Printf("\n参数帮助:\n")
|
||||||
|
for _, f := range flags {
|
||||||
|
flagprint(f)
|
||||||
|
}
|
||||||
|
fmt.Printf("\n在线串口: %v\n", strings.Join(ports, ","))
|
||||||
|
}
|
||||||
|
func flagFindValue(v ptrVal) ValType {
|
||||||
|
if v.string != nil {
|
||||||
|
return stringVal
|
||||||
|
}
|
||||||
|
if v.bool != nil {
|
||||||
|
return boolVal
|
||||||
|
}
|
||||||
|
if v.int != nil {
|
||||||
|
return intVal
|
||||||
|
}
|
||||||
|
return notVal
|
||||||
|
}
|
||||||
|
func flagprint(f Flag) {
|
||||||
|
switch flagFindValue(f.v) {
|
||||||
|
case stringVal:
|
||||||
|
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%q\n", f.sStr, f.lStr, f.dv.string, f.help, f.dv.string)
|
||||||
|
case intVal:
|
||||||
|
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)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
BIN
image/img3.png
Normal file
BIN
image/img3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
BIN
image/img4.png
Normal file
BIN
image/img4.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
BIN
image/img5.png
Normal file
BIN
image/img5.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
190
main.go
190
main.go
@@ -2,142 +2,63 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/hex"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/zimolab/charsetconv"
|
"github.com/zimolab/charsetconv"
|
||||||
"go.bug.st/serial"
|
"go.bug.st/serial"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
enableLog bool
|
|
||||||
logFilePath string
|
|
||||||
portName string
|
|
||||||
endStr string
|
|
||||||
inputCode string
|
|
||||||
outputCode string
|
|
||||||
baudRate int
|
|
||||||
parityBit int
|
|
||||||
stopBits int
|
|
||||||
dataBits int
|
|
||||||
}
|
|
||||||
type Command struct {
|
|
||||||
name string
|
|
||||||
description string
|
|
||||||
function func()
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
config Config
|
|
||||||
commands []Command
|
|
||||||
serialPort serial.Port
|
serialPort serial.Port
|
||||||
err error
|
err error
|
||||||
args []string
|
args []string
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkPortAvailability() {
|
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()
|
ports, err := serial.GetPortsList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(ports) == 0 {
|
if len(ports) == 0 {
|
||||||
log.Fatal("未找到串口!")
|
return nil, fmt.Errorf("无串口")
|
||||||
|
}
|
||||||
|
if name == "" {
|
||||||
|
return ports, fmt.Errorf("串口未指定")
|
||||||
}
|
}
|
||||||
fmt.Printf("找到的串口: ")
|
|
||||||
for _, port := range ports {
|
for _, port := range ports {
|
||||||
fmt.Printf(" %v", port)
|
if strings.Compare(port, name) == 0 {
|
||||||
|
return ports, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return ports, fmt.Errorf("串口 " + name + " 未在线")
|
||||||
}
|
}
|
||||||
func printUsage() {
|
|
||||||
checkPortAvailability()
|
|
||||||
fmt.Printf("\n参数帮助:\n")
|
|
||||||
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "p", "portName", "", "连接的串口(/dev/ttyUSB0、COMx)", "")
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "b", "baudRate", 115200, "波特率", 115200)
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "d", "data", 8, "数据位", 8)
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "s", "stop", 0, "停止位停止位(0: 1停止 1:1.5停止 2:2停止)", 0)
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "o", "out", "UTF-8", "输出编码", "UTF-8")
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "i", "in", "UTF-8", "输入编码", "UTF-8")
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "e", "end", "\n", "终端换行符", "\\n")
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "l", "log", false, "是否启用日志保存", false)
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "P", "Path", "./Log.txt", "日志保存路径", "./Log.txt")
|
|
||||||
fmt.Printf("\t-%v -%v %T \n\t %v\t默认值:%v\n", "v", "verify", 0, "奇偶校验(0:无校验、1:奇校验、2:偶校验、3:1校验、4:0校验)", 0)
|
|
||||||
}
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile | log.Lmsgprefix)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile | log.Lmsgprefix)
|
||||||
flag.BoolVar(&config.enableLog, "log", false, "是否启用日志保存")
|
for _, f := range flags {
|
||||||
flag.BoolVar(&config.enableLog, "l", false, "")
|
flagInit(&f)
|
||||||
|
}
|
||||||
flag.StringVar(&config.logFilePath, "Path", "./Log.txt", "日志保存路径")
|
|
||||||
flag.StringVar(&config.logFilePath, "P", "./Log.txt", "")
|
|
||||||
|
|
||||||
flag.StringVar(&config.portName, "port", "", "连接的串口\t(/dev/ttyUSB0、COMx)")
|
|
||||||
flag.StringVar(&config.portName, "p", "", "")
|
|
||||||
|
|
||||||
flag.StringVar(&config.endStr, "end", "\n", "终端换行符")
|
|
||||||
flag.StringVar(&config.endStr, "e", "\n", "")
|
|
||||||
|
|
||||||
flag.IntVar(&config.baudRate, "baud", 115200, "波特率")
|
|
||||||
flag.IntVar(&config.baudRate, "b", 115200, "")
|
|
||||||
|
|
||||||
flag.IntVar(&config.parityBit, "verify", 0, "奇偶校验(0:无校验 1:奇校验 2:偶校验 3:1校验 4:0校验)")
|
|
||||||
flag.IntVar(&config.parityBit, "v", 0, "")
|
|
||||||
|
|
||||||
flag.IntVar(&config.stopBits, "stop", 0, "停止位(0: 1停止 1:1.5停止 2:2停止)")
|
|
||||||
flag.IntVar(&config.stopBits, "s", 0, "")
|
|
||||||
|
|
||||||
flag.IntVar(&config.dataBits, "data", 8, "数据位")
|
|
||||||
flag.IntVar(&config.dataBits, "d", 8, "")
|
|
||||||
|
|
||||||
flag.StringVar(&config.outputCode, "out", "UTF-8", "输出编码")
|
|
||||||
flag.StringVar(&config.outputCode, "o", "UTF-8", "")
|
|
||||||
|
|
||||||
flag.StringVar(&config.inputCode, "in", "UTF-8", "输入编码")
|
|
||||||
flag.StringVar(&config.inputCode, "i", "UTF-8", "")
|
|
||||||
cmdinit()
|
cmdinit()
|
||||||
}
|
}
|
||||||
func cmdhelp() {
|
|
||||||
var page = 0
|
func input(in io.Reader) {
|
||||||
fmt.Printf(">-------Help(%v)-------<\n", page)
|
input := bufio.NewScanner(in)
|
||||||
for i := 0; i < len(commands); i++ {
|
|
||||||
strout(config.outputCode, fmt.Sprintf(" %-10v --%v\n", commands[i].name, commands[i].description))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func cmdexit() {
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
func cmdargs() {
|
|
||||||
fmt.Printf(">-------Args(%v)-------<\n", len(args)-1)
|
|
||||||
fmt.Printf("%q\n", args[1:])
|
|
||||||
}
|
|
||||||
func cmdhex() {
|
|
||||||
fmt.Printf(">-----Hex Send-----<\n")
|
|
||||||
fmt.Printf("%q\n", args[1:])
|
|
||||||
s := strings.Join(args[1:], "")
|
|
||||||
b, err := hex.DecodeString(s)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
_, err = serialPort.Write(b)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func cmdinit() {
|
|
||||||
commands = append(commands, Command{name: ".help", description: "帮助信息", function: cmdhelp})
|
|
||||||
commands = append(commands, Command{name: ".args", description: "参数信息", function: cmdargs})
|
|
||||||
commands = append(commands, Command{name: ".hex", description: "发送Hex", function: cmdhex})
|
|
||||||
commands = append(commands, Command{name: ".exit", description: "退出终端", function: cmdexit})
|
|
||||||
}
|
|
||||||
func input() {
|
|
||||||
input := bufio.NewScanner(os.Stdin)
|
|
||||||
var ok = false
|
var ok = false
|
||||||
for {
|
for {
|
||||||
input.Scan()
|
input.Scan()
|
||||||
|
ok = false
|
||||||
args = strings.Split(input.Text(), " ")
|
args = strings.Split(input.Text(), " ")
|
||||||
for _, cmd := range commands {
|
for _, cmd := range commands {
|
||||||
if strings.Compare(strings.TrimSpace(args[0]), cmd.name) == 0 {
|
if strings.Compare(strings.TrimSpace(args[0]), cmd.name) == 0 {
|
||||||
@@ -146,27 +67,36 @@ func input() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
_, err := serialPort.Write(input.Bytes())
|
_, err := io.WriteString(serialPort, input.Text())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
_, err = io.WriteString(serialPort, config.endStr)
|
_, err = io.WriteString(serialPort, config.endStr)
|
||||||
//_, err = io.Copy(portName, os.Stdin)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
err = serialPort.Drain()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func strout(cs, str string) {
|
|
||||||
err = charsetconv.EncodeWith(strings.NewReader(str), os.Stdout, charsetconv.Charset(cs), false)
|
func strout(out io.Writer, cs, str string) {
|
||||||
|
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 {
|
||||||
_, err = io.Copy(hex.NewEncoder(out), serialPort)
|
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 {
|
} else {
|
||||||
err = charsetconv.ConvertWith(serialPort, charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
|
err = charsetconv.ConvertWith(serialPort, charsetconv.Charset(config.inputCode), out, charsetconv.Charset(config.outputCode), false)
|
||||||
}
|
}
|
||||||
@@ -176,9 +106,10 @@ func output(out io.Writer) {
|
|||||||
}
|
}
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if config.portName == "" {
|
ports, err := checkPortAvailability(config.portName)
|
||||||
fmt.Println("端口未指定")
|
if err != nil {
|
||||||
printUsage()
|
fmt.Println(err)
|
||||||
|
printUsage(ports)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
mode := &serial.Mode{
|
mode := &serial.Mode{
|
||||||
@@ -194,22 +125,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