-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrors.go
More file actions
54 lines (44 loc) · 1.56 KB
/
errors.go
File metadata and controls
54 lines (44 loc) · 1.56 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
package modbus
import (
"fmt"
)
// Error is a custom type for Modbus errors
type Error struct {
msg string
code uint8
}
func (err *Error) Error() string {
return err.msg
}
// Code is the Modbus code used to identify the type of modbus error
func (err *Error) Code() uint8 {
return err.code
}
// PDU Returns the error in the form of a Modbus exception response PDU
func (err *Error) asPDU(function uint8) pdu {
p := pdu{}
p.function |= 0x80
p.data = make([]uint8, 1)
p.data[0] = err.code
return p
}
// IllegalFunctionErrorF represents an invalid function code - Modbus error code 1
func IllegalFunctionErrorF(format string, args ...interface{}) *Error {
return &Error{fmt.Sprintf(format, args...), 1}
}
// IllegalAddressErrorF represents an invalid address - Modbus error code 2
func IllegalAddressErrorF(format string, args ...interface{}) *Error {
return &Error{fmt.Sprintf(format, args...), 2}
}
// IllegalValueErrorF represents an illegal data value - Modbus error code 3
func IllegalValueErrorF(format string, args ...interface{}) *Error {
return &Error{fmt.Sprintf(format, args...), 3}
}
// ServerFailureErrorF represents an error that is not represented by the above types - Modbus error code 4
func ServerFailureErrorF(format string, args ...interface{}) *Error {
return &Error{fmt.Sprintf(format, args...), 4}
}
// ServerBusyErrorF represents a condition in which the server is busy and cannot process the client request - Modbus error code 6
func ServerBusyErrorF(format string, args ...interface{}) *Error {
return &Error{fmt.Sprintf(format, args...), 6}
}