forked from xiegeo/modbusone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_rtu_test.go
82 lines (74 loc) · 1.77 KB
/
serial_rtu_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package modbusone_test
import (
"encoding/hex"
"fmt"
"testing"
"time"
. "github.com/xiegeo/modbusone"
)
func TestRTU(t *testing.T) {
testCases := []RTU{
// from http://www.simplymodbus.ca/FC16.htm
RTU([]byte{0x11, 0x10, 0x00, 0x01, 0x00, 0x02, 0x04, 0x00, 0x0A, 0x01, 0x02, 0xC6, 0xF0}),
RTU([]byte{0x11, 0x10, 0x00, 0x01, 0x00, 0x02, 0x12, 0x98}),
}
failCases := []RTU{
RTU([]byte{}), // too short
RTU([]byte{0x02, 0x12, 0x98}), // too short
RTU([]byte{0xf1, 0x10, 0x00, 0x01, 0x00, 0x02, 0x12, 0x98}), // crc error
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%v:%v", i, hex.EncodeToString(tc)), func(t *testing.T) {
p, err := tc.GetPDU()
if err != nil {
t.Fatal(err)
}
t.Log("PDU:", hex.EncodeToString(p))
/*ec := p.Validate()
if ec != EcOK {
t.Fatal("error code", ec)
}*/
})
}
for i, tc := range failCases {
t.Run(fmt.Sprintf("ShouldFail%v:%v", i, hex.EncodeToString(tc)), func(t *testing.T) {
_, err := tc.GetPDU()
if err == nil {
t.Fatal("Expected error here")
}
t.Log("expected err:", err)
})
}
}
func TestRTUDelay(t *testing.T) {
testCases := []struct {
bw int64
delay time.Duration
}{
{1, 35 * 11 * time.Second / 10},
{19200, 2005209},
{38400, 1750000},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Delay for bw:%v", tc.bw), func(t *testing.T) {
d := MinDelay(tc.bw)
if d != tc.delay {
t.Fatalf("expected:%v got:%v", tc.delay, d)
}
})
}
}
func TestMaxPerPacketSized(t *testing.T) {
for fc := FunctionCode(1); fc < 0xf0; fc++ {
if !fc.Valid() {
continue
}
t.Run(fmt.Sprintf("fc:%v", fc), func(t *testing.T) {
r := fc.MaxPerPacket()
s := fc.MaxPerPacketSized(MaxPDUSize)
if r != s {
t.Fatalf("expected:%v got:%v", r, s)
}
})
}
}