package test import ( "bytes" "encoding/binary" "log" "os" "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) { 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) { 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/3", Baud: 9600, ReadTimeout: 5}) if err != nil { t.Fatal(err) } for i := 0; i < 20; i++ { buf := make([]byte, 128) port1.Read(buf) log.Println(buf) time.Sleep(time.Second * 1) } // port2, err := serial.OpenPort(&serial.Config{Name: "/dev/pts/4", 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() }