Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions cores/portduino/linux/LinuxSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,26 @@ namespace arduino {
}

int LinuxSerial::peek(void) {
if (hasPeeked) return peekedByte;
unsigned char c = 0;
ssize_t rv = ::read(serial_port, &c, 1);
if (rv == 1) {
hasPeeked = true;
peekedByte = (int)c;
return peekedByte;
}
return -1;
}

int LinuxSerial::read(void) {
int buf = 0;
::read(serial_port, &buf, 1);
return buf;
if (hasPeeked) {
hasPeeked = false;
return peekedByte;
}
unsigned char c = 0;
ssize_t rv = ::read(serial_port, &c, 1);
if (rv == 1) return (int)c;
return -1;
}

void LinuxSerial::flush(void) {
Expand Down
4 changes: 4 additions & 0 deletions cores/portduino/linux/LinuxSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace arduino {
virtual size_t write(uint8_t);
using Print::write; // pull in write(str) and write(buf, size) from Print
virtual operator bool();

private:
bool hasPeeked = false;
int peekedByte = 0;
};

class SimSerial : public HardwareSerial {
Expand Down