-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathread.go
178 lines (153 loc) · 4.14 KB
/
read.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package uvr
import (
"fmt"
"github.com/brutella/can"
"github.com/brutella/canopen"
"github.com/brutella/canopen/sdo"
"time"
)
func ReadFromIndex(idx canopen.ObjectIndex, nodeID uint8, bus *can.Bus) (interface{}, error) {
upload := sdo.Upload{
ObjectIndex: idx,
RequestCobID: uint16(SSDOClientToServer2) + uint16(nodeID),
ResponseCobID: uint16(SSDOServerToClient2) + uint16(nodeID),
}
b, err := upload.Do(bus)
if err != nil {
return nil, err
}
if len(b) != 7 {
return nil, fmt.Errorf("Invalid number of received bytes")
}
// isParam := b[6]&0x80
dt := b[6] & 0x7F
dataType := dt & 0x70
switch dataType {
case 0x10: // String reference
index := parseStringIndex(b)
return ReadStringAtIndex(index, nodeID, bus)
case 0x20: // Bit field
return fmt.Sprintf("Bits %b", b[0]), nil
case 0x30:
return parseCharacter(b)
case 0x40: // 16-bit integer converted to a 32-bit float
return parseInt16(b), nil
case 0x50: // 32-bit integer converted to a 32-bit float
return parseInt32(b), nil
// index := parseUnitIndex(b)
// str, err := ReadStringAtIndex(index, nodeID, bus)
// if err != nil {
// return nil, err
// }
// return fmt.Sprintf("value %f %s", value, str), nil
default:
break
}
return nil, fmt.Errorf("Unknown data type %X", dataType)
}
func ReadStringAtIndex(idx canopen.ObjectIndex, nodeID uint8, bus *can.Bus) (string, error) {
upload := sdo.Upload{
ObjectIndex: idx,
RequestCobID: uint16(SSDOClientToServer2) + uint16(nodeID),
ResponseCobID: uint16(SSDOServerToClient2) + uint16(nodeID),
}
b, err := upload.Do(bus)
if err != nil {
return "", err
}
// b may contain invalid utf8 characters
return printableASCIIString(b), nil
}
func printableASCIIString(b []byte) string {
var ascii []byte
for _, b := range b {
if b >= 32 && b <= 126 {
ascii = append(ascii, b)
}
}
return string(ascii)
}
func parseStringIndex(b []byte) canopen.ObjectIndex {
index := canopen.ObjectIndex{}
index.Index.B0 = b[4]
index.Index.B1 = b[5]
index.SubIndex = b[0]
return index
}
func parseCharacter(b []byte) (interface{}, error) {
dt := b[6] & 0x7F
value := byte(b[0])
floatValue := float32(value)
decimal := int(b[4] & 0x1)
if decimal > 0 {
floatValue = floatValue / float32(decimal*10)
}
switch dt {
case 0x32:
if value == 0 {
return OutletStateOff, nil
}
return floatValue, nil
case 0x33:
return value, nil
case 0x34:
if value <= 25 {
return string(0x41 + value), nil
} else {
return nil, fmt.Errorf("Invalid value %d for data type %d", value, dt)
}
case 0x35:
return value * 5, nil
case 0x36:
if value <= 90 {
return time.Duration(value) * time.Second, nil
} else if value <= 107 {
return time.Duration(value-87) * 30 * time.Second, nil
} else if value <= 157 {
return time.Duration(value-97) * 60 * time.Second, nil
} else {
return time.Duration(value-155) * 1800 * time.Second, nil
}
default:
return nil, fmt.Errorf("Unsupported character data type %X", dt)
}
}
func parseUnitIndex(b []byte) canopen.ObjectIndex {
return canopen.NewObjectIndex(0x5002, uint8(b[5]))
}
// parseInt32 parses a 32-bit integer and converts it to a 32-bit float
func parseInt32(b []byte) float32 {
// Bytes
// 0: first 8 bits of int32
// 1: second 8 bits of int32
// 2: third 8 bits of int32
// 3: fourth 8 bits of int32
// 4: LSD = number of decimal places; MSB = ignored for now
// 5: subindex of unit object at index 0x5002
// 6: ?
decimal := byte(b[4] & 0x1)
value := (int32(b[3]) << 24) + (int32(b[2]) << 16) + (int32(b[1]) << 8) + int32(b[0])
floatValue := float32(value)
if decimal > 0 {
floatValue = floatValue / (float32(decimal) * 10)
}
return floatValue
}
// parseInt16 parses a 16-bit integer and converts it to a 32-bit float
func parseInt16(b []byte) float32 {
// Bytes
// 0: first 8 bits of int16
// 1: second 8 bits of int16
// 2: ?
// 3: ?
// 4: LSD = number of decimal places; MSB = ignored for now
// 5: subindex of unit object at index 0x5002
// 6: ?
decimal := byte(b[4] & 0x1)
value := (int16(b[1]) << 8) + int16(b[0])
floatValue := float32(value)
if decimal > 0 {
floatValue = floatValue / (float32(decimal) * 10)
}
return floatValue
}