Compare commits
3 Commits
5bf90d1b63
...
604e5bb4ad
Author | SHA1 | Date |
---|---|---|
|
604e5bb4ad | |
|
e3415ae05a | |
|
d19c09e4cd |
|
@ -10,10 +10,10 @@
|
|||
* [x] 双向编码转换
|
||||
* [x] 活动端口探测
|
||||
* [x] 数据日志保存
|
||||
* [ ] 自动断帧设置
|
||||
* [x] Hex断帧设置
|
||||
* [x] UDP数据转发
|
||||
* [x] TCP数据转发
|
||||
* [ ] 文件接收发送
|
||||
* [ ] UDP数据转发
|
||||
* [ ] TCP数据转发
|
||||
|
||||
## 运行示例
|
||||
|
||||
|
|
10
command.go
10
command.go
|
@ -18,7 +18,7 @@ var commands []Command
|
|||
|
||||
func cmdhelp() {
|
||||
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++ {
|
||||
strout(out, config.outputCode, fmt.Sprintf(" %-10v --%v\n", commands[i].name, commands[i].description))
|
||||
}
|
||||
|
@ -27,12 +27,12 @@ func cmdexit() {
|
|||
os.Exit(0)
|
||||
}
|
||||
func cmdargs() {
|
||||
fmt.Printf(">-------Args(%v)-------<\n", len(args)-1)
|
||||
fmt.Printf("%q\n", args[1:])
|
||||
strout(out, config.outputCode, fmt.Sprintf(">-------Args(%v)-------<\n", len(args)-1))
|
||||
strout(out, config.outputCode, fmt.Sprintf("%q\n", args[1:]))
|
||||
}
|
||||
func cmdhex() {
|
||||
fmt.Printf(">-----Hex Send-----<\n")
|
||||
fmt.Printf("%q\n", args[1:])
|
||||
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 {
|
||||
|
|
|
@ -17,31 +17,28 @@ type Config struct {
|
|||
enableLog bool
|
||||
logFilePath string
|
||||
forWard int
|
||||
frameSize int
|
||||
address string
|
||||
}
|
||||
type FoeWardMode int
|
||||
|
||||
const (
|
||||
NOT FoeWardMode = iota
|
||||
TCPS
|
||||
TCPC
|
||||
UDPS
|
||||
UDPC
|
||||
)
|
||||
|
||||
var config Config
|
||||
|
||||
func setForWard() (conn net.Conn) {
|
||||
func setForWardClient() (conn net.Conn) {
|
||||
switch FoeWardMode(config.forWard) {
|
||||
case TCPS:
|
||||
case NOT:
|
||||
|
||||
case TCPC:
|
||||
conn, err = net.Dial("tcp", config.address)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
case UDPS:
|
||||
|
||||
case UDPC:
|
||||
conn, err = net.Dial("udp", config.address)
|
||||
if err != nil {
|
||||
|
|
5
flag.go
5
flag.go
|
@ -38,10 +38,11 @@ var (
|
|||
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-S 2:TCP-C 3:UDP-S 4: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"}, "转发服务地址"}
|
||||
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, parityBit}
|
||||
flags = []Flag{portName, baudRate, dataBits, stopBits, outputCode, inputCode, endStr, enableLog, logFilePath, forWard, address, frameSize, parityBit}
|
||||
)
|
||||
|
||||
type ValType int
|
||||
|
|
31
main.go
31
main.go
|
@ -8,6 +8,7 @@ import (
|
|||
"go.bug.st/serial"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
@ -55,7 +56,8 @@ func init() {
|
|||
func input(in io.Reader) {
|
||||
input := bufio.NewScanner(in)
|
||||
var ok = false
|
||||
for input.Scan() {
|
||||
for {
|
||||
input.Scan()
|
||||
ok = false
|
||||
args = strings.Split(input.Text(), " ")
|
||||
for _, cmd := range commands {
|
||||
|
@ -74,7 +76,7 @@ func input(in io.Reader) {
|
|||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
err := serialPort.Drain()
|
||||
err = serialPort.Drain()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -88,10 +90,10 @@ func strout(out io.Writer, cs, str string) {
|
|||
}
|
||||
}
|
||||
|
||||
func output(out io.Writer) {
|
||||
func output() {
|
||||
if strings.Compare(config.inputCode, "hex") == 0 {
|
||||
b := make([]byte, 16)
|
||||
r, _ := io.LimitReader(serialPort, 16).Read(b)
|
||||
r, _ := io.LimitReader(serialPort, int64(config.frameSize)).Read(b)
|
||||
if r != 0 {
|
||||
strout(out, config.outputCode, fmt.Sprintf("% X %q \n", b, b))
|
||||
}
|
||||
|
@ -123,21 +125,26 @@ func main() {
|
|||
defer func(port serial.Port) {
|
||||
err := port.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}(serialPort)
|
||||
|
||||
if FoeWardMode(config.forWard) != NOT {
|
||||
conn := setForWard()
|
||||
conn := setForWardClient()
|
||||
ins = append(ins, conn)
|
||||
outs = append(outs, conn)
|
||||
defer conn.Close()
|
||||
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 len(ins) != 1 {
|
||||
in = io.MultiReader(ins...)
|
||||
}
|
||||
go input(in)
|
||||
|
||||
if config.enableLog {
|
||||
f, err := os.OpenFile(config.logFilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
|
@ -149,6 +156,6 @@ func main() {
|
|||
out = io.MultiWriter(outs...)
|
||||
}
|
||||
for {
|
||||
output(out)
|
||||
output()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue