This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRS485Communicator.cpp
319 lines (252 loc) · 9.6 KB
/
RS485Communicator.cpp
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
/*
RS485 communicator synchronization library
Copyright (C) 2018 - Ivan Vaccari <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RS485Communicator.h"
#define RS485_DRIVER_ENABLE HIGH
#define RS485_DRIVER_DISABLE LOW
RS485Communicator::RS485Communicator():
writePermission(false),
recvBufferTailIndex(0),
recvBufferHeadIndex(0),
recvBufferCount(0),
sendBufferTailIndex(0),
sendBufferHeadIndex(0),
sendBufferCount(0),
recvMessageCount(0),
sentMessageCount(0),
lenghtErrorCount(0),
crcErrorCount(0),
tokenCount(0)
{
}
bool RS485Communicator::begin(byte _dePin, byte _myAddr, unsigned long baud) {
de485ControlPin = _dePin;
localAddress = _myAddr;
// set the RE pin to low(read) by default
pinMode(de485ControlPin, OUTPUT);
digitalWrite(de485ControlPin, RS485_DRIVER_DISABLE);
// by default only the master (localAddress=0) must have write permission by default
writePermission = localAddress == 0;
if (localAddress == 0){
lastWritePermissionMillis = millis();
}
RS485_SERIAL_NAME.begin(baud);
while (!RS485_SERIAL_NAME) {}
return true;
}
bool RS485Communicator::loop(byte tokenAddress, bool writeToken) {
if (writePermission) {
// enable DRIVER ENABLE signal of the transcaiver, making the current transceiver in write mode
digitalWrite(de485ControlPin, RS485_DRIVER_ENABLE);
// wait some time to meke the driver enable signal effective
// FIXME: how to make this time not hardcoded?
delayMicroseconds(500);
//sending messages
while (sendBufferCount > 0) {
sendOneBufferedMessage();
}
if (writeToken) {
//sending token
sendToken(tokenAddress);
writePermission = false;
}
// disable DRIVER ENABLE signal of the transcaiver, making the current transceiver in read mode
digitalWrite(de485ControlPin, RS485_DRIVER_DISABLE);
if (writeToken) {
return true;
}
} else {
readOneMessage();
}
return false;
}
void RS485Communicator::readOneMessage() {
while (RS485_SERIAL_NAME.available()) {
byte b = RS485_SERIAL_NAME.read();
if (b == 255) {
/* Received byte is the delimiter. Message receive is complete. Now check for validity. */
if (currentReceivingMessage.bufferWrittenBytes < (3 + RS485_PAYLOAD_LENGTH + RS485_PAYLOAD_FIX_BYTES + 1)) {
/* Received message is shorter than expected. An error occurred during transmission .
Since we have no clue what bytes are lost, and we cannot recover it, the message is just discarded.
*/
currentReceivingMessage.clean();
lenghtErrorCount++;
} else if (calculateMessageCrc(¤tReceivingMessage) != currentReceivingMessage.getReceivedCrcByte()) {
/* The message checksum is invalid. An error occurred during transmission .
Since we have no clue what bytes are lost, and we cannot recover it, the message is just discarded.
*/
currentReceivingMessage.clean();
crcErrorCount++;
} else if ((currentReceivingMessage.getDestinationAddress() != localAddress) &&
(currentReceivingMessage.getDestinationAddress() != 254)){
/* I'm not the destination of the message. Discard it*/
currentReceivingMessage.clean();
} else if (currentReceivingMessage.getMessageType() == RS485Message::MessageToken) {
/* message is a token for me. Applying validity checks.*/
if (localAddress == 0) {
/* i'm the master and this is a token return.*/
tokenCount++;
writePermission = true;
lastWritePermissionMillis = millis();
} else if (currentReceivingMessage.getSourceAddress() == 0 ) {
/* i'm a slave. Slaves are allowed to receive tokens only from master.
Tokens from other slaves are protocol violations.*/
writePermission = true;
lastWritePermissionMillis = millis();
tokenCount++;
}
/* no need to maintain the message*/
currentReceivingMessage.clean();
} else {
/* Message is not a token, is valid AND i am the destination. trying to store it in the free space.*/
if (recvBufferCount < RS485_RECV_BUFFER_SIZE) {
recvBufferCount++;
recvCircularBuffer[recvBufferHeadIndex] = currentReceivingMessage;
/* pointing head to the next array cell */
recvBufferHeadIndex = (recvBufferHeadIndex + 1) % RS485_RECV_BUFFER_SIZE;
recvMessageCount++;
}
currentReceivingMessage.clean();
}
} else if (currentReceivingMessage.bufferWrittenBytes < (3 + RS485_PAYLOAD_LENGTH + RS485_PAYLOAD_FIX_BYTES + 1)) {
/*appending received bytes in the buffer*/
currentReceivingMessage.buffer[currentReceivingMessage.bufferWrittenBytes] = b;
currentReceivingMessage.bufferWrittenBytes++;
} else {
/* too many bytes. An error somewhere occurred. Discard the mesage*/
currentReceivingMessage.clean();
}
}
}
RS485Message* RS485Communicator::popMessage() {
RS485Message *m = NULL;
if (recvBufferCount > 0) {
m = new RS485Message();
*m = recvCircularBuffer[recvBufferTailIndex];
recvCircularBuffer[recvBufferTailIndex].clean();
recvBufferTailIndex = (recvBufferTailIndex + 1) % RS485_RECV_BUFFER_SIZE;
recvBufferCount--;
}
return m;
}
void RS485Communicator::writeMessageToBus(RS485Message * message) {
// calculating crc
byte crc = calculateMessageCrc(message);
/* Writing the actual message byte by byte.
We need to wait after each byte to be sure the byte has been effectively written to the serial port.
This is made by checking internal arduino registers.
*/
for (byte i = 0; i < 3+ RS485_PAYLOAD_LENGTH + RS485_PAYLOAD_FIX_BYTES; ++i) {
RS485_SERIAL_NAME.write(message->buffer[i]);
while (!(UCSR0A & (1 << TXC0)));
}
// write crc
RS485_SERIAL_NAME.write(crc);
while (!(UCSR0A & (1 << TXC0)));
//write message delimiter
RS485_SERIAL_NAME.write(255);
while (!(UCSR0A & (1 << TXC0)));
// wait some time time at the end of the transmission
// FIXME: check if this timer is still needed after the 'dramatic day discover'
delayMicroseconds(300);
message->clean();
}
bool RS485Communicator::sendOneBufferedMessage() {
if (sendBufferCount > 0 ) {
RS485Message* message = sendCircularBuffer + sendBufferTailIndex;
writeMessageToBus(message);
sendBufferCount--;
if (sendBufferCount > 0) {
sendBufferTailIndex = (sendBufferTailIndex + 1) % RS485_SEND_BUFFER_SIZE;
}
return true;
}
return false;
}
bool RS485Communicator::sendToken(byte _address) {
tokenMessage.setMessageType(RS485Message::MessageToken);
tokenMessage.setDestinationAddress(_address);
tokenMessage.setSourceAddress(localAddress);
writeMessageToBus(&tokenMessage);
return true;
}
byte RS485Communicator::inboxCount() {
return recvBufferCount;
}
byte RS485Communicator::available() {
return RS485_SERIAL_NAME.available();
}
boolean RS485Communicator::ready() {
return RS485_SERIAL_NAME;
}
byte RS485Communicator::getLocalAddress(){
return localAddress;
}
RS485Communicator::RS485QueueResult RS485Communicator::queueMessage(RS485Message *_message) {
if (sendBufferCount == RS485_SEND_BUFFER_SIZE) {
return RS485QueueResult::BufferFull;
}
if (sendBufferCount > 0) {
sendBufferHeadIndex = (sendBufferHeadIndex + 1) % RS485_SEND_BUFFER_SIZE;
}
sentMessageCount++;
_message->setSourceAddress(localAddress);
sendCircularBuffer[sendBufferHeadIndex] = *_message;
sendBufferCount++;
return RS485QueueResult::Success;
}
byte RS485Communicator::calculateMessageCrc(RS485Message * _message) {
/* crc is calculated on bytes 1,2,3,...,2+RS485_PAYLOAD_LENGTH+RS485_PAYLOAD_FIX_BYTES of the message.
The first one and the last one bytes are omitted (first is delimiter, last is where to place the CRC).
credits to: http://www.leonardomiliani.com/2013/un-semplice-crc8-per-arduino/
*/
byte crc = 0x00;
byte index = 3 + RS485_PAYLOAD_LENGTH + RS485_PAYLOAD_FIX_BYTES;
while (index > 1) {
byte extract = _message->buffer[index - 1];
for (byte tempI = 8; tempI; tempI--) {
byte sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum)
crc ^= 0x8C;
extract >>= 1;
}
index--;
}
/* This is the crc fix. Since byte 255 is reserved as message delimiter, it cannot be used on the other message bytes
This is an act of faith. We cannot then detect an error if the received-message crc will be 254 but the orignal one was faked from 255 to 254.
However, that's better than nothing.
*/
if (crc == 255)
crc = 254;
return crc;
}
unsigned long RS485Communicator::getSentMessageCount(){
return sentMessageCount;
}
unsigned long RS485Communicator::getRecvMessageCount(){
return recvMessageCount;
}
unsigned long RS485Communicator::getLenghtErrorCount(){
return lenghtErrorCount;
}
unsigned long RS485Communicator::getCrcErrorCount(){
return crcErrorCount;
}
unsigned long RS485Communicator::getTokenCount(){
return tokenCount;
}
bool RS485Communicator::haveWritePermission(){
return writePermission;
}