From 601a0235879f0aa0679a7a4748e4760df12186f9 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 12 Dec 2023 14:40:08 -0800 Subject: [PATCH 1/2] As previously implemented, it's possible that our xon/off flow control could send XOFF, do a read, and then...not send XON, leading to a stuck pipe --- .../src/kaleidoscope/plugin/FocusSerial.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h index f4990b291e..862353cc4d 100644 --- a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h +++ b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.h @@ -116,24 +116,29 @@ class FocusSerial : public kaleidoscope::Plugin { void read(Key &key) { manageFlowControl(); key.setRaw(Runtime.serialPort().parseInt()); + manageFlowControl(); } void read(cRGB &color) { manageFlowControl(); color.r = Runtime.serialPort().parseInt(); color.g = Runtime.serialPort().parseInt(); color.b = Runtime.serialPort().parseInt(); + manageFlowControl(); } void read(char &c) { manageFlowControl(); Runtime.serialPort().readBytes(&c, 1); + manageFlowControl(); } void read(uint8_t &u8) { manageFlowControl(); u8 = Runtime.serialPort().parseInt(); + manageFlowControl(); } void read(uint16_t &u16) { manageFlowControl(); u16 = Runtime.serialPort().parseInt(); + manageFlowControl(); } From 81851f7878c9d3ca58bedbecaa16c3802fddf114 Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Tue, 12 Dec 2023 21:23:18 -0800 Subject: [PATCH 2/2] I was still getting XOFF with no XON with one of my test cases (63 A followed by a newline, sent via tio hexadecimal mode). I added the additional patch here, which fixed it. The extra calls to flush() are probably also a good idea. --- .../src/kaleidoscope/plugin/FocusSerial.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp index e1091382bc..30d7bb6741 100644 --- a/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp +++ b/plugins/Kaleidoscope-FocusSerial/src/kaleidoscope/plugin/FocusSerial.cpp @@ -39,11 +39,13 @@ void FocusSerial::manageFlowControl() { if (xon == true) { if (avail > RECV_BUFFER_THRESHOLD) { Runtime.serialPort().write(XOFF); // Send XOFF to stop data + Runtime.serialPort().flush(); xon = false; } } else { if (avail < RECV_BUFFER_RESUME) { Runtime.serialPort().write(XON); // Send XON to resume data + Runtime.serialPort().flush(); xon = true; } } @@ -65,6 +67,7 @@ EventHandlerResult FocusSerial::afterEachCycle() { break; } c = Runtime.serialPort().read(); + manageFlowControl(); // Don't store the separator; just stash it if (c == SEPARATOR) { break; @@ -82,6 +85,7 @@ EventHandlerResult FocusSerial::afterEachCycle() { Runtime.onFocusEvent(input_); while (Runtime.serialPort().available()) { c = Runtime.serialPort().read(); + manageFlowControl(); if (c == NEWLINE) { // newline serves as an end-of-command marker // don't drain the buffer past there