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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
// SPDX-FileCopyrightText: Copyright (c) 2017-2024 slowtec GmbH <post@slowtec.de>
// SPDX-License-Identifier: MIT OR Apache-2.0
#[cfg(feature = "rtu")]
pub(crate) mod rtu;
#[cfg(feature = "tcp")]
pub(crate) mod tcp;
use std::{
borrow::Cow,
error,
fmt::{self, Display},
};
use crate::bytes::Bytes;
/// A Modbus function code.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FunctionCode {
/// Modbus Function Code: `01` (`0x01`).
ReadCoils,
/// Modbus Function Code: `02` (`0x02`).
ReadDiscreteInputs,
/// Modbus Function Code: `05` (`0x05`).
WriteSingleCoil,
/// Modbus Function Code: `06` (`0x06`).
WriteSingleRegister,
/// Modbus Function Code: `03` (`0x03`).
ReadHoldingRegisters,
/// Modbus Function Code: `04` (`0x04`).
ReadInputRegisters,
/// Modbus Function Code: `15` (`0x0F`).
WriteMultipleCoils,
/// Modbus Function Code: `16` (`0x10`).
WriteMultipleRegisters,
/// Modbus Function Code: `22` (`0x16`).
MaskWriteRegister,
/// Modbus Function Code: `23` (`0x17`).
ReadWriteMultipleRegisters,
/// Custom Modbus Function Code.
Custom(u8),
Disconnect,
}
impl FunctionCode {
/// Create a new [`FunctionCode`] with `value`.
#[must_use]
pub const fn new(value: u8) -> Self {
match value {
0x01 => Self::ReadCoils,
0x02 => Self::ReadDiscreteInputs,
0x05 => Self::WriteSingleCoil,
0x06 => Self::WriteSingleRegister,
0x03 => Self::ReadHoldingRegisters,
0x04 => Self::ReadInputRegisters,
0x0F => Self::WriteMultipleCoils,
0x10 => Self::WriteMultipleRegisters,
0x16 => Self::MaskWriteRegister,
0x17 => Self::ReadWriteMultipleRegisters,
code => Self::Custom(code),
}
}
/// Get the [`u8`] value of the current [`FunctionCode`].
///
/// # Panics
///
/// Panics on [`Disconnect`](Self::Disconnect) which has no corresponding Modbus function code.
#[must_use]
pub const fn value(self) -> u8 {
match self {
Self::ReadCoils => 0x01,
Self::ReadDiscreteInputs => 0x02,
Self::WriteSingleCoil => 0x05,
Self::WriteSingleRegister => 0x06,
Self::ReadHoldingRegisters => 0x03,
Self::ReadInputRegisters => 0x04,
Self::WriteMultipleCoils => 0x0F,
Self::WriteMultipleRegisters => 0x10,
Self::MaskWriteRegister => 0x16,
Self::ReadWriteMultipleRegisters => 0x17,
Self::Custom(code) => code,
Self::Disconnect => unreachable!(),
}
}
}
impl Display for FunctionCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value().fmt(f)
}
}
/// A Modbus protocol address is represented by 16 bit from `0` to `65535`.
///
/// This *protocol address* uses 0-based indexing, while the *coil address* or
/// *register address* is often specified as a number with 1-based indexing.
/// Please consult the specification of your devices if 1-based coil/register
/// addresses need to be converted to 0-based protocol addresses by subtracting 1.
pub type Address = u16;
/// A Coil represents a single bit.
///
/// - `true` is equivalent to `ON`, `1` and `0xFF00`.
/// - `false` is equivalent to `OFF`, `0` and `0x0000`.
pub(crate) type Coil = bool;
/// Modbus uses 16 bit for its data items.
///
/// Transmitted using a big-endian representation.
pub(crate) type Word = u16;
/// Number of items to process.
pub type Quantity = u16;
/// A request represents a message from the client (master) to the server (slave).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Request<'a> {
/// A request to read multiple coils.
/// The first parameter is the address of the first coil to read.
/// The second parameter is the number of coils to read.
ReadCoils(Address, Quantity),
/// A request to read multiple discrete inputs
/// The first parameter is the address of the first discrete input to read.
/// The second parameter is the number of discrete inputs to read.
ReadDiscreteInputs(Address, Quantity),
/// A request to write a single coil.
/// The first parameter is the address of the coil.
/// The second parameter is the value to write to the coil.
WriteSingleCoil(Address, Coil),
/// A request to write multiple coils.
/// The first parameter is the address of the first coil to write.
/// The second parameter is the vector of values to write to the coils.
WriteMultipleCoils(Address, Cow<'a, [Coil]>),
/// A request to read multiple input registers.
/// The first parameter is the address of the first input register to read.
/// The second parameter is the number of input registers to read.
ReadInputRegisters(Address, Quantity),
/// A request to read multiple holding registers.
/// The first parameter is the address of the first holding register to read.
/// The second parameter is the number of holding registers to read.
ReadHoldingRegisters(Address, Quantity),
/// A request to write a single register.
/// The first parameter is the address of the register to read.
/// The second parameter is the value to write to the register.
WriteSingleRegister(Address, Word),
/// A request to write to multiple registers.
/// The first parameter is the address of the first register to write.
/// The second parameter is the vector of values to write to the registers.
WriteMultipleRegisters(Address, Cow<'a, [Word]>),
/// A request to set or clear individual bits of a holding register.
/// The first parameter is the address of the holding register.
/// The second parameter is the AND mask.
/// The third parameter is the OR mask.
MaskWriteRegister(Address, Word, Word),
/// A request to simultaneously read multiple registers and write multiple registers.
/// The first parameter is the address of the first register to read.
/// The second parameter is the number of registers to read.
/// The third parameter is the address of the first register to write.
/// The fourth parameter is the vector of values to write to the registers.
ReadWriteMultipleRegisters(Address, Quantity, Address, Cow<'a, [Word]>),
/// A raw Modbus request.
/// The first parameter is the Modbus function code.
/// The second parameter is the raw bytes of the request.
Custom(u8, Cow<'a, [u8]>),
/// A poison pill for stopping the client service and to release
/// the underlying transport, e.g. for disconnecting from an
/// exclusively used serial port.
///
/// This is an ugly workaround, because `tokio-proto` does not
/// provide other means to gracefully shut down the `ClientService`.
/// Otherwise the bound transport is never freed as long as the
/// executor is active, even when dropping the Modbus client
/// context.
Disconnect,
}
impl<'a> Request<'a> {
/// Converts the request into an owned instance with `'static'` lifetime.
#[must_use]
pub fn into_owned(self) -> Request<'static> {
use Request::*;
match self {
ReadCoils(addr, qty) => ReadCoils(addr, qty),
ReadDiscreteInputs(addr, qty) => ReadDiscreteInputs(addr, qty),
WriteSingleCoil(addr, coil) => WriteSingleCoil(addr, coil),
WriteMultipleCoils(addr, coils) => {
WriteMultipleCoils(addr, Cow::Owned(coils.into_owned()))
}
ReadInputRegisters(addr, qty) => ReadInputRegisters(addr, qty),
ReadHoldingRegisters(addr, qty) => ReadHoldingRegisters(addr, qty),
WriteSingleRegister(addr, word) => WriteSingleRegister(addr, word),
WriteMultipleRegisters(addr, words) => {
WriteMultipleRegisters(addr, Cow::Owned(words.into_owned()))
}
MaskWriteRegister(addr, and_mask, or_mask) => {
MaskWriteRegister(addr, and_mask, or_mask)
}
ReadWriteMultipleRegisters(addr, qty, write_addr, words) => {
ReadWriteMultipleRegisters(addr, qty, write_addr, Cow::Owned(words.into_owned()))
}
Custom(func, bytes) => Custom(func, Cow::Owned(bytes.into_owned())),
Disconnect => Disconnect,
}
}
/// Get the [`FunctionCode`] of the [`Request`].
#[must_use]
pub const fn function_code(&self) -> FunctionCode {
use Request::*;
match self {
ReadCoils(_, _) => FunctionCode::ReadCoils,
ReadDiscreteInputs(_, _) => FunctionCode::ReadDiscreteInputs,
WriteSingleCoil(_, _) => FunctionCode::WriteSingleCoil,
WriteMultipleCoils(_, _) => FunctionCode::WriteMultipleCoils,
ReadInputRegisters(_, _) => FunctionCode::ReadInputRegisters,
ReadHoldingRegisters(_, _) => FunctionCode::ReadHoldingRegisters,
WriteSingleRegister(_, _) => FunctionCode::WriteSingleRegister,
WriteMultipleRegisters(_, _) => FunctionCode::WriteMultipleRegisters,
MaskWriteRegister(_, _, _) => FunctionCode::MaskWriteRegister,
ReadWriteMultipleRegisters(_, _, _, _) => FunctionCode::ReadWriteMultipleRegisters,
Custom(code, _) => FunctionCode::Custom(*code),
Disconnect => unreachable!(),
}
}
}
/// A Modbus request with slave included
#[cfg(feature = "server")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SlaveRequest<'a> {
/// Slave Id from the request
pub slave: crate::slave::SlaveId,
/// A `Request` enum
pub request: Request<'a>,
}
#[cfg(feature = "server")]
impl<'a> SlaveRequest<'a> {
/// Converts the request into an owned instance with `'static'` lifetime.
#[must_use]
pub fn into_owned(self) -> SlaveRequest<'static> {
let Self { slave, request } = self;
SlaveRequest {
slave,
request: request.into_owned(),
}
}
}
/// The data of a successful request.
///
/// ReadCoils/ReadDiscreteInputs: The length of the result Vec is always a
/// multiple of 8. Only the values of the first bits/coils that have actually
/// been requested are defined. The value of the remaining bits depend on the
/// server implementation and those coils should be should be ignored.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Response {
/// Response to a `ReadCoils` request
/// The parameter contains the coil values that have been read
/// See also the note above regarding the vector length
ReadCoils(Vec<Coil>),
/// Response to a `ReadDiscreteInputs` request
/// The parameter contains the discrete input values that have been read
/// See also the note above regarding the vector length
ReadDiscreteInputs(Vec<Coil>),
/// Response to a `WriteSingleCoil` request
/// The first parameter contains the address of the coil that has been written to
/// The second parameter contains the value that has been written to the coil the given address
WriteSingleCoil(Address, Coil),
/// Response to a `WriteMultipleCoils` request
/// The first parameter contains the address at the start of the range that has been written to
/// The second parameter contains the amount of values that have been written
WriteMultipleCoils(Address, Quantity),
/// Response to a `ReadInputRegisters` request
/// The parameter contains the register values that have been read
ReadInputRegisters(Vec<Word>),
/// Response to a `ReadHoldingRegisters` request
/// The parameter contains the register values that have been read
ReadHoldingRegisters(Vec<Word>),
/// Response to a `WriteSingleRegister` request
/// The first parameter contains the address of the register that has been written to
/// The second parameter contains the value that has been written to the register at the given address
WriteSingleRegister(Address, Word),
/// Response to a `WriteMultipleRegisters` request
/// The first parameter contains the address at the start of the register range that has been written to
/// The second parameter contains the amount of register that have been written
WriteMultipleRegisters(Address, Quantity),
/// Response `MaskWriteRegister`
/// The first parameter is the address of the holding register.
/// The second parameter is the AND mask.
/// The third parameter is the OR mask.
MaskWriteRegister(Address, Word, Word),
/// Response to a `ReadWriteMultipleRegisters` request
/// The parameter contains the register values that have been read as part of the read instruction
ReadWriteMultipleRegisters(Vec<Word>),
/// Response to a raw Modbus request
/// The first parameter contains the returned Modbus function code
/// The second parameter contains the bytes read following the function code
Custom(u8, Bytes),
}
impl Response {
/// Get the [`FunctionCode`] of the [`Response`].
#[must_use]
pub const fn function_code(&self) -> FunctionCode {
use Response::*;
match self {
ReadCoils(_) => FunctionCode::ReadCoils,
ReadDiscreteInputs(_) => FunctionCode::ReadDiscreteInputs,
WriteSingleCoil(_, _) => FunctionCode::WriteSingleCoil,
WriteMultipleCoils(_, _) => FunctionCode::WriteMultipleCoils,
ReadInputRegisters(_) => FunctionCode::ReadInputRegisters,
ReadHoldingRegisters(_) => FunctionCode::ReadHoldingRegisters,
WriteSingleRegister(_, _) => FunctionCode::WriteSingleRegister,
WriteMultipleRegisters(_, _) => FunctionCode::WriteMultipleRegisters,
MaskWriteRegister(_, _, _) => FunctionCode::MaskWriteRegister,
ReadWriteMultipleRegisters(_) => FunctionCode::ReadWriteMultipleRegisters,
Custom(code, _) => FunctionCode::Custom(*code),
}
}
}
/// A server (slave) exception.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Exception {
IllegalFunction = 0x01,
IllegalDataAddress = 0x02,
IllegalDataValue = 0x03,
ServerDeviceFailure = 0x04,
Acknowledge = 0x05,
ServerDeviceBusy = 0x06,
MemoryParityError = 0x08,
GatewayPathUnavailable = 0x0A,
GatewayTargetDevice = 0x0B,
}
impl From<Exception> for u8 {
fn from(from: Exception) -> Self {
from as u8
}
}
impl Exception {
pub(crate) fn description(&self) -> &str {
use crate::frame::Exception::*;
match *self {
IllegalFunction => "Illegal function",
IllegalDataAddress => "Illegal data address",
IllegalDataValue => "Illegal data value",
ServerDeviceFailure => "Server device failure",
Acknowledge => "Acknowledge",
ServerDeviceBusy => "Server device busy",
MemoryParityError => "Memory parity error",
GatewayPathUnavailable => "Gateway path unavailable",
GatewayTargetDevice => "Gateway target device failed to respond",
}
}
}
/// A server (slave) exception response.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExceptionResponse {
pub function: FunctionCode,
pub exception: Exception,
}
/// Represents a message from the client (slave) to the server (master).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RequestPdu<'a>(pub(crate) Request<'a>);
impl<'a> From<Request<'a>> for RequestPdu<'a> {
fn from(from: Request<'a>) -> Self {
RequestPdu(from)
}
}
impl<'a> From<RequestPdu<'a>> for Request<'a> {
fn from(from: RequestPdu<'a>) -> Self {
from.0
}
}
/// Represents a message from the server (slave) to the client (master).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ResponsePdu(pub(crate) Result<Response, ExceptionResponse>);
impl From<Response> for ResponsePdu {
fn from(from: Response) -> Self {
ResponsePdu(Ok(from))
}
}
impl From<ExceptionResponse> for ResponsePdu {
fn from(from: ExceptionResponse) -> Self {
ResponsePdu(Err(from))
}
}
#[cfg(any(
feature = "rtu-over-tcp-server",
feature = "rtu-server",
feature = "tcp-server"
))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct OptionalResponsePdu(pub(crate) Option<ResponsePdu>);
#[cfg(any(
feature = "rtu-over-tcp-server",
feature = "rtu-server",
feature = "tcp-server"
))]
impl From<Result<Option<Response>, ExceptionResponse>> for OptionalResponsePdu {
fn from(from: Result<Option<Response>, ExceptionResponse>) -> Self {
match from {
Ok(None) => Self(None),
Ok(Some(response)) => Self(Some(response.into())),
Err(exception) => Self(Some(exception.into())),
}
}
}
impl From<ResponsePdu> for Result<Response, ExceptionResponse> {
fn from(from: ResponsePdu) -> Self {
from.0
}
}
impl fmt::Display for Exception {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl error::Error for Exception {
fn description(&self) -> &str {
self.description()
}
}
impl fmt::Display for ExceptionResponse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Modbus function {}: {}", self.function, self.exception)
}
}
impl error::Error for ExceptionResponse {
fn description(&self) -> &str {
self.exception.description()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_function_code() {
assert_eq!(FunctionCode::ReadCoils, FunctionCode::new(0x01));
assert_eq!(FunctionCode::ReadDiscreteInputs, FunctionCode::new(0x02));
assert_eq!(FunctionCode::WriteSingleCoil, FunctionCode::new(0x05));
assert_eq!(FunctionCode::WriteSingleRegister, FunctionCode::new(0x06));
assert_eq!(FunctionCode::ReadHoldingRegisters, FunctionCode::new(0x03));
assert_eq!(FunctionCode::ReadInputRegisters, FunctionCode::new(0x04));
assert_eq!(FunctionCode::WriteMultipleCoils, FunctionCode::new(0x0F));
assert_eq!(
FunctionCode::WriteMultipleRegisters,
FunctionCode::new(0x10)
);
assert_eq!(FunctionCode::MaskWriteRegister, FunctionCode::new(0x016));
assert_eq!(
FunctionCode::ReadWriteMultipleRegisters,
FunctionCode::new(0x017)
);
assert_eq!(FunctionCode::Custom(70), FunctionCode::new(70));
}
#[test]
fn function_code_values() {
assert_eq!(FunctionCode::ReadCoils.value(), 0x01);
assert_eq!(FunctionCode::ReadDiscreteInputs.value(), 0x02);
assert_eq!(FunctionCode::WriteSingleCoil.value(), 0x05);
assert_eq!(FunctionCode::WriteSingleRegister.value(), 0x06);
assert_eq!(FunctionCode::ReadHoldingRegisters.value(), 0x03);
assert_eq!(FunctionCode::ReadInputRegisters.value(), 0x04);
assert_eq!(FunctionCode::WriteMultipleCoils.value(), 0x0F);
assert_eq!(FunctionCode::WriteMultipleRegisters.value(), 0x10);
assert_eq!(FunctionCode::MaskWriteRegister.value(), 0x016);
assert_eq!(FunctionCode::ReadWriteMultipleRegisters.value(), 0x017);
assert_eq!(FunctionCode::Custom(70).value(), 70);
}
#[test]
fn function_code_from_request() {
use Request::*;
assert_eq!(ReadCoils(0, 0).function_code(), FunctionCode::ReadCoils);
assert_eq!(
ReadDiscreteInputs(0, 0).function_code(),
FunctionCode::ReadDiscreteInputs
);
assert_eq!(
WriteSingleCoil(0, true).function_code(),
FunctionCode::WriteSingleCoil
);
assert_eq!(
WriteMultipleCoils(0, Cow::Borrowed(&[])).function_code(),
FunctionCode::WriteMultipleCoils
);
assert_eq!(
ReadInputRegisters(0, 0).function_code(),
FunctionCode::ReadInputRegisters
);
assert_eq!(
ReadHoldingRegisters(0, 0).function_code(),
FunctionCode::ReadHoldingRegisters
);
assert_eq!(
WriteSingleRegister(0, 0).function_code(),
FunctionCode::WriteSingleRegister
);
assert_eq!(
WriteMultipleRegisters(0, Cow::Borrowed(&[])).function_code(),
FunctionCode::WriteMultipleRegisters
);
assert_eq!(
MaskWriteRegister(0, 0, 0).function_code(),
FunctionCode::MaskWriteRegister
);
assert_eq!(
ReadWriteMultipleRegisters(0, 0, 0, Cow::Borrowed(&[])).function_code(),
FunctionCode::ReadWriteMultipleRegisters
);
assert_eq!(Custom(88, Cow::Borrowed(&[])).function_code().value(), 88);
}
#[test]
fn function_code_from_response() {
use Response::*;
assert_eq!(ReadCoils(vec![]).function_code(), FunctionCode::ReadCoils);
assert_eq!(
ReadDiscreteInputs(vec![]).function_code(),
FunctionCode::ReadDiscreteInputs
);
assert_eq!(
WriteSingleCoil(0x0, false).function_code(),
FunctionCode::WriteSingleCoil
);
assert_eq!(
WriteMultipleCoils(0x0, 0x0).function_code(),
FunctionCode::WriteMultipleCoils
);
assert_eq!(
ReadInputRegisters(vec![]).function_code(),
FunctionCode::ReadInputRegisters
);
assert_eq!(
ReadHoldingRegisters(vec![]).function_code(),
FunctionCode::ReadHoldingRegisters
);
assert_eq!(
WriteSingleRegister(0, 0).function_code(),
FunctionCode::WriteSingleRegister
);
assert_eq!(
WriteMultipleRegisters(0, 0).function_code(),
FunctionCode::WriteMultipleRegisters
);
assert_eq!(
MaskWriteRegister(0, 0, 0).function_code(),
FunctionCode::MaskWriteRegister
);
assert_eq!(
ReadWriteMultipleRegisters(vec![]).function_code(),
FunctionCode::ReadWriteMultipleRegisters
);
assert_eq!(
Custom(99, Bytes::from_static(&[])).function_code().value(),
99
);
}
}