-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.go
More file actions
72 lines (56 loc) · 1.81 KB
/
example.go
File metadata and controls
72 lines (56 loc) · 1.81 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/nblair2/go-dnp3/dnp3"
)
func main() {
// DNP3 Application Response G2 V2 - 2 byte prefix, 1 byte value, 6 byte abs time
input := []byte{
0x05, 0x64, 0x2a, 0x44, 0x01, 0x00, 0x00, 0x04,
0xe5, 0x79, 0xc1, 0xe2, 0x81, 0x90, 0x00, 0x02,
0x02, 0x28, 0x03, 0x00, 0x00, 0x00, 0x81, 0xda,
0x33, 0xd2, 0xdf, 0xe5, 0x64, 0x71, 0x01, 0x00,
0x00, 0x01, 0xda, 0x33, 0xd2, 0x64, 0x71, 0x01,
0xff, 0xff, 0x81, 0xdb, 0xdd, 0x14, 0x33, 0xd2,
0x64, 0x71, 0x01, 0x38, 0x5d,
}
// Parse the input bytes into a DNP3 Frame
frame := dnp3.Frame{}
if err := frame.FromBytes(input); err != nil {
log.Fatalf("Failed to parse frame: %v", err)
}
// Display with String() method
fmt.Println("--- Before (String) ---")
fmt.Println(frame.String())
// Change data
data := frame.Application.GetData()
point := data.Objects[0].Points[0].(*dnp3.PointBytes)
if err := point.SetIndex(0x0201); err != nil {
log.Fatalf("Failed to set index: %v", err)
}
if err := point.SetValue([]byte{0xFF}); err != nil {
log.Fatalf("Failed to set value: %v", err)
}
timestamp := time.Date(2010, time.July, 1, 0, 0, 0, 0, time.UTC)
absTime := dnp3.AbsoluteTime(timestamp)
if err := point.SetAbsTime(absTime); err != nil {
log.Fatalf("Failed to set absolute time: %v", err)
}
data.Objects[0].Points[0] = point
frame.Application.SetData(data)
// Convert back to bytes (forces CRC recalculation)
output, err := frame.ToBytes()
if err != nil {
log.Fatalf("Failed to convert frame to bytes: %v", err)
}
// Display as JSON
fmt.Println("--- After (json) ---")
jsonOutput, _ := json.MarshalIndent(frame, "", " ")
fmt.Println(string(jsonOutput))
// Display as bytes
fmt.Println("--- Compare ([]byte) ---")
fmt.Printf("INPUT : 0x % X\nOUTPUT: 0x % X\n", input, output)
}