Skip to content

Commit 791e765

Browse files
committed
add flowcontrol serial option hardware/software/none
1 parent f1ee2ac commit 791e765

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

Source/Device/Serial.cpp

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,22 +326,37 @@ namespace Device
326326
tty.c_cflag &= ~(PARENB | PARODD); // No parity
327327

328328
// ===== STEP 8: Flow control =====
329-
// Hardware flow control
330-
tty.c_cflag &= ~CRTSCTS;
329+
if (flowcontrol == FlowControl::HARDWARE)
330+
{
331+
tty.c_cflag |= CRTSCTS;
332+
Info() << "Serial: hardware flow control (RTS/CTS) enabled" << std::endl;
333+
}
334+
else
335+
{
336+
tty.c_cflag &= ~CRTSCTS;
337+
}
331338

332-
if (disable_xonxoff)
339+
// Software flow control
340+
if (flowcontrol == FlowControl::SOFTWARE)
341+
{
342+
// Enable XON/XOFF
343+
#ifdef IXANY
344+
tty.c_iflag |= (IXON | IXOFF);
345+
tty.c_iflag &= ~IXANY; // But not IXANY
346+
#else
347+
tty.c_iflag |= (IXON | IXOFF);
348+
#endif
349+
Info() << "Serial: software flow control (XON/XOFF) enabled" << std::endl;
350+
}
351+
else if (flowcontrol == FlowControl::NONE)
333352
{
334353
// Explicitly disable XON/XOFF
335354
#ifdef IXANY
336355
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
337356
#else
338357
tty.c_iflag &= ~(IXON | IXOFF);
339358
#endif
340-
Info() << "Serial: XON/XOFF software flow control explicitly disabled" << std::endl;
341-
}
342-
else
343-
{
344-
Info() << "Serial: XON/XOFF software flow control left at system defaults" << std::endl;
359+
Info() << "Serial: flow control disabled" << std::endl;
345360
}
346361

347362
// ===== STEP 9: VMIN/VTIME =====
@@ -437,7 +452,19 @@ namespace Device
437452
}
438453
else if (option == "DISABLE_XONXOFF")
439454
{
440-
disable_xonxoff = Util::Parse::Switch(arg);
455+
throw std::runtime_error("Serial: DISABLE_XONXOFF option is deprecated. Use FLOWCONTROL instead.");
456+
}
457+
else if (option == "FLOWCONTROL")
458+
{
459+
Util::Convert::toUpper(arg);
460+
if (arg == "NONE" || arg == "0")
461+
flowcontrol = FlowControl::NONE;
462+
else if (arg == "HARDWARE" || arg == "HW" || arg == "RTSCTS" || arg == "1")
463+
flowcontrol = FlowControl::HARDWARE;
464+
else if (arg == "SOFTWARE" || arg == "SW" || arg == "XONXOFF" || arg == "2")
465+
flowcontrol = FlowControl::SOFTWARE;
466+
else
467+
throw std::runtime_error("Serial: invalid flowcontrol option: " + arg + ". Valid options are NONE, HARDWARE, SOFTWARE.");
441468
}
442469
else
443470
Device::Set(option, arg);
@@ -447,9 +474,26 @@ namespace Device
447474

448475
std::string SerialPort::Get()
449476
{
450-
return Device::Get() + " baudrate " + std::to_string(baudrate) + " disable_xonxoff " + (disable_xonxoff ? "on" : "off") + " port " + port + " print " + Util::Convert::toString(print);
451-
}
477+
std::string fc_str;
478+
switch (flowcontrol)
479+
{
480+
case FlowControl::NONE:
481+
fc_str = "none";
482+
break;
483+
case FlowControl::HARDWARE:
484+
fc_str = "hardware";
485+
break;
486+
case FlowControl::SOFTWARE:
487+
fc_str = "software";
488+
break;
489+
}
452490

491+
return Device::Get() +
492+
" baudrate " + std::to_string(baudrate) +
493+
" flowcontrol " + fc_str +
494+
" port " + port +
495+
" print " + Util::Convert::toString(print);
496+
}
453497
void SerialPort::getDeviceList(std::vector<Description> &DeviceList)
454498
{
455499
device_list.clear();

Source/Device/Serial.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ namespace Device
3636

3737
class SerialPort : public Device
3838
{
39+
enum class FlowControl
40+
{
41+
NONE, // Explicitly disable both hardware and software flow control
42+
HARDWARE, // Enable hardware (RTS/CTS), disable software
43+
SOFTWARE // Enable software (XON/XOFF), disable hardware
44+
};
45+
3946
#ifdef _WIN32
4047
HANDLE serial_handle = INVALID_HANDLE_VALUE;
4148

@@ -66,8 +73,8 @@ namespace Device
6673

6774
std::string port;
6875
int baudrate;
69-
bool disable_xonxoff = false;
70-
76+
FlowControl flowcontrol = FlowControl::NONE; // Default
77+
7178
std::thread read_thread;
7279

7380
bool lost = false;

0 commit comments

Comments
 (0)