TODO: 字节写入

This commit is contained in:
huangsimin 2019-11-02 15:48:16 +08:00
parent 5515ece4f0
commit 8194e385bb
5 changed files with 288 additions and 26 deletions

View File

@ -8,8 +8,8 @@ type Command struct {
commandTime time.Time
}
// NewCommand 生成一个命令
func NewCommand() *Command {
// CreateCommand 生成一个命令
func CreateCommand() *Command {
c := &Command{}
return c
}

View File

@ -7,10 +7,12 @@ import (
)
func main() {
engine := gin.Default()
engine.GET("/status")
engine.POST("/operate")
log.Fatal(engine.Run(":15678"))
}

82
test/com_test.go Normal file
View File

@ -0,0 +1,82 @@
package test
import (
"log"
"os"
"sync"
"testing"
"time"
serial "github.com/tarm/goserial"
)
type OperatorFlag uint16
const (
UltrasonicPower OperatorFlag = 0b1000000000000000 // bit15 超声波电源开关 1开0关
CirculatingIrrigation OperatorFlag = 0b0100000000000000 // bit14 循环灌洗水泵 1开0关
UFRecoil OperatorFlag = 0b0010000000000000 // bit13 UF 超滤膜反冲进水阀 1开0关
UFPositive OperatorFlag = 0b0001000000000000 // bit12 UF 超滤膜正冲进水阀 1开0关
UFTreatedWater OperatorFlag = 0b0000100000000000 // bit11 UF 超滤膜净水出水阀 1开0关
UFRawWater OperatorFlag = 0b0000010000000000 // bit10 UF超滤膜原水进水阀 1开0关
CirculatingTankWashWater OperatorFlag = 0b0000001000000000 // bit9 循环罐洗进水电动球阀 1开0关
UFPositiveFlushingWaterOutlet OperatorFlag = 0b0000000100000000 // bit8 UF超滤膜正冲浓水出口电磁阀 1开0关
CleaningTankExhaust OperatorFlag = 0b0000000010000000 // bit7 清洗罐排气电磁阀 1开0关
DPFCompactCylinderControlB OperatorFlag = 0b0000000001000000 // bit6 DPF压紧气缸控制电磁阀B 1开0关
DPFCompactCylinderControlA OperatorFlag = 0b0000000000100000 // bit5 DPF压紧气缸控制电磁阀A 1开0关
CleaningTankDrainingWater OperatorFlag = 0b0000000000010000 // bit4 清洗罐放水阀控制电磁阀 1开0关
GasExplosion OperatorFlag = 0b0000000000001000 // bit3 气爆阀控制电磁阀 1开0关
CleaningTankInflation OperatorFlag = 0b0000000000000100 // bit2 清洗罐充气电磁阀 1开0关
CleaningTankSealB OperatorFlag = 0b0000000000000010 // bit1 清洗罐密封圈充气电磁阀B 1开0关
CleaningTankSealA OperatorFlag = 0b0000000000000001 // bit0 清洗罐密封圈充气电磁阀A 1开0关
)
func TestBit(t *testing.T) {
t.Error(CleaningTankSealA)
}
func TestLinuxSerial(t *testing.T) {
f, err := os.OpenFile("./log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
log.SetOutput(f)
port1, err := serial.OpenPort(&serial.Config{Name: "/dev/pts/13", Baud: 9600, ReadTimeout: 5})
if err != nil {
t.Fatal(err)
}
port2, err := serial.OpenPort(&serial.Config{Name: "/dev/pts/14", Baud: 9600, ReadTimeout: 5})
if err != nil {
t.Fatal(err)
}
wait := &sync.WaitGroup{}
wait.Add(1)
go func(w *sync.WaitGroup) {
defer w.Done()
for {
var buf []byte
port1.Read(buf)
log.Println("read ...", buf)
time.Sleep(time.Millisecond * 500)
}
}(wait)
wait.Add(1)
go func(w *sync.WaitGroup) {
defer w.Done()
for i := 0; i < 100; i++ {
port2.Write([]byte("write"))
log.Println("write ...", i)
time.Sleep(time.Millisecond * 500)
}
}(wait)
wait.Wait()
}

120
test/log Normal file
View File

@ -0,0 +1,120 @@
2019/11/02 12:42:34 write ... 0
2019/11/02 12:42:34 read ... []
2019/11/02 12:42:34 write ... 1
2019/11/02 12:42:34 read ... []
2019/11/02 12:42:35 write ... 2
2019/11/02 12:42:35 read ... []
2019/11/02 12:42:35 write ... 3
2019/11/02 12:42:35 read ... []
2019/11/02 12:42:36 write ... 4
2019/11/02 12:42:36 read ... []
2019/11/02 12:42:36 write ... 5
2019/11/02 12:42:36 read ... []
2019/11/02 12:42:37 write ... 6
2019/11/02 12:42:37 read ... []
2019/11/02 12:42:37 read ... []
2019/11/02 12:42:37 write ... 7
2019/11/02 12:42:38 write ... 8
2019/11/02 12:42:38 read ... []
2019/11/02 12:42:38 read ... []
2019/11/02 12:42:38 write ... 9
2019/11/02 12:42:39 write ... 10
2019/11/02 12:42:39 read ... []
2019/11/02 12:42:39 write ... 11
2019/11/02 12:42:39 read ... []
2019/11/02 12:42:40 read ... []
2019/11/02 12:42:40 write ... 12
2019/11/02 12:42:40 write ... 13
2019/11/02 12:42:40 read ... []
2019/11/02 12:42:41 write ... 14
2019/11/02 12:42:41 read ... []
2019/11/02 12:42:41 read ... []
2019/11/02 12:42:41 write ... 15
2019/11/02 12:42:42 write ... 16
2019/11/02 12:42:42 read ... []
2019/11/02 12:42:42 write ... 17
2019/11/02 12:42:42 read ... []
2019/11/02 12:42:43 write ... 18
2019/11/02 12:42:43 read ... []
2019/11/02 12:42:43 write ... 19
2019/11/02 12:42:43 read ... []
2019/11/02 12:42:44 write ... 20
2019/11/02 12:42:44 read ... []
2019/11/02 12:42:44 read ... []
2019/11/02 12:42:44 write ... 21
2019/11/02 12:42:45 read ... []
2019/11/02 12:42:45 write ... 22
2019/11/02 12:42:45 write ... 23
2019/11/02 12:42:45 read ... []
2019/11/02 12:42:46 write ... 24
2019/11/02 12:42:46 read ... []
2019/11/02 12:42:46 write ... 25
2019/11/02 12:42:46 read ... []
2019/11/02 12:42:47 write ... 26
2019/11/02 12:42:47 read ... []
2019/11/02 12:42:47 read ... []
2019/11/02 12:42:47 write ... 27
2019/11/02 12:42:48 read ... []
2019/11/02 12:42:48 write ... 28
2019/11/02 12:42:48 read ... []
2019/11/02 12:42:48 write ... 29
2019/11/02 12:42:49 read ... []
2019/11/02 12:42:49 write ... 30
2019/11/02 12:42:49 read ... []
2019/11/02 12:42:49 write ... 31
2019/11/02 12:42:50 read ... []
2019/11/02 12:42:50 write ... 32
2019/11/02 12:42:50 read ... []
2019/11/02 12:42:50 write ... 33
2019/11/02 12:42:51 read ... []
2019/11/02 12:42:51 write ... 34
2019/11/02 12:42:51 read ... []
2019/11/02 12:42:51 write ... 35
2019/11/02 12:42:52 read ... []
2019/11/02 12:42:52 write ... 36
2019/11/02 12:42:52 read ... []
2019/11/02 12:42:52 write ... 37
2019/11/02 12:42:53 read ... []
2019/11/02 12:42:53 write ... 38
2019/11/02 12:42:53 write ... 39
2019/11/02 12:42:53 read ... []
2019/11/02 12:42:54 write ... 40
2019/11/02 12:42:54 read ... []
2019/11/02 12:42:54 write ... 41
2019/11/02 12:42:54 read ... []
2019/11/02 12:42:55 read ... []
2019/11/02 12:42:55 write ... 42
2019/11/02 12:42:55 read ... []
2019/11/02 12:42:55 write ... 43
2019/11/02 12:42:56 write ... 44
2019/11/02 12:42:56 read ... []
2019/11/02 12:42:56 write ... 45
2019/11/02 12:42:56 read ... []
2019/11/02 12:42:57 write ... 46
2019/11/02 12:42:57 read ... []
2019/11/02 12:42:57 read ... []
2019/11/02 12:42:57 write ... 47
2019/11/02 12:42:58 read ... []
2019/11/02 12:42:58 write ... 48
2019/11/02 12:42:46 write ... 49
2019/11/02 12:42:46 read ... []
2019/11/02 12:42:46 write ... 50
2019/11/02 12:42:46 read ... []
2019/11/02 12:42:47 write ... 51
2019/11/02 12:42:47 read ... []
2019/11/02 12:42:47 write ... 52
2019/11/02 12:42:47 read ... []
2019/11/02 12:42:48 write ... 53
2019/11/02 12:42:48 read ... []
2019/11/02 12:42:48 write ... 54
2019/11/02 12:42:48 read ... []
2019/11/02 12:42:49 write ... 55
2019/11/02 12:42:49 read ... []
2019/11/02 12:42:49 write ... 56
2019/11/02 12:42:49 read ... []
2019/11/02 12:42:50 write ... 57
2019/11/02 12:42:50 read ... []
2019/11/02 12:42:50 write ... 58
2019/11/02 12:42:50 read ... []
2019/11/02 12:42:51 write ... 59
2019/11/02 12:42:51 read ... []

106
worker.go
View File

@ -1,11 +1,47 @@
package main
import (
"bytes"
"log"
"sync"
"time"
)
// Register 操作注册表
var Register map[string]func(worker *Worker)
// OperatorFlag 训练
type OperatorFlag uint16
const (
// UltrasonicPower bit15 超声波电源开关 1开0关
UltrasonicPower OperatorFlag = 0b1000000000000000
// CirculatingIrrigation bit14 循环灌洗水泵 1开0关
CirculatingIrrigation OperatorFlag = 0b0100000000000000
UFRecoil OperatorFlag = 0b0010000000000000 // bit13 UF 超滤膜反冲进水阀 1开0关
UFPositive OperatorFlag = 0b0001000000000000 // bit12 UF 超滤膜正冲进水阀 1开0关
UFTreatedWater OperatorFlag = 0b0000100000000000 // bit11 UF 超滤膜净水出水阀 1开0关
UFRawWater OperatorFlag = 0b0000010000000000 // bit10 UF超滤膜原水进水阀 1开0关
CirculatingTankWashWater OperatorFlag = 0b0000001000000000 // bit9 循环罐洗进水电动球阀 1开0关
UFPositiveFlushingWaterOutlet OperatorFlag = 0b0000000100000000 // bit8 UF超滤膜正冲浓水出口电磁阀 1开0关
CleaningTankExhaust OperatorFlag = 0b0000000010000000 // bit7 清洗罐排气电磁阀 1开0关
DPFCompactCylinderControlB OperatorFlag = 0b0000000001000000 // bit6 DPF压紧气缸控制电磁阀B 1开0关
DPFCompactCylinderControlA OperatorFlag = 0b0000000000100000 // bit5 DPF压紧气缸控制电磁阀A 1开0关
CleaningTankDrainingWater OperatorFlag = 0b0000000000010000 // bit4 清洗罐放水阀控制电磁阀 1开0关
GasExplosion OperatorFlag = 0b0000000000001000 // bit3 气爆阀控制电磁阀 1开0关
CleaningTankInflation OperatorFlag = 0b0000000000000100 // bit2 清洗罐充气电磁阀 1开0关
CleaningTankSealB OperatorFlag = 0b0000000000000010 // bit1 清洗罐密封圈充气电磁阀B 1开0关
CleaningTankSealA OperatorFlag = 0b0000000000000001 // bit0 清洗罐密封圈充气电磁阀A 1开0关
)
// init 初始化
func init() {
Register["干洗"] = func(worker *Worker) {
buf := &bytes.Buffer{}
buf.Write([]byte(0xaa))
}
}
// Worker 接收命令
type Worker struct {
commandLock *sync.Mutex
@ -37,7 +73,7 @@ func NewWorker() *Worker {
w.isStop = 0
w.waitGroup = &sync.WaitGroup{}
w.command = NewCommand()
w.command = CreateCommand()
w.commandLock = new(sync.Mutex)
w.readlogsLock = &sync.Mutex{}
@ -51,6 +87,7 @@ func NewWorker() *Worker {
}
func (worker *Worker) write(data []byte) {
worker.writelogsLock.Lock()
worker.isOperating = true
worker.writelogs = append(worker.writelogs, Log{data, time.Now()})
@ -58,6 +95,7 @@ func (worker *Worker) write(data []byte) {
worker.writelogs = worker.writelogs[500:1000]
}
worker.writelogsLock.Unlock()
}
// read 如果没有读到数据为nil
@ -84,16 +122,20 @@ func (worker *Worker) operator(wait *sync.WaitGroup) {
}
now := time.Now()
worker.writelogsLock.Lock()
worker.commandLock.Lock()
if worker.isOperating {
l := worker.writelogs[len(worker.writelogs)-1]
if now.Sub(l.Time).Seconds() >= 5 {
if now.Sub(worker.command.commandTime).Seconds() >= 5 {
// TODO: 操作
if operate, ok := Register[worker.command.commands]; ok {
operate(worker)
}
worker.isOperating = false
}
}
worker.writelogsLock.Unlock()
worker.commandLock.Unlock()
}
} else { // Windows
@ -118,6 +160,7 @@ func (worker *Worker) status(wait *sync.WaitGroup) {
if err != nil {
log.Println(err)
} else {
if n > 0 {
worker.readlogsLock.Lock()
@ -126,31 +169,46 @@ func (worker *Worker) status(wait *sync.WaitGroup) {
worker.readlogs = worker.readlogs[500:1000]
}
log.Println("data size ", n)
log.Panicln("data: ", buf)
worker.readlogsLock.Unlock()
}
}
time.Sleep(time.Millisecond * 50)
}
} else { // windows
for {
if worker.isStop > 0 {
break
}
var buf []byte
n, err := worker.port.windowsRWPort.Read(buf)
if err != nil {
log.Println(err)
} else {
if n > 0 {
log.Println("data size ", n)
log.Panicln("data: ", buf)
worker.readlogsLock.Lock()
worker.readlogs = append(worker.readlogs, Log{buf, time.Now()})
if len(worker.readlogs) >= 1000 {
worker.readlogs = worker.readlogs[500:1000]
}
log.Println("data size ", n)
log.Panicln("data: ", buf)
worker.readlogsLock.Unlock()
}
}
time.Sleep(time.Millisecond * 50)
}
} else { // windows
for {
var buf []byte
n, err := worker.port.windowsRWPort.Read(buf)
if err != nil {
log.Println(err)
} else {
if n > 0 {
log.Println("data size ", n)
log.Panicln("data: ", buf)
}
}
time.Sleep(time.Millisecond * 50)