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
#[macro_use]
extern crate enum_primitive;
extern crate num;
extern crate rustc_serialize;
extern crate bincode;
extern crate byteorder;
use std::io;
use std::str::FromStr;
use bincode::rustc_serialize as bcs;
pub mod binary;
mod client;
pub mod scoped;
pub mod tcp;
pub use tcp::Transport;
pub use tcp::Config;
pub use client::Client;
type Address = u16;
type Quantity = u16;
type Value = u16;
enum Function<'a> {
ReadCoils(Address, Quantity),
ReadDiscreteInputs(Address, Quantity),
ReadHoldingRegisters(Address, Quantity),
ReadInputRegisters(Address, Quantity),
WriteSingleCoil(Address, Value),
WriteSingleRegister(Address, Value),
WriteMultipleCoils(Address, Quantity, &'a [u8]),
WriteMultipleRegisters(Address, Quantity, &'a [u8]),
}
impl<'a> Function<'a> {
fn code(&self) -> u8 {
match *self {
Function::ReadCoils(_, _) => 0x01,
Function::ReadDiscreteInputs(_, _) => 0x02,
Function::ReadHoldingRegisters(_, _) => 0x03,
Function::ReadInputRegisters(_, _) => 0x04,
Function::WriteSingleCoil(_, _) => 0x05,
Function::WriteSingleRegister(_, _) => 0x06,
Function::WriteMultipleCoils(_, _, _) => 0x0f,
Function::WriteMultipleRegisters(_, _, _) => 0x10,
}
}
}
enum_from_primitive! {
#[derive(Debug, PartialEq)]
pub enum ExceptionCode {
IllegalFunction = 0x01,
IllegalDataAddress = 0x02,
IllegalDataValue = 0x03,
SlaveOrServerFailure = 0x04,
Acknowledge = 0x05,
SlaveOrServerBusy = 0x06,
NegativeAcknowledge = 0x07,
MemoryParity = 0x08,
NotDefined = 0x09,
GatewayPath = 0x0a,
GatewayTarget = 0x0b
}
}
#[derive(Debug)]
pub enum Reason {
UnexpectedReplySize,
BytecountNotEven,
SendBufferEmpty,
RecvBufferEmpty,
SendBufferTooBig,
DecodingError,
EncodingError,
InvalidByteorder,
}
#[derive(Debug)]
pub enum Error {
Exception(ExceptionCode),
Io(io::Error),
InvalidResponse,
InvalidData(Reason),
InvalidFunction,
ParseCoilError,
}
impl From<ExceptionCode> for Error {
fn from(err: ExceptionCode) -> Error {
Error::Exception(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
Error::Io(err)
}
}
impl From<bcs::DecodingError> for Error {
fn from(_err: bcs::DecodingError) -> Error {
Error::InvalidData(Reason::DecodingError)
}
}
impl From<bcs::EncodingError> for Error {
fn from(_err: bcs::EncodingError) -> Error {
Error::InvalidData(Reason::EncodingError)
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Coil {
On,
Off,
}
impl Coil {
fn code(&self) -> u16 {
match *self {
Coil::On => 0xff00,
Coil::Off => 0x0000,
}
}
}
impl FromStr for Coil {
type Err = Error;
fn from_str(s: &str) -> Result<Coil> {
if s == "On" {
Ok(Coil::On)
} else if s == "Off" {
Ok(Coil::Off)
} else {
Err(Error::ParseCoilError)
}
}
}