This commit is contained in:
2019-11-04 02:00:35 +08:00
parent 8194e385bb
commit e08af94eb6
10 changed files with 288 additions and 190 deletions

View File

@@ -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()
}

140
test/log
View File

@@ -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 []