Skip to main content

FunctionCode

Enum FunctionCode 

Source
#[repr(u8)]
pub enum FunctionCode {
Show 39 variants Default = 0, ReadCoils = 1, ReadDiscreteInputs = 2, WriteSingleCoil = 5, WriteMultipleCoils = 15, ReadHoldingRegisters = 3, ReadInputRegisters = 4, WriteSingleRegister = 6, WriteMultipleRegisters = 16, MaskWriteRegister = 22, ReadWriteMultipleRegisters = 23, ReadFifoQueue = 24, ReadFileRecord = 20, WriteFileRecord = 21, ReadExceptionStatus = 7, Diagnostics = 8, GetCommEventCounter = 11, GetCommEventLog = 12, ReportServerId = 17, EncapsulatedInterfaceTransport = 43, ReadCoilsException = 129, ReadDiscreteInputsException = 130, ReadHoldingRegistersException = 131, ReadInputRegistersException = 132, WriteSingleCoilException = 133, WriteSingleRegisterException = 134, ReadExceptionStatusException = 135, DiagnosticsException = 136, GetCommEventCounterException = 139, GetCommEventLogException = 140, WriteMultipleCoilsException = 143, WriteMultipleRegistersException = 144, ReportServerIdException = 145, ReadFileRecordException = 148, WriteFileRecordException = 149, MaskWriteRegisterException = 150, ReadWriteMultipleRegistersException = 151, ReadFifoQueueException = 152, EncapsulatedInterfaceTransportException = 171,
}
Expand description

Modbus Public Function Codes.

These are the standardized function codes defined in the Modbus Application Protocol Specification V1.1b3.

See:

  • Section 5.1 Public Function Code Definition
  • Section 6.x for individual function behaviors

Reference: :contentReference[oaicite:1]{index=1}

Variants§

§

Default = 0

0x00 — Undefined This value is not defined in the specification and can be used as a placeholder for uninitialized or unknown function codes. It is not a valid function code for actual Modbus transactions.

§

ReadCoils = 1

0x01 — Read Coils

Reads the ON/OFF status of discrete output coils. Section 6.1

§

ReadDiscreteInputs = 2

0x02 — Read Discrete Inputs

Reads the ON/OFF status of discrete input contacts. Section 6.2

§

WriteSingleCoil = 5

0x05 — Write Single Coil

Forces a single coil to ON (0xFF00) or OFF (0x0000). Section 6.5

§

WriteMultipleCoils = 15

0x0F — Write Multiple Coils

Forces multiple coils to ON/OFF. Section 6.11

§

ReadHoldingRegisters = 3

0x03 — Read Holding Registers

Reads one or more 16-bit holding registers. Section 6.3

§

ReadInputRegisters = 4

0x04 — Read Input Registers

Reads one or more 16-bit input registers. Section 6.4

§

WriteSingleRegister = 6

0x06 — Write Single Register

Writes a single 16-bit holding register. Section 6.6

§

WriteMultipleRegisters = 16

0x10 — Write Multiple Registers

Writes multiple 16-bit holding registers. Section 6.12

§

MaskWriteRegister = 22

0x16 — Mask Write Register

Performs a bitwise mask write on a single register. Section 6.16

§

ReadWriteMultipleRegisters = 23

0x17 — Read/Write Multiple Registers

Reads and writes multiple registers in a single transaction. Section 6.17

§

ReadFifoQueue = 24

0x18 — Read FIFO Queue

Reads the contents of a FIFO queue. Section 6.18

§

ReadFileRecord = 20

0x14 — Read File Record

Reads structured file records. Section 6.14

§

WriteFileRecord = 21

0x15 — Write File Record

Writes structured file records. Section 6.15

§

ReadExceptionStatus = 7

0x07 — Read Exception Status (Serial Line Only)

Returns 8-bit exception status. Section 6.7

§

Diagnostics = 8

0x08 — Diagnostics (Serial Line Only)

Provides diagnostic and loopback tests. Requires sub-function codes. Section 6.8

§

GetCommEventCounter = 11

0x0B — Get Communication Event Counter (Serial Line Only)

Returns communication event counter. Section 6.9

§

GetCommEventLog = 12

0x0C — Get Communication Event Log (Serial Line Only)

Returns communication event log. Section 6.10

§

ReportServerId = 17

0x11 — Report Server ID (Serial Line Only)

Returns server identification. Section 6.13

§

EncapsulatedInterfaceTransport = 43

0x2B — Encapsulated Interface Transport

Used for:

  • CANopen General Reference (Sub-function 0x0D)
  • Read Device Identification (Sub-function 0x0E)

Section 6.19, 6.20, 6.21

§

ReadCoilsException = 129

0x81 — Exception Response for Read Coils (0x01 | 0x80)

§

ReadDiscreteInputsException = 130

0x82 — Exception Response for Read Discrete Inputs (0x02 | 0x80)

§

ReadHoldingRegistersException = 131

0x83 — Exception Response for Read Holding Registers (0x03 | 0x80)

§

ReadInputRegistersException = 132

0x84 — Exception Response for Read Input Registers (0x04 | 0x80)

§

WriteSingleCoilException = 133

0x85 — Exception Response for Write Single Coil (0x05 | 0x80)

§

WriteSingleRegisterException = 134

0x86 — Exception Response for Write Single Register (0x06 | 0x80)

§

ReadExceptionStatusException = 135

0x87 — Exception Response for Read Exception Status (0x07 | 0x80)

§

DiagnosticsException = 136

0x88 — Exception Response for Diagnostics (0x08 | 0x80)

§

GetCommEventCounterException = 139

0x8B — Exception Response for Get Communication Event Counter (0x0B | 0x80)

§

GetCommEventLogException = 140

0x8C — Exception Response for Get Communication Event Log (0x0C | 0x80)

§

WriteMultipleCoilsException = 143

0x8F — Exception Response for Write Multiple Coils (0x0F | 0x80)

§

WriteMultipleRegistersException = 144

0x90 — Exception Response for Write Multiple Registers (0x10 | 0x80)

§

ReportServerIdException = 145

0x91 — Exception Response for Report Server ID (0x11 | 0x80)

§

ReadFileRecordException = 148

0x94 — Exception Response for Read File Record (0x14 | 0x80)

§

WriteFileRecordException = 149

0x95 — Exception Response for Write File Record (0x15 | 0x80)

§

MaskWriteRegisterException = 150

0x96 — Exception Response for Mask Write Register (0x16 | 0x80)

§

ReadWriteMultipleRegistersException = 151

0x97 — Exception Response for Read/Write Multiple Registers (0x17 | 0x80)

§

ReadFifoQueueException = 152

0x98 — Exception Response for Read FIFO Queue (0x18 | 0x80)

§

EncapsulatedInterfaceTransportException = 171

0xAB — Exception Response for Encapsulated Interface Transport (0x2B | 0x80)

Implementations§

Source§

impl FunctionCode

Source

pub fn exception_code_for_error(&self, error: &MbusError) -> ExceptionCode

Maps an application error to the corresponding Modbus exception code.

This method determines the appropriate exception code to return based on the error that occurred during request processing. For errors that don’t map to a specific exception code, ServerDeviceFailure is used as a default.

§Arguments
  • error - The error that occurred during processing
§Returns

The Modbus exception code to send in the response

§Example
let fc = FunctionCode::ReadHoldingRegisters;
let error = MbusError::InvalidAddress;
let exc_code = fc.exception_code_for_error(&error);
assert_eq!(exc_code, ExceptionCode::IllegalDataAddress);
Source

pub fn exception_response(&self) -> Option<FunctionCode>

Returns the exception function code variant (with 0x80 bit set) for this function code.

Exception responses use function codes with the high bit (0x80) set to indicate that an exception occurred. This method maps normal function codes to their exception equivalents.

§Returns

The exception function code variant, or None if this is not a valid function code that can have exceptions.

§Example
let fc = FunctionCode::ReadHoldingRegisters;
let exc_fc = fc.exception_response();
assert_eq!(exc_fc, Some(FunctionCode::ReadHoldingRegistersException));

Trait Implementations§

Source§

impl Clone for FunctionCode

Source§

fn clone(&self) -> FunctionCode

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FunctionCode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FunctionCode

Source§

fn default() -> FunctionCode

Returns the “default value” for a type. Read more
Source§

impl PartialEq for FunctionCode

Source§

fn eq(&self, other: &FunctionCode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<u8> for FunctionCode

Source§

type Error = MbusError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u8) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for FunctionCode

Source§

impl Eq for FunctionCode

Source§

impl StructuralPartialEq for FunctionCode

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.