Skip to content

Commit 4afa7a6

Browse files
authored
RS485: Fix Auto ID assignment (#1451)
* S485: Fix auto id assignment * RS485: assertDE(),deassertDE() cleanup Co-authored-by: Klaudiusz <Klaudiusz>
1 parent 263ea26 commit 4afa7a6

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

hal/transport/RS485/MyTransportRS485.cpp

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#define deassertDE() hwDigitalWrite(MY_RS485_DE_PIN, LOW)
6363
#else
6464
#define assertDE() hwDigitalWrite(MY_RS485_DE_PIN, LOW); delayMicroseconds(5)
65-
#define deassertDE() hwDigitalWrite(MY_RS485_DE_PIN, HIG)
65+
#define deassertDE() hwDigitalWrite(MY_RS485_DE_PIN, HIGH)
6666
#endif
6767
#else
6868
#define assertDE()
@@ -142,11 +142,14 @@ bool _serialProcess()
142142
// Case 0 looks for the header. Bytes arrive in the serial interface and get
143143
// shifted through a header buffer. When the start and end characters in
144144
// the buffer match the SOH/STX pair, and the destination station ID matches
145-
// our ID, save the header information and progress to the next state.
145+
// our ID or BROADCAST_ADDRESS, save the header information and progress to
146+
// the next state.
146147
case 0:
147148
memcpy(&_header[0],&_header[1],5);
148149
_header[5] = inch;
149-
if ((_header[0] == SOH) && (_header[5] == STX) && (_header[1] != _header[2])) {
150+
if ((_header[0] == SOH) && (_header[5] == STX) &&
151+
((_header[1] == (char)_nodeId) ||
152+
(_header[1] == (char)BROADCAST_ADDRESS && _header[2] != (char)_nodeId))) {
150153
_recCalcCS = 0;
151154
_recStation = _header[1];
152155
_recSender = _header[2];
@@ -165,16 +168,6 @@ bool _serialProcess()
165168
break;
166169
}
167170

168-
//Check if we should process this message
169-
//We reject the message if we are the sender
170-
//We reject if we are not the receiver and message is not a broadcast
171-
if ((_recSender == _nodeId) ||
172-
(_recStation != _nodeId &&
173-
_recStation != BROADCAST_ADDRESS)) {
174-
_serialReset();
175-
break;
176-
}
177-
178171
if (_recLen == 0) {
179172
_recPhase = 2;
180173
}
@@ -267,14 +260,7 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
267260
}
268261
}
269262

270-
#if defined(MY_RS485_DE_PIN)
271-
#if !defined(MY_RS485_DE_INVERSE)
272-
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
273-
#else
274-
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
275-
#endif
276-
delayMicroseconds(5);
277-
#endif
263+
assertDE();
278264

279265
// Start of header by writing multiple SOH
280266
for(byte w=0; w<MY_RS485_SOH_COUNT; w++) {
@@ -318,11 +304,7 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
318304
_dev.flush();
319305
#endif
320306
#endif
321-
#if !defined(MY_RS485_DE_INVERSE)
322-
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
323-
#else
324-
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
325-
#endif
307+
deassertDE();
326308
#endif
327309
return true;
328310
}
@@ -334,13 +316,10 @@ bool transportInit(void)
334316
// Reset the state machine
335317
_dev.begin(MY_RS485_BAUD_RATE);
336318
_serialReset();
319+
_nodeId = AUTO;
337320
#if defined(MY_RS485_DE_PIN)
338321
hwPinMode(MY_RS485_DE_PIN, OUTPUT);
339-
#if !defined(MY_RS485_DE_INVERSE)
340-
hwDigitalWrite(MY_RS485_DE_PIN, LOW);
341-
#else
342-
hwDigitalWrite(MY_RS485_DE_PIN, HIGH);
343-
#endif
322+
deassertDE();
344323
#endif
345324
return true;
346325
}

0 commit comments

Comments
 (0)