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
use libc::c_int; use libmodbus_sys as ffi; use modbus::Modbus; use failure::Error; /// The Modbus protocol defines different data types and functions to read and write them from/to remote devices. /// The following functions are used by the clients to send Modbus requests: /// /// * Read data /// - [`read_bits()`](struct.Modbus.html#method.read_bits), /// [`read_input_bits()`](struct.Modbus.html#method.read_input_bits), /// [`read_registers()`](struct.Modbus.html#method.read_registers), /// [`read_input_registers()`](struct.Modbus.html#method.read_input_registers), /// [`report_slave_id()`](struct.Modbus.html#method.report_slave_id) /// * Write data /// - [`write_bit()`](struct.Modbus.html#method.write_bit), /// [`write_register()`](struct.Modbus.html#method.write_register), /// [`write_bits()`](struct.Modbus.html#method.write_bits), /// [`write_registers()`](struct.Modbus.html#method.write_registers) /// * Write and read data /// - [`write_and_read_registers()`](struct.Modbus.html#method.write_and_read_registers) /// * Raw requests /// - [`send_raw_request()`](struct.Modbus.html#method.send_raw_request), /// [`receive_confirmation()`](struct.Modbus.html#method.receive_confirmation) /// * Reply an exception /// - [`reply_exception()`](struct.Modbus.html#method.reply_exception) /// pub trait ModbusClient { fn read_bits(&self, address: u16, num: u16, dest: &mut [u8]) -> Result<u16, Error>; fn read_input_bits(&self, address: u16, num: u16, dest: &mut [u8]) -> Result<u16, Error>; fn read_registers(&self, address: u16, num: u16, dest: &mut [u16]) -> Result<u16, Error>; fn read_input_registers(&self, address: u16, num: u16, dest: &mut [u16]) -> Result<u16, Error>; fn report_slave_id(&self, max_dest: usize, dest: &mut [u8]) -> Result<u16, Error>; fn write_bit(&self, address: u16, status: bool) -> Result<(), Error>; fn write_bits(&self, address: u16, num: u16, src: &[u8]) -> Result<u16, Error>; fn write_register(&self, address: u16, value: u16) -> Result<(), Error>; fn write_registers(&self, address: u16, num: u16, src: &[u16]) -> Result<u16, Error>; fn write_and_read_registers(&self, write_address: u16, write_num: u16, src: &[u16], read_address: u16, read_num: u16, dest: &mut [u16]) -> Result<u16, Error>; fn mask_write_register(&self, address: u16, and_mask: u16, or_mask: u16) -> Result<(), Error>; fn send_raw_request(&self, raw_request: &mut [u8], lenght: usize) -> Result<u16, Error>; fn receive_confirmation(&self, response: &mut [u8]) -> Result<u16, Error>; } // TODO: add real, working examples impl ModbusClient for Modbus { /// `read_bits` - read many bits /// /// The [`read_bits()`](#method.read_bits) function shall read the status of the `num` bits (coils) to the /// `address` of the remote device. The result of reading is stored in `dest` slice as unsigned bytes (8 bits) set /// to TRUE or FALSE. /// /// The function uses the **Modbus function code 0x01** (read coil status). /// /// # Return value /// /// The function returns a `Result` containing the number of read bits if successful. Otherwise it returns an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `num` - number of coils to read /// * `dest` - the result of the reading is stored here /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let mut dest = vec![0u8; 100]; /// /// assert!(modbus.read_bits(0, 1, &mut dest).is_ok()); /// ``` fn read_bits(&self, address: u16, num: u16, dest: &mut [u8]) -> Result<u16, Error> { unsafe { match ffi::modbus_read_bits(self.ctx, address as c_int, num as c_int, dest.as_mut_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), len => Ok(len as u16), } } } /// `read_input_bits` - read many input bits /// /// The [`read_input_bits()`](#method.read_input_bits) function shall read the content of the `num` input bits to /// the `address` of the remote device. The result of reading is stored in `dest` slice as unsigned bytes (8 bits) /// set to TRUE or FALSE. /// /// The function uses the **Modbus function code 0x02** (read input status). /// /// # Return value /// /// The function returns a `Result` containing the number of read bits if successful. Otherwise it returns an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `num` - number of input bits to read /// * `dest` - the result of the reading is stored here /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let mut dest = vec![0u8; 100]; /// /// assert!(modbus.read_input_bits(0, 1, &mut dest).is_ok()); /// ``` fn read_input_bits(&self, address: u16, num: u16, dest: &mut [u8]) -> Result<u16, Error> { unsafe { match ffi::modbus_read_input_bits(self.ctx, address as c_int, num as c_int, dest.as_mut_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), len => Ok(len as u16), } } } /// `read_registers` - read many registers /// /// The [`read_registers()`](#method.read_registers) function shall read the content of the `num` holding registers /// to the `address` of the remote device. The result of reading is stored in `dest` slice as u16 word values. /// /// The function uses the **Modbus function code 0x03** (read holding registers). /// /// # Return value /// /// The function returns a `Result` containing the number of read bits if successful. Otherwise it returns an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `num` - number of holding registers to read /// * `dest` - the result of the reading is stored here /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let mut dest = vec![0u16; 100]; /// /// assert!(modbus.read_registers(0, 1, &mut dest).is_ok()); /// ``` fn read_registers(&self, address: u16, num: u16, dest: &mut [u16]) -> Result<u16, Error> { unsafe { match ffi::modbus_read_registers(self.ctx, address as c_int, num as c_int, dest.as_mut_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), len => Ok(len as u16), } } } /// `read_input_registers` - read many input registers /// /// The [`read_input_registers()`](#method.read_input_registers) function shall read the content of the `num` /// holding registers to the `address` of the remote device. The result of reading is stored in `dest` slice as u16 /// word values. /// /// The function uses the **Modbus function code 0x04** (read input registers). The holding registers and input /// registers have different historical meaning, but nowadays it’s more common to use holding registers only. /// /// # Return value /// /// The function returns a `Result` containing the number of read bits if successful. Otherwise it returns an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `num` - number of input registers to read /// * `dest` - the result of the reading is stored here /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let mut dest = vec![0u16; 100]; /// /// assert!(modbus.read_input_registers(0, 1, &mut dest).is_ok()); /// ``` fn read_input_registers(&self, address: u16, num: u16, dest: &mut [u16]) -> Result<u16, Error> { unsafe { match ffi::modbus_read_input_registers(self.ctx, address as c_int, num as c_int, dest.as_mut_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), len => Ok(len as u16), } } } /// `report_slave_id` - returns a description of the controller /// /// The [`report_slave_id()`](#method.report_slave_id) function shall send a request to the controller to obtain a /// description of the controller. The response stored in `dest` contains: /// * the slave ID, this unique ID is in reality not unique at all so it's not possible to depend on it to know /// how the information are packed in the response. /// * the run indicator status (0x00 = OFF, 0xFF = ON) /// * additional data specific to each controller. For example, libmodbus returns the version of the library as /// a string. /// /// # Return value /// /// The function returns a `Result` containing the number of read bits if successful. If the output was truncated /// due the `max_dest` limit then the return value is the number of bytes which would have been written to `dest`. /// Thus, a return value greater than the `max_dest` means that the resonse data was truncated. /// Otherwise the Result contains an Error. /// /// # Parameters /// /// * `max_dest` - limit, write `max_dest` bytes from the response to `dest` /// * `dest` - the result of the reading is stored here /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let mut bytes = vec![0u8; Modbus::MAX_PDU_LENGTH]; /// /// assert!(modbus.report_slave_id(Modbus::MAX_PDU_LENGTH, &mut bytes).is_ok()); /// // assert_eq!(bytes, vec![180, 255, 76, 77, 66, 51, 46, 49, 46, 52])); /// ``` fn report_slave_id(&self, max_dest: usize, dest: &mut [u8]) -> Result<u16, Error> { unsafe { match ffi::modbus_report_slave_id(self.ctx, max_dest as c_int, dest.as_mut_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), len => Ok(len as u16), } } } /// `write_bit` - write a single bit /// /// The [`write_bit()`](#method.write_bit) function shall write the `status` at the `address` of the remote device. /// The value must be set to `true` of `false`. /// /// The function uses the Modbus function code 0x05 (force single coil). /// /// # Return value /// /// The function return an OK Result, containing a one, if successful. Otherwise it contains an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `status` - status that should write at the address `addr` /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let address = 1; /// /// assert!(modbus.write_bit(address, true).is_ok()); /// ``` fn write_bit(&self, address: u16, status: bool) -> Result<(), Error> { unsafe { match ffi::modbus_write_bit(self.ctx, address as c_int, status as c_int) { -1 => bail!(::std::io::Error::last_os_error()), 1 => Ok(()), _ => panic!("libmodbus API incompatible response"), } } } /// `write_register` - write a single register /// /// The [`write_register()`](#method.write_register) function shall write the value of value holding registers at /// the address addr of the remote device. /// /// The function uses the Modbus function code 0x06 (preset single register). /// /// # Return value /// /// The function return an OK Result, containing a one, if successful. Otherwise it contains an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `value` - vec with the value of the holding register which shall be written /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let address = 1; /// let value = u16::max_value(); /// /// assert!(modbus.write_register(address, value).is_ok()); /// ``` fn write_register(&self, address: u16, value: u16) -> Result<(), Error> { unsafe { match ffi::modbus_write_register(self.ctx, address as c_int, value as c_int) { -1 => bail!(::std::io::Error::last_os_error()), 1 => Ok(()), _ => panic!("libmodbus API incompatible response"), } } } /// `write_bits` - write many bits /// /// The [`write_bits()`](#method.write_bits) function shall write the status of the bits (coils) from `src` at the /// `address` of the remote device. The `src` array must contains bytes set to TRUE or FALSE. /// /// The function shall return the number of written bits if successful. Otherwise it contains an Error. /// /// # Return value /// /// The function returns a Ok Result containing the number of written bits. Otherwise it contains an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `num` - number or bits that should be writen at the address `address` /// * `src` - vec of `0` and `1` (true and false) values /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let address = 1; /// let tab_bytes = vec![0u8]; /// /// assert_eq!(modbus.write_bits(address, 1, &tab_bytes).unwrap(), 1); /// ``` fn write_bits(&self, address: u16, num: u16, src: &[u8]) -> Result<u16, Error> { unsafe { match ffi::modbus_write_bits(self.ctx, address as c_int, num as c_int, src.as_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), num => Ok(num as u16), } } } /// `write_registers` - write many registers /// /// The [`write_registers()`](#method.write_registers) function shall write the content of the `num` holding /// registers /// from the array `src` at `address` of the remote device. /// /// The function uses the Modbus function code 0x10 (preset multiple registers). /// /// # Return value /// /// The function returns a Ok Result containing the number of written bytes. Otherwise it contains an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `num` - number of holding registers that should write at the address `address` /// * `src` - holding register /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let address = 1; /// let tab_bytes = vec![0u16]; /// /// assert_eq!(modbus.write_registers(address, 1, &tab_bytes).unwrap(), 1); /// ``` fn write_registers(&self, address: u16, num: u16, src: &[u16]) -> Result<u16, Error> { unsafe { match ffi::modbus_write_registers(self.ctx, address as c_int, num as c_int, src.as_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), num => Ok(num as u16), } } } /// `write_and_read_registers` - write and read many registers in a single transaction /// /// The [`write_and_read_registers()`](#method.write_and_read_registers) function shall write the content of the /// write_nb holding registers from the array src to the address write_addr of the remote device then shall read /// the content of the read_nb holding registers to the address read_addr of the remote device. The result of /// reading is stored in dest array as word values (16 bits). /// /// The function uses the Modbus function code 0x17 (write/read registers). /// /// # Return value /// /// The function returns a Ok Result containing the number of read registers. Otherwise it contains an Error. /// /// # Parameters /// /// * `write_address` - address of the remote device /// * `write_num` - number of holding registers /// * `src` - holding register /// * `read_address` - address of the remote device /// * `read_num` - number of holding registers /// * `dest` - holding register /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let address = 1; /// let request_bytes = vec![1u16]; /// let mut response_bytes = vec![0u16]; /// /// assert_eq!(modbus.write_and_read_registers( /// address, 1, &request_bytes, /// address, 1, &mut response_bytes).unwrap(), 1); /// ``` fn write_and_read_registers(&self, write_address: u16, write_num: u16, src: &[u16], read_address: u16, read_num: u16, dest: &mut [u16]) -> Result<u16, Error> { unsafe { match ffi::modbus_write_and_read_registers(self.ctx, write_address as c_int, write_num as c_int, src.as_ptr(), read_address as c_int, read_num as c_int, dest.as_mut_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), num => Ok(num as u16), } } } /// `mask_write_register` - mask a single register /// /// The [`mask_write_register()`](#method.mask_write_register) function shall modify the value of the /// holding register at the address `address` of the remote device using the algorithm: /// /// ```bash,no_run /// new value = (current value AND 'and') OR ('or' AND (NOT 'and')) /// ``` /// /// The function uses the **Modbus function code 0x16** (mask single register). /// /// # Return value /// /// The function returns a Ok Result if succesful. Otherwise it contains an Error. /// /// # Parameters /// /// * `address` - address of the remote device /// * `and_mask` - AND mask /// * `or_mask` - OR mask /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// /// assert!(modbus.mask_write_register(1, 0xF2, 0x25).is_ok()); /// ``` fn mask_write_register(&self, address: u16, and_mask: u16, or_mask: u16) -> Result<(), Error> { unsafe { match ffi::modbus_mask_write_register(self.ctx, address as c_int, and_mask, or_mask) { -1 => bail!(::std::io::Error::last_os_error()), 1 => Ok(()), _ => panic!("libmodbus API incompatible response"), } } } /// `send_raw_request` - send a raw request /// /// The [`send_raw_request()`](#method.send_raw_request) function shall send a request via the socket of the /// current modbus contest. /// This function must be used for debugging purposes because you have to take care to make a valid request by hand. /// The function only adds to the message, the header or CRC of the selected backend, so `raw_request` must start /// and contain at least a slave/unit identifier and a function code. /// This function can be used to send request not handled by the library. /// /// The enum [`FunctionCode`](enum.FunctionCode.html) provides a list of supported Modbus functions codes, to help /// build of raw requests. /// /// # Parameters /// /// * `raw_request` - raw request to send /// * `length` - raw request length /// /// # Return value /// /// The function returns a Result, containing the full message lenght, counting the extra data relating to the /// backend, if successful. Otherwise it contains an Error. /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP, FunctionCode}; /// /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let mut raw_request: Vec<u8> = vec![0xFF, FunctionCode::ReadHoldingRegisters as u8, 0x00, 0x01, 0x0, 0x05]; /// let mut response = vec![0u8; Modbus::TCP_MAX_ADU_LENGTH]; /// let request_len = raw_request.len(); /// /// assert_eq!(modbus.send_raw_request(&mut raw_request, request_len).unwrap(), 6); /// assert!(modbus.receive_confirmation(&mut response).is_ok()); /// ``` fn send_raw_request(&self, raw_request: &mut [u8], lenght: usize) -> Result<u16, Error> { unsafe { match ffi::modbus_send_raw_request(self.ctx, raw_request.as_mut_ptr(), lenght as c_int) { -1 => bail!(::std::io::Error::last_os_error()), num => Ok(num as u16), } } } /// `receive_confirmation` - receive a confirmation request /// /// The [`receive_confirmation()`](#method.receive_confirmation) function shall receive a request via the socket of /// the context `ctx` Member of the [Modbus struct](struct.Modbus.html). /// This function must be used for debugging purposes because the received response isn’t checked against the /// initial request. /// This function can be used to receive request not handled by the library. /// /// The maximum size of the response depends on the used backend, /// in RTU the `response` array must be `Modbus::RTU_MAX_ADU_LENGTH` bytes and in TCP it must be /// `Modbus::TCP_MAX_ADU_LENGTH` bytes. /// If you want to write code compatible with both, you can use the constant MODBUS_MAX_ADU_LENGTH (maximum value /// of all libmodbus backends). /// Take care to allocate enough memory to store responses to avoid crashes of your server. /// /// # Return value /// /// The function returns a Result containing the response length if successful. The returned request length can be /// zero if the indication request is ignored (eg. a query for another slave in RTU mode). /// Otherwise it contains an Error. /// /// # Parameters /// /// * `response` - store for the received response /// /// # Examples /// /// ```rust,no_run /// use libmodbus_rs::{Modbus, ModbusClient, ModbusTCP}; /// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap(); /// let mut response = vec![0u8; Modbus::MAX_ADU_LENGTH]; /// /// assert!(modbus.receive_confirmation(&mut response).is_ok()); /// ``` fn receive_confirmation(&self, response: &mut [u8]) -> Result<u16, Error> { unsafe { match ffi::modbus_receive_confirmation(self.ctx, response.as_mut_ptr()) { -1 => bail!(::std::io::Error::last_os_error()), len => Ok(len as u16), } } } }