|
| 1 | +/* |
| 2 | + This file is part of the ArduinoModbus library. |
| 3 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#ifndef _MODBUS_RTU_DELAY_H_INCLUDED |
| 21 | +#define _MODBUS_RTU_DELAY_H_INCLUDED |
| 22 | + |
| 23 | +class ModbusRTUDelay |
| 24 | +{ |
| 25 | +public: |
| 26 | + /* Do not allow construction or copying. */ |
| 27 | + ModbusRTUDelay() = delete; |
| 28 | + ModbusRTUDelay(ModbusRTUDelay const &) = delete; |
| 29 | + |
| 30 | + /* Calculate __minimum__ preDelay and postDelay in |
| 31 | + * microseconds as per Modbus RTU Specification. |
| 32 | + * |
| 33 | + * MODBUS over serial line specification and implementation guide V1.02 |
| 34 | + * Paragraph 2.5.1.1 MODBUS Message RTU Framing |
| 35 | + * https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf |
| 36 | + */ |
| 37 | + static unsigned long const preDelay(unsigned long const baudrate) |
| 38 | + { |
| 39 | + double const bit_duration = 1. / baudrate; |
| 40 | + double const word_len = 9.6f; // try also with 10.0f |
| 41 | + double const pre_delay_us = bit_duration * word_len * 3.5f * 1e6; |
| 42 | + return static_cast<unsigned long>(pre_delay_us); |
| 43 | + } |
| 44 | + static unsigned long const postDelay(unsigned long const baudrate) |
| 45 | + { |
| 46 | + return preDelay(baudrate); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +#endif /* _MODBUS_RTU_DELAY_H_INCLUDED */ |
0 commit comments