Skip to content

Commit 95bc109

Browse files
committed
Add read function
1 parent 65c976b commit 95bc109

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Serial.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ void Serial::write(const string& str) {
133133
}
134134
}
135135

136+
const string Serial::read() {
137+
if (IS_OPEN(_fd)) {
138+
char buf[256];
139+
size_t len = ::read(_fd, &buf, 255);
140+
buf[len] = '\0';
141+
return string(buf);
142+
}
143+
throw runtime_error("Error: Port is not open.");
144+
}
145+
136146
void Serial::close() {
137147
if (IS_OPEN(_fd)) {
138148
::close(_fd);

Serial.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Serial {
5656

5757
void open();
5858
void write(const string &str);
59+
const string read();
5960
void close();
6061
};
6162

0 commit comments

Comments
 (0)