Skip to content

Commit 07c890e

Browse files
committed
improve serial handling
1 parent 1fdbc1e commit 07c890e

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

CompostaUSBBridge/CompostaUSBBridge.ino

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ void setup() {
132132
#endif
133133
/* Set up wifi event */
134134
WiFi.onEvent(CAtHandler::onWiFiEvent);
135-
136135

137136
xTaskCreatePinnedToCore(
138137
atLoop, /* Function to implement the task */
@@ -148,38 +147,39 @@ void setup() {
148147
arduino-builder -compile -fqbn=espressif:esp32:esp32s3:JTAGAdapter=default,PSRAM=disabled,FlashMode=qio,FlashSize=4M,LoopCore=1,EventsCore=1,USBMode=default,CDCOnBoot=cdc,MSCOnBoot=default,DFUOnBoot=default,UploadMode=default,PartitionScheme=huge_app,CPUFreq=240,UploadSpeed=921600,DebugLevel=none,EraseFlash=none -vid-pid=303A_1001 -ide-version=10820 CompostaUSBBridge.ino
149148
*/
150149

150+
static uint8_t buf[2048];
151+
151152
/* -------------------------------------------------------------------------- */
152153
void loop() {
153154
/* -------------------------------------------------------------------------- */
154155

155-
if (SERIAL_USER && SERIAL_USER.baudRate() != _baud) {
156+
if (SERIAL_USER.baudRate() != _baud) {
156157
_baud = SERIAL_USER.baudRate();
157158
}
158159

159-
uint8_t buf[2048];
160160
int i = 0;
161-
while (SERIAL_USER && SERIAL_USER.available() && i < sizeof(buf)) {
162-
buf[i++] = SERIAL_USER.read();
161+
162+
if (SERIAL_USER.available()) {
163+
i = min((unsigned int)SERIAL_USER.available(), sizeof(buf));
164+
SERIAL_USER.readBytes(buf, i);
163165
}
166+
164167
if (i > 0) {
165168
SERIAL_USER_INTERNAL.write(buf, i);
166169
}
170+
167171
i = 0;
168-
while (SERIAL_USER_INTERNAL.available() && i < sizeof(buf)) {
169-
buf[i++] = SERIAL_USER_INTERNAL.read();
172+
if (SERIAL_USER_INTERNAL.available()) {
173+
i = min((unsigned int)SERIAL_USER_INTERNAL.available(), sizeof(buf));
174+
SERIAL_USER_INTERNAL.readBytes(buf, i);
170175
}
171-
if (i > 0 && SERIAL_USER) {
176+
if (i > 0) {
172177
SERIAL_USER.write(buf, i);
173178
}
174179

175180
yield();
176181
}
177182

178-
179-
180-
181-
182-
183183
void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
184184
if (event_base == ARDUINO_USB_CDC_EVENTS) {
185185
arduino_usb_cdc_event_data_t * data = (arduino_usb_cdc_event_data_t*)event_data;

CompostaUSBBridge/at_handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ CAtHandler::CAtHandler(HardwareSerial *s) : last_server_client_sock(0) {
9999
yield();
100100
return (unsigned int)0;
101101
}
102-
return serial->read(buf, len);
102+
return serial->read(buf, min((unsigned int)serial->available(), len));
103103
},
104104
.callback_io_write = [this](auto buf, auto len) {
105105
return serial->write(buf, len);

0 commit comments

Comments
 (0)