Closed avillacis closed 2 years ago
In file SoftwareSerial.cpp line 468 I see the following comment and code:
// A stop bit can go undetected if leading data bits are at same level
// and there was also no next start bit yet, so one word may be pending.
// Check that there was no new ISR data received in the meantime, inserting an
// extraneous stop level bit out of sequence breaks rx.
if (m_rxCurBit > -1 && m_rxCurBit < m_pduBits - m_stopBits) {
const uint32_t detectionCycles = (m_pduBits - m_stopBits - m_rxCurBit) * m_bitCycles;
if (!m_isrBuffer->available() && ESP.getCycleCount() - m_isrLastCycle > detectionCycles) {
// Produce faux stop bit level, prevents start bit maldetection
// cycle's LSB is repurposed for the level bit
rxBits(((m_isrLastCycle + detectionCycles) | 1) ^ m_invert);
}
}
At a wild guess, I believe there is a bug in this code section that prevents it from working properly.
@avillacis Please verify that release 6.15.1, indeed fixes your issue. Thank you for pointing out this bug, it went previously unnoticed due to a mistake in the testing procedure.
Confirmed fixed in ESP32. I can now read all bytes from Nextion as intended.
Hardware: ESP32 Dev Module (YUBOX Node), no PSRAM, Nextion screen on pins 25 (TX), 32 (RX) Software: Arduino IDE 1.8.16, Arduino-ESP32 2.0.1 (released 9 November 2021)
When attempting to use SoftwareSerial for communication with a Nextion touchscreen, the last byte of the response packet (both command responses and event packets) gets "stuck" inside the library, until more data is sent from Nextion to the ESP32, at which the missing byte appears preceding the new data.
The Nextion serial protocol is a binary packet exchange, in which most, if not all packets, consist of a frame terminated by the binary sequence 0xFF,0xFF,0xFF. Data transmission from the ESP32 to the Nextion screen proceeds normally with both the default HardwareSerial class, and the SoftwareSerial one. However, when attempting to receive data, HardwareSerial behaves normally and all data bytes are eventually and timely received, but SoftwareSerial delivers all but the last 0xFF of the data response. This makes the packet appear "incomplete", and screws up both command response parsing and event handling on this setup.
Consider the following sample sketch (with NEX_SOFTWARE_SERIAL macro to switch between HardwareSerial and SoftwareSerial):
With HardwareSerial, I get the following output:
With SoftwareSerial, I get this output instead:
In both tests, a known-working firmware was used in the Nextion screen, which delivers the touch event packet, which consists of the byte sequence
65 00 09 01 ff ff ff
.With HardwareSerial, all bytes of the packet were received at the same time. With SoftwareSerial, however, the received sequence is
65 00 09 01 ff ff
. The last 0xFF is missing at that time. However, when sending the next touch event packet, the ESP32 receivesff 65 00 09 01 ff ff
. The missing byte from the previous data packet now appears preceding the new data packet.