forked from robinson/gos7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime.go
51 lines (46 loc) · 1.41 KB
/
datetime.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
package gos7
// Copyright 2018 Trung Hieu Le. All rights reserved.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
import (
"encoding/binary"
"fmt"
"time"
)
//implement GetPLCDateTime
func (mb *client) PGClockWrite() (datetime time.Time, err error) {
requestData := make([]byte, len(s7GetDatetimeTelegram))
copy(requestData, s7GetDatetimeTelegram)
request := NewProtocolDataUnit(requestData)
//send
response, err := mb.send(&request)
if length := len(response.Data); length > 30 {
if (binary.BigEndian.Uint16(response.Data[27:]) == 0) && (response.Data[29] == 0xFF) {
var s7 Helper
datetime = s7.GetDateTimeAt(response.Data, 35)
} else {
err = fmt.Errorf(ErrorText(errCliInvalidPlcAnswer))
}
} else {
err = fmt.Errorf(ErrorText(errIsoInvalidPDU))
}
return
}
//implement SetPLCDateTime
func (mb *client) PGClockRead(datetime time.Time) (err error) {
requestData := make([]byte, len(s7SetDatetimeTelegram))
copy(requestData, s7SetDatetimeTelegram)
var s7 Helper
s7.SetDateTimeAt(requestData, 32, datetime)
request := NewProtocolDataUnit(requestData)
//send
response, err := mb.send(&request)
if length := len(response.Data); length > 30 {
if binary.BigEndian.Uint16(response.Data[27:]) != 0 {
err = fmt.Errorf(ErrorText(errCliInvalidPlcAnswer))
}
} else {
err = fmt.Errorf(ErrorText(errIsoInvalidPDU))
}
return
}