forked from pelud/modbus-potato
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModbusASCII.h
77 lines (75 loc) · 2.35 KB
/
ModbusASCII.h
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
#ifndef __ModbusPotato_ModbusASCII_h__
#define __ModbusPotato_ModbusASCII_h__
#include "ModbusInterface.h"
namespace ModbusPotato
{
/// <summary>
/// This class handles the RTU based protocol for Modbus.
/// </summary>
/// <remarks>
/// See the IFramer interface for a complete description of the public
/// methods.
///
/// The setup() method must be called with the correct baud rate before
/// using this class in order to calculate the proper inter-character and
/// inter-frame delays.
/// </remarks>
class CModbusASCII : public IFramer
{
public:
/// <summary>
/// Constructor for the RTU framer.
/// </summary>
CModbusASCII(IStream* stream, ITimeProvider* timer, uint8_t* buffer, size_t buffer_max);
/// <summary>
/// Sets the timeout, in milliseconds
/// </summary>
/// <remarks>
/// The poll() method must be called and the timer adjusted according
/// to the semantics described in IFramer::poll() for the new timeout
/// to take effect.
/// </remarks>
void set_timeout(unsigned int milliseconds);
unsigned long poll();
bool begin_send();
void send();
void finished();
bool frame_ready() const override { return m_state == state_frame_ready; }
private:
enum
{
LRC_LEN = 1,
min_pdu_length = 2, // minimum PDU length, excluding the station address. function code and one LRC byte
default_timeout = 1000, // default timeout, in milliseconds
};
uint8_t m_checksum;
uint8_t m_buffer_tx_pos;
enum state_type
{
state_exception,
state_idle,
state_frame_ready,
state_queue,
state_collision,
state_rx_addr_high,
state_rx_addr_low,
state_rx_pdu_high,
state_rx_pdu_low,
state_rx_cr,
state_tx_sof,
state_tx_addr_high,
state_tx_addr_low,
state_tx_pdu_high,
state_tx_pdu_low,
state_tx_lrc_high,
state_tx_lrc_low,
state_tx_cr,
state_tx_lf,
state_tx_wait,
};
state_type m_state;
system_tick_t m_last_ticks;
system_tick_t m_T1s;
};
}
#endif