62
62
#define deassertDE () hwDigitalWrite(MY_RS485_DE_PIN, LOW)
63
63
#else
64
64
#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 )
66
66
#endif
67
67
#else
68
68
#define assertDE ()
@@ -142,11 +142,14 @@ bool _serialProcess()
142
142
// Case 0 looks for the header. Bytes arrive in the serial interface and get
143
143
// shifted through a header buffer. When the start and end characters in
144
144
// 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.
146
147
case 0 :
147
148
memcpy (&_header[0 ],&_header[1 ],5 );
148
149
_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))) {
150
153
_recCalcCS = 0 ;
151
154
_recStation = _header[1 ];
152
155
_recSender = _header[2 ];
@@ -165,16 +168,6 @@ bool _serialProcess()
165
168
break ;
166
169
}
167
170
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
-
178
171
if (_recLen == 0 ) {
179
172
_recPhase = 2 ;
180
173
}
@@ -267,14 +260,7 @@ bool transportSend(const uint8_t to, const void* data, const uint8_t len, const
267
260
}
268
261
}
269
262
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 ();
278
264
279
265
// Start of header by writing multiple SOH
280
266
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
318
304
_dev.flush ();
319
305
#endif
320
306
#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 ();
326
308
#endif
327
309
return true ;
328
310
}
@@ -334,13 +316,10 @@ bool transportInit(void)
334
316
// Reset the state machine
335
317
_dev.begin (MY_RS485_BAUD_RATE);
336
318
_serialReset ();
319
+ _nodeId = AUTO;
337
320
#if defined(MY_RS485_DE_PIN)
338
321
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 ();
344
323
#endif
345
324
return true ;
346
325
}
0 commit comments