FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ModbusRTUComm/src/ModbusRTUComm.cpp at main · CMB27/ModbusRTUComm · GitHub
CMB27
/
ModbusRTUComm
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ModbusRTUComm
/
src
/
ModbusRTUComm.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
122 lines (117 loc) · 3.19 KB
Breadcrumbs
ModbusRTUComm
/
src
/
ModbusRTUComm.cpp
Copy path
File metadata and controls
122 lines (117 loc) · 3.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
ModbusRTUComm.h
"
ModbusRTUComm::ModbusRTUComm
(Stream& serial,
int
dePin,
int
rePin) : _serial(serial) {
_dePin = dePin;
_rePin = rePin;
}
void
ModbusRTUComm::begin
(
unsigned
long
baud,
uint32_t
config) {
unsigned
long
bitsPerChar;
switch
(config) {
case
SERIAL_8E2
:
case
SERIAL_8O2
:
bitsPerChar =
12
;
break
;
case
SERIAL_8N2
:
case
SERIAL_8E1
:
case
SERIAL_8O1
:
bitsPerChar =
11
;
break
;
case
SERIAL_8N1
:
default
:
bitsPerChar =
10
;
break
;
}
if
(baud <=
19200
) {
_charTimeout = (bitsPerChar *
2500000
) / baud;
_frameTimeout = (bitsPerChar *
4500000
) / baud;
}
else
{
_charTimeout = (bitsPerChar *
1000000
) / baud +
750
;
_frameTimeout = (bitsPerChar *
1000000
) / baud +
1750
;
}
_bytePeriod = (bitsPerChar *
1000000
) / baud;
_postDelay = ((bitsPerChar *
1000000
) +
1500000
) / baud;
if
(_dePin >=
0
) {
pinMode
(_dePin,
OUTPUT
);
digitalWrite
(_dePin,
LOW
);
}
if
(_rePin >=
0
) {
pinMode
(_rePin,
OUTPUT
);
digitalWrite
(_rePin,
LOW
);
}
unsigned
long
startMicros =
micros
();
do
{
if
(_serial.
available
() >
0
) {
startMicros =
micros
();
_serial.
read
();
}
}
while
(
micros
() - startMicros < _frameTimeout);
}
void
ModbusRTUComm::setTimeout
(
unsigned
long
timeout) {
_readTimeout = timeout;
}
ModbusRTUCommError
ModbusRTUComm::readAdu
(ModbusADU& adu) {
adu.
setRtuLen
(
0
);
unsigned
long
startMillis =
millis
();
while
(!_serial.
available
()) {
if
(
millis
() - startMillis >= _readTimeout)
return
MODBUS_RTU_COMM_TIMEOUT
;
}
uint16_t
len =
0
;
unsigned
long
startMicros =
micros
();
do
{
if
(_serial.
available
()) {
startMicros =
micros
();
adu.
rtu
[len] = _serial.
read
();
len++;
}
}
while
(
micros
() - startMicros <= _charTimeout && len <
256
);
adu.
setRtuLen
(len);
while
(
micros
() - startMicros < _frameTimeout);
if
(_serial.
available
()) {
adu.
setRtuLen
(
0
);
return
MODBUS_RTU_COMM_FRAME_ERROR
;
}
if
(!adu.
crcGood
()) {
adu.
setRtuLen
(
0
);
return
MODBUS_RTU_COMM_CRC_ERROR
;
}
return
MODBUS_RTU_COMM_SUCCESS
;
}
bool
ModbusRTUComm::writeAdu
(ModbusADU& adu) {
uint16_t
i =
0
;
uint16_t
j =
0
;
bool
transmitting =
true
;
bool
verified =
false
;
adu.
updateCrc
();
uint16_t
len = adu.
getRtuLen
();
if
(_dePin >=
0
)
digitalWrite
(_dePin,
HIGH
);
unsigned
long
microsNow=
micros
();
unsigned
long
txStartMicros = microsNow;
unsigned
long
rxStartMicros = microsNow;
while
(
true
) {
microsNow =
micros
();
if
(transmitting) {
if
(i ==
0
|| (i < len && microsNow - txStartMicros >= _bytePeriod)) {
txStartMicros = microsNow;
_serial.
write
(adu.
rtu
[i]);
_serial.
flush
();
i++;
}
if
(i == len && microsNow - txStartMicros >= _postDelay) {
if
(_dePin >=
0
)
digitalWrite
(_dePin,
LOW
);
transmitting =
false
;
}
}
if
(_serial.
available
()) {
rxStartMicros = microsNow;
uint8_t
value = _serial.
read
();
if
(j ==
0
) verified =
true
;
if
(j < len && value != adu.
rtu
[j]) verified =
false
;
j++;
}
if
(!transmitting && (microsNow - rxStartMicros) > _charTimeout) {
if
(j != len) verified =
false
;
break
;
}
}
return
verified;
}
Back
|
FazBrowse Home
|
New Git URL