diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a82bb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +DPF +log diff --git a/main.go b/main.go index 206fc28..e802af4 100644 --- a/main.go +++ b/main.go @@ -2,16 +2,34 @@ package main import ( "log" + "os" "github.com/gin-gonic/gin" ) +func (worker *Worker) OperateHandler(cxt *gin.Context) { + log.Println(worker.Sensor()) +} + +func (worker *Worker) StatusHandler(cxt *gin.Context) { + +} + func main() { + f, err := os.OpenFile("./log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) + if err != nil { + panic(err) + } + log.SetOutput(f) engine := gin.Default() - engine.GET("/status") - engine.POST("/operate") + worker := NewWorker() + + engine.GET("/status", worker.StatusHandler) + engine.POST("/operate", worker.OperateHandler) + + go worker.Run() log.Fatal(engine.Run(":15678")) diff --git a/operator.go b/operator.go new file mode 100644 index 0000000..29ad2d3 --- /dev/null +++ b/operator.go @@ -0,0 +1,110 @@ +package main + +import "time" + +// OperatorFlag 操作位 +type OperatorFlag uint16 + +const ( + UltrasonicPower OperatorFlag = 0b1000000000000000 // UltrasonicPower bit15 超声波电源开关 1开,0关 + // CYZ-A-X + CirculatingIrrigation OperatorFlag = 0b0100000000000000 // CirculatingIrrigation bit14 循环灌洗水泵 1开,0关 + UFRecoil OperatorFlag = 0b0010000000000000 // bit13 UF 超滤膜反冲进水阀 1开,0关 + UFPositive OperatorFlag = 0b0001000000000000 // bit12 UF 超滤膜正冲进水阀 1开,0关 + // YM-01-X-03 + UFTreatedWater OperatorFlag = 0b0000100000000000 // bit11 UF 超滤膜净水出水阀 1开,0关 + UFRawWater OperatorFlag = 0b0000010000000000 // bit10 UF超滤膜原水进水阀 1开,0关 + // YM-01-X-01 + CirculatingTankWashWater OperatorFlag = 0b0000001000000000 // bit9 循环罐洗进水电动球阀 1开,0关 + UFPositiveFlushingWaterOutlet OperatorFlag = 0b0000000100000000 // bit8 UF超滤膜正冲浓水出口电磁阀 1开,0关 + // YV-02-02-1-X-06 + CleaningTankExhaust OperatorFlag = 0b0000000010000000 // bit7 清洗罐排气电磁阀 1开,0关 + DPFCompactCylinderControlB OperatorFlag = 0b0000000001000000 // bit6 DPF压紧气缸控制电磁阀B 1开,0关 + DPFCompactCylinderControlA OperatorFlag = 0b0000000000100000 // bit5 DPF压紧气缸控制电磁阀A 1开,0关 + // YV-02-05-1-X-04 + CleaningTankDrainingWater OperatorFlag = 0b0000000000010000 // bit4 清洗罐放水阀控制电磁阀 1开,0关 + GasExplosion OperatorFlag = 0b0000000000001000 // bit3 气爆阀控制电磁阀 1开,0关 + // YV-02-02-1-X-02 + CleaningTankInflation OperatorFlag = 0b0000000000000100 // bit2 清洗罐充气电磁阀 1开,0关 + CleaningTankSealB OperatorFlag = 0b0000000000000010 // bit1 清洗罐密封圈充气电磁阀B 1开,0关 + CleaningTankSealA OperatorFlag = 0b0000000000000001 // bit0 清洗罐密封圈充气电磁阀A 1开,0关 +) + +// ChaoShengBoQingXi +func ChaoShengBoQingXi(worker *Worker) { + for sensor := worker.Sensor(); (20.0/1024.0)*float64(sensor.SP02) < 10.0; sensor = worker.Sensor() { // TODO: 预设值未知 + + } + + flag := CleaningTankExhaust | + CleaningTankDrainingWater | + CirculatingTankWashWater | + UFTreatedWater | + CirculatingIrrigation + + worker.Write(OperatorOption(flag)) + + time.Sleep(time.Second * 10) // 10秒后 + + flag ^= CleaningTankDrainingWater + worker.Write(OperatorOption(flag)) + + // 满水位传感器LT-01 >= 4.5电压 + for sensor := worker.Sensor(); ((5.0 / 1024.0) * float64(sensor.LT01)) < 4.5; sensor = worker.Sensor() { + + } + + flag ^= CleaningTankExhaust + flag ^= CirculatingIrrigation + flag |= UltrasonicPower + worker.Write(OperatorOption(flag)) + + //TODO: 设置时间 + time.Sleep(time.Second * 1) + + flag ^= UltrasonicPower + worker.Write(OperatorOption(flag)) + + // 灌洗开始 + flag |= CleaningTankDrainingWater + flag |= CirculatingIrrigation + worker.Write(OperatorOption(flag)) + + for { + if time.Now().Unix() >= 15000230 { + break + } + time.Sleep(time.Millisecond * 100) + } + + flag ^= CirculatingIrrigation + worker.Write(OperatorOption(flag)) + + QingXiGuanFangShui(worker) +} + +// QingXiGuanFangShui 清洗灌放水 +func QingXiGuanFangShui(worker *Worker) { + // 满水位传感器LT-01 >= 4.5电压 + flag := CleaningTankDrainingWater | + CirculatingTankWashWater | + UFTreatedWater | + CleaningTankInflation + + for sensor := worker.Sensor(); ((5.0 / 1024.0) * float64(sensor.LT02)) >= 0.5; sensor = worker.Sensor() { + worker.Write(OperatorOption(flag)) + time.Sleep(time.Second) + } + + flag = 0 + worker.Write(OperatorOption(flag)) + + flag = CleaningTankExhaust + for sensor := worker.Sensor(); ((5.0 / 1024.0) * float64(sensor.SP01)) > 0.5; sensor = worker.Sensor() { + worker.Write(OperatorOption(flag)) + time.Sleep(time.Second) + } + + flag = 0 + worker.Write(OperatorOption(flag)) +} diff --git a/sensor.go b/sensor.go new file mode 100644 index 0000000..994eece --- /dev/null +++ b/sensor.go @@ -0,0 +1,38 @@ +package main + +import ( + "encoding/binary" + "log" +) + +// Sensor 传感器 +type Sensor struct { + SP01 uint16 // 清洗罐气压传感器 SP-01 罐体排空,值≤0.5(暂定) + SP02 uint16 // 清洗液水箱水位传感器 SP-02 模拟量传感器 + LT01 uint16 // 清洗罐满水位传感器 (非接触式水位传感器) LT-01 模拟量传感器 + LT02 uint16 // 清洗罐排水传感器 (非接触式水位传感器) LT-02 模拟量传感器 + LT03 uint8 // 清洗液水箱满水位传感器 (浮子式水位传感器) LT-03 值为1,清洗液水箱满水位 +} + +func NewSensor(buf []byte) *Sensor { + + if len(buf) == 14 { + sensor := &Sensor{} + + if buf[0] == byte(0xaa) && buf[1] == byte(0x55) { + + sensor.SP01 = binary.BigEndian.Uint16(buf[2:4]) + sensor.SP02 = binary.BigEndian.Uint16(buf[4:6]) + sensor.LT01 = binary.BigEndian.Uint16(buf[6:8]) + sensor.LT02 = binary.BigEndian.Uint16(buf[8:10]) + sensor.LT03 = uint8(buf[10]) + + return sensor + } + + } + + log.Println(buf, "非标准传感器字节流") + + return nil +} diff --git a/serialport.go b/serialport.go index 43da296..c4aeb91 100644 --- a/serialport.go +++ b/serialport.go @@ -32,8 +32,8 @@ func NewSerialPort() *SerialPort { sp.com = "COM1" - sp.port1 = "/dev/pts/11" - sp.port2 = "/dev/pts/12" + sp.port1 = "/dev/pts/3" + sp.port2 = "/dev/pts/4" sp.baud = 9600 @@ -50,23 +50,25 @@ func (sp *SerialPort) OpenPort() { cfg1 := &serial.Config{Name: sp.port1, Baud: sp.baud, ReadTimeout: 5} port1, err := serial.OpenPort(cfg1) if err != nil { - sp.linuxRPort = port1 + panic(err) } + sp.linuxRPort = port1 cfg2 := &serial.Config{Name: sp.port2, Baud: sp.baud, ReadTimeout: 5} port2, err := serial.OpenPort(cfg2) if err != nil { - sp.linuxWPort = port2 + panic(err) } + sp.linuxWPort = port2 } else { cfg1 := &serial.Config{Name: sp.com, Baud: sp.baud, ReadTimeout: 5} port1, err := serial.OpenPort(cfg1) if err != nil { - sp.windowsRWPort = port1 + panic(err) } - + sp.windowsRWPort = port1 } } diff --git a/test/com_test.go b/test/com_test.go index 1e13264..5da18b4 100644 --- a/test/com_test.go +++ b/test/com_test.go @@ -1,9 +1,10 @@ package test import ( + "bytes" + "encoding/binary" "log" "os" - "sync" "testing" "time" @@ -32,7 +33,32 @@ const ( ) func TestBit(t *testing.T) { - t.Error(CleaningTankSealA) + buf := &bytes.Buffer{} + binary.Write(buf, binary.BigEndian, byte(0xaa)) + binary.Write(buf, binary.BigEndian, byte(0x55)) + // binary.Write(buf, binary.BigEndian, 12) + t.Error(buf, buf.Bytes(), buf.Len()) +} + +func TestSendCommand(t *testing.T) { + var buf []byte = make([]byte, 8, 8) + buf[0] = byte(0xaa) + buf[1] = byte(0x55) + flag := CleaningTankExhaust | CleaningTankDrainingWater | CirculatingTankWashWater | UFTreatedWater | CirculatingIrrigation + binary.BigEndian.PutUint16(buf[2:], uint16(flag)) + + t.Errorf("%.16b", flag) + flag ^= CirculatingIrrigation + t.Errorf("%.16b", flag) + + check := byte(0) + for _, b := range buf { + check += b + } + + buf[7] = check + t.Error(buf) + } func TestLinuxSerial(t *testing.T) { @@ -43,40 +69,47 @@ func TestLinuxSerial(t *testing.T) { } log.SetOutput(f) - port1, err := serial.OpenPort(&serial.Config{Name: "/dev/pts/13", Baud: 9600, ReadTimeout: 5}) + port1, err := serial.OpenPort(&serial.Config{Name: "/dev/pts/3", 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) + for i := 0; i < 20; i++ { + var buf []byte + port1.Read(buf) + log.Println(buf) + time.Sleep(time.Second * 1) } - wait := &sync.WaitGroup{} - wait.Add(1) - go func(w *sync.WaitGroup) { - defer w.Done() - for { + // port2, err := serial.OpenPort(&serial.Config{Name: "/dev/pts/4", Baud: 9600, ReadTimeout: 5}) + // if err != nil { + // t.Fatal(err) + // } - var buf []byte - port1.Read(buf) - log.Println("read ...", buf) - time.Sleep(time.Millisecond * 500) + // wait := &sync.WaitGroup{} + // wait.Add(1) + // go func(w *sync.WaitGroup) { + // defer w.Done() + // for { - } - }(wait) + // var buf []byte + // port1.Read(buf) + // log.Println("read ...", buf) + // time.Sleep(time.Millisecond * 500) - wait.Add(1) - go func(w *sync.WaitGroup) { - defer w.Done() - for i := 0; i < 100; i++ { + // } + // }(wait) - port2.Write([]byte("write")) - log.Println("write ...", i) - time.Sleep(time.Millisecond * 500) - } - }(wait) + // wait.Add(1) + // go func(w *sync.WaitGroup) { + // defer w.Done() + // for i := 0; i < 100; i++ { - wait.Wait() + // port2.Write([]byte("write")) + // log.Println("write ...", i) + // time.Sleep(time.Millisecond * 500) + // } + // }(wait) + + // wait.Wait() } diff --git a/test/log b/test/log index 62f8662..494c0d6 100644 --- a/test/log +++ b/test/log @@ -1,120 +1,20 @@ -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 ... [] +2019/11/04 01:59:43 [] +2019/11/04 01:59:44 [] +2019/11/04 01:59:45 [] +2019/11/04 01:59:46 [] +2019/11/04 01:59:47 [] +2019/11/04 01:59:48 [] +2019/11/04 01:59:49 [] +2019/11/04 01:59:50 [] +2019/11/04 01:59:51 [] +2019/11/04 01:59:52 [] +2019/11/04 01:59:53 [] +2019/11/04 01:59:54 [] +2019/11/04 01:59:55 [] +2019/11/04 01:59:56 [] +2019/11/04 01:59:57 [] +2019/11/04 01:59:58 [] +2019/11/04 01:59:59 [] +2019/11/04 02:00:00 [] +2019/11/04 02:00:01 [] +2019/11/04 02:00:02 [] diff --git a/worker.go b/worker.go index 2e6d6c1..e88d8f3 100644 --- a/worker.go +++ b/worker.go @@ -1,7 +1,7 @@ package main import ( - "bytes" + "encoding/binary" "log" "sync" "time" @@ -10,36 +10,13 @@ import ( // 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)) - } + Register = make(map[string]func(worker *Worker)) + + Register["超声波清洗"] = ChaoShengBoQingXi + + Register["清洗罐放水"] = QingXiGuanFangShui } // Worker 接收命令 @@ -60,6 +37,19 @@ type Worker struct { waitGroup *sync.WaitGroup } +func OperatorOption(flag OperatorFlag) []byte { + var buf []byte = make([]byte, 8, 8) + buf[0] = byte(0xaa) + buf[1] = byte(0x55) + binary.BigEndian.PutUint16(buf[2:], uint16(flag)) + check := byte(0) + for _, b := range buf { + check += b + } + buf[7] = check + return buf +} + // Log 日志 type Log struct { Data []byte @@ -86,7 +76,12 @@ func NewWorker() *Worker { return w } -func (worker *Worker) write(data []byte) { +// Sensor 当前传感器状态 +func (worker *Worker) Sensor() *Sensor { + return NewSensor(worker.Read()) +} + +func (worker *Worker) Write(data []byte) { worker.writelogsLock.Lock() worker.isOperating = true @@ -98,8 +93,8 @@ func (worker *Worker) write(data []byte) { } -// read 如果没有读到数据为nil -func (worker *Worker) read() (result []byte) { +// Read 如果没有读到数据为nil +func (worker *Worker) Read() (result []byte) { worker.readlogsLock.Lock() if len(worker.readlogs) > 0 { @@ -120,7 +115,6 @@ func (worker *Worker) operator(wait *sync.WaitGroup) { if worker.isStop > 0 { break } - now := time.Now() worker.commandLock.Lock() @@ -160,9 +154,8 @@ func (worker *Worker) status(wait *sync.WaitGroup) { if err != nil { log.Println(err) } else { - + log.Println(n) if n > 0 { - worker.readlogsLock.Lock() worker.readlogs = append(worker.readlogs, Log{buf, time.Now()}) if len(worker.readlogs) >= 1000 { @@ -225,7 +218,9 @@ func (worker *Worker) Run() { worker.port = sp worker.waitGroup.Add(1) + go worker.status(worker.waitGroup) + go worker.operator(worker.waitGroup) worker.waitGroup.Wait() } diff --git a/清洗灌放水子流程.png b/清洗灌放水子流程.png new file mode 100644 index 0000000..907c904 Binary files /dev/null and b/清洗灌放水子流程.png differ diff --git a/超声波.png b/超声波.png new file mode 100644 index 0000000..b1f6dc5 Binary files /dev/null and b/超声波.png differ