forked from grid-x/modbus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtu_timing_test.go
47 lines (36 loc) · 1.1 KB
/
rtu_timing_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
package modbus
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRTUTiming(t *testing.T) {
c := rtuSerialTransporter{}
precision := 0.007 // 0.7%
imprecise := func(a, b time.Duration) bool {
return math.Abs(float64(a)/float64(b)-1) > precision
}
for _, baudRate := range []int{2400, 9600, 19200, 38400, 57600, 115200} {
t.Log(baudRate)
c.BaudRate = baudRate
charDuration := time.Duration(float64(time.Second) / float64(baudRate) * 11)
// if res := c.charDuration(); imprecise(res,charDuration) {
// assert.Equal(t, charDuration, res, "character duration")
// }
// characterDelay := charDuration * 3 / 2 // 1.5
// if baudRate > 19200 {
// characterDelay = 750 * time.Microsecond
// }
// if res := c.characterDelay(); imprecise(res,characterDelay) {
// assert.Equal(t, characterDelay, res, "character delay")
// }
frameDelay := charDuration * 7 / 2 // 3.5
if baudRate > 19200 {
frameDelay = 1750 * time.Microsecond
}
if res := c.frameDelay(); imprecise(res, frameDelay) {
assert.Equal(t, frameDelay, res, "frame delay")
}
}
}