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
/
RS485Master.cpp
108 lines (86 loc) · 3.27 KB
/
RS485Master.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
/*
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 "RS485Master.h"
RS485Master::RS485Master():
RS485Communicator(),
currentSlaveAddress(0),
lastSentTokenTimeout(0),
tokenReturnTimeout(400),
delayBetweenPolls(100){
memset(slavesAvailability,0,AVAILABILITY_BYTES);
}
bool RS485Master::begin(byte _dePin, unsigned long _tokenReturnTimeout, unsigned long _delayBetweenPolls, unsigned long _baud){
// too low timeouts are not accepted by default.
if (_tokenReturnTimeout < 20 ){
return false;
}
delayBetweenPolls=_delayBetweenPolls;
tokenReturnTimeout = _tokenReturnTimeout;
return RS485Communicator::begin(_dePin,0,_baud);
}
void RS485Master::setSlaveAvailability(byte _addr,bool _present){
if ((_addr > 0) && (_addr < MAXIMUM_DEVICES_COUNT)){
byte i=floor(_addr/8);
bitWrite(slavesAvailability[i], _addr%8, _present);
}
}
bool RS485Master::getSlaveAvailability(byte _addr){
if ((_addr > 0) && (_addr < MAXIMUM_DEVICES_COUNT)){
byte i=floor(_addr/8);
return bitRead(slavesAvailability[i], _addr%8);
}
return false;
}
void RS485Master::loop(){
unsigned long currentMillis = millis();
if (writePermission && (currentMillis - lastWritePermissionMillis <= delayBetweenPolls)) {
return;
}
byte nextAddressToPoll=0;
if (writePermission){
/* Master must manage the write Token.
* He have to calculate the next slave allowed to write and send the token to it.
*/
for(byte n=1;n<=MAXIMUM_DEVICES_COUNT;n++){
byte addr=(currentSlaveAddress+n)%MAXIMUM_DEVICES_COUNT;
if (bitRead(slavesAvailability[(byte)floor(addr/8)], addr%8)){
currentSlaveAddress = addr;
nextAddressToPoll = addr;
break;
}
}
/*
generate the token for the next client. If no salves are enabled no token is generated.
label: [LABEL_TOK_REG]
*/
lastSentTokenTimeout = 0;
}
if(RS485Communicator::loop(nextAddressToPoll, nextAddressToPoll>0)){
lastSentTokenTimeout = millis();
}
if ((!writePermission) && (lastSentTokenTimeout>0)){
/* Master also have to manage the token return timeouts.
* Timeouts occur when the slave crash and do not send the token back or when the token
* get lost due transmission error.
* In that case, the master take the writePermission who implicitly trigger
* the token regeneration (done in the next loop cycle, code at label [LABEL_TOK_REG])
*/
unsigned long currentMillis=millis();
if (currentMillis - lastSentTokenTimeout > tokenReturnTimeout) {
lastSentTokenTimeout = 0;
writePermission = true;
}
}
}