1010void Rdm6300::begin (int rx_pin, uint8_t uart_nr)
1111{
1212 /* init serial port to rdm6300 baud, without TX, and 20ms read timeout */
13- #ifdef ARDUINO_ARCH_ESP32
14- _serial = new HardwareSerial (uart_nr);
15- _serial->begin (RDM6300_BAUDRATE, SERIAL_8N1, rx_pin, -1 );
16- #else
17- _serial = new SoftwareSerial (rx_pin, -1 );
18- _serial->begin (RDM6300_BAUDRATE);
13+ end ();
14+ #if defined(ARDUINO_ARCH_ESP32)
15+ _stream = _hardware_serial = new HardwareSerial (uart_nr);
16+ _hardware_serial->begin (RDM6300_BAUDRATE, SERIAL_8N1, rx_pin, -1 );
17+ #elif defined(ARDUINO_ARCH_ESP8266)
18+ if (rx_pin == 13 ) {
19+ _stream = _hardware_serial = &Serial;
20+ _hardware_serial->begin (RDM6300_BAUDRATE, SERIAL_8N1, SERIAL_RX_ONLY);
21+ if (uart_nr)
22+ _hardware_serial->swap ();
23+ }
24+ #endif
25+ #ifdef RDM6300_SOFTWARE_SERIAL
26+ if (!_hardware_serial) {
27+ _stream = _software_serial = new SoftwareSerial (rx_pin, -1 );
28+ _software_serial->begin (RDM6300_BAUDRATE);
29+ }
30+ #endif
31+ if (!_stream)
32+ return ;
33+ _stream->setTimeout (RDM6300_READ_TIMEOUT);
34+ }
35+
36+ void Rdm6300::end ()
37+ {
38+ _stream = NULL ;
39+ #ifdef RDM6300_HARDWARE_SERIAL
40+ if (_hardware_serial)
41+ _hardware_serial->end ();
42+ #endif
43+ #ifdef RDM6300_SOFTWARE_SERIAL
44+ if (_software_serial)
45+ _software_serial->end ();
1946#endif
20- _serial->setTimeout (20 );
2147}
2248
2349bool Rdm6300::update (void )
@@ -26,15 +52,18 @@ bool Rdm6300::update(void)
2652 uint32_t tag_id;
2753 uint8_t checksum;
2854
29- if (!_serial->available ())
55+ if (!_stream)
56+ return false ;
57+
58+ if (!_stream->available ())
3059 return false ;
3160
3261 /* if a packet doesn't begin with the right byte, remove that byte */
33- if (_serial ->peek () != RDM6300_PACKET_BEGIN && _serial ->read ())
62+ if (_stream ->peek () != RDM6300_PACKET_BEGIN && _stream ->read ())
3463 return false ;
3564
3665 /* if read a packet with the wrong size, drop it */
37- if (RDM6300_PACKET_SIZE != _serial ->readBytes (buff, RDM6300_PACKET_SIZE))
66+ if (RDM6300_PACKET_SIZE != _stream ->readBytes (buff, RDM6300_PACKET_SIZE))
3867 return false ;
3968
4069 /* if a packet doesn't end with the right byte, drop it */
0 commit comments