mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-23 04:04:54 +08:00
Fixing a reading buffer bug and expanding the example
This commit is contained in:
parent
0eab7f1f38
commit
6853f8c05d
@ -1,3 +1,21 @@
|
||||
/***
|
||||
* This example expects the serial port has a loopback on it.
|
||||
*
|
||||
* Alternatively, you could use an Arduino:
|
||||
*
|
||||
* <pre>
|
||||
* setup() {
|
||||
* Serial.begin(<insert your baudrate here>);
|
||||
* }
|
||||
*
|
||||
* loop() {
|
||||
* if (Serial.available()) {
|
||||
* Serial.write(Serial.read());
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
@ -40,14 +58,15 @@ int run(int argc, char **argv)
|
||||
sscanf(argv[2], "%lu", &baud);
|
||||
|
||||
// port, baudrate, timeout in milliseconds
|
||||
serial::Serial serial(port, baud, 1000);
|
||||
serial::Serial my_serial(port, baud, 1000);
|
||||
|
||||
cout << "Is the serial port open?";
|
||||
if(serial.isOpen())
|
||||
if(my_serial.isOpen())
|
||||
cout << " Yes." << endl;
|
||||
else
|
||||
cout << " No." << endl;
|
||||
|
||||
// Get the Test string
|
||||
int count = 0;
|
||||
string test_string;
|
||||
if (argc == 4) {
|
||||
@ -55,17 +74,65 @@ int run(int argc, char **argv)
|
||||
} else {
|
||||
test_string = "Testing.";
|
||||
}
|
||||
while (true) {
|
||||
size_t bytes_wrote = serial.write(test_string);
|
||||
|
||||
string result = serial.read(test_string.length());
|
||||
// Test the timeout, there should be 1 second between prints
|
||||
cout << "Timeout == 1000ms, asking for 1 more byte than written." << endl;
|
||||
while (count < 10) {
|
||||
size_t bytes_wrote = my_serial.write(test_string);
|
||||
|
||||
string result = my_serial.read(test_string.length()+1);
|
||||
|
||||
cout << "Iteration: " << count << ", Bytes written: ";
|
||||
cout << bytes_wrote << ", Bytes read: ";
|
||||
cout << result.length() << ", String read: " << result << endl;
|
||||
|
||||
count += 1;
|
||||
}
|
||||
|
||||
// Test the timeout at 250ms
|
||||
my_serial.setTimeout(250);
|
||||
count = 0;
|
||||
cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
|
||||
while (count < 10) {
|
||||
size_t bytes_wrote = my_serial.write(test_string);
|
||||
|
||||
string result = my_serial.read(test_string.length()+1);
|
||||
|
||||
cout << "Iteration: " << count << ", Bytes written: ";
|
||||
cout << bytes_wrote << ", Bytes read: ";
|
||||
cout << result.length() << ", String read: " << result << endl;
|
||||
|
||||
count += 1;
|
||||
}
|
||||
|
||||
// Test the timeout at 250ms, but asking exactly for what was written
|
||||
count = 0;
|
||||
cout << "Timeout == 250ms, asking for exactly what was written." << endl;
|
||||
while (count < 10) {
|
||||
size_t bytes_wrote = my_serial.write(test_string);
|
||||
|
||||
string result = my_serial.read(test_string.length());
|
||||
|
||||
cout << "Iteration: " << count << ", Bytes written: ";
|
||||
cout << bytes_wrote << ", Bytes read: ";
|
||||
cout << result.length() << ", String read: " << result << endl;
|
||||
|
||||
count += 1;
|
||||
}
|
||||
|
||||
// Test the timeout at 250ms, but asking for 1 less than what was written
|
||||
count = 0;
|
||||
cout << "Timeout == 250ms, asking for 1 less than was written." << endl;
|
||||
while (count < 10) {
|
||||
size_t bytes_wrote = my_serial.write(test_string);
|
||||
|
||||
string result = my_serial.read(test_string.length()-1);
|
||||
|
||||
cout << "Iteration: " << count << ", Bytes written: ";
|
||||
cout << bytes_wrote << ", Bytes read: ";
|
||||
cout << result.length() << ", String read: " << result << endl;
|
||||
|
||||
count += 1;
|
||||
my_sleep(10);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@ -454,7 +454,8 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size)
|
||||
if (FD_ISSET (fd_, &readfds)) {
|
||||
// This should be non-blocking returning only what is avaialble now
|
||||
// Then returning so that select can block again.
|
||||
ssize_t bytes_read_now = ::read (fd_, buf, size-bytes_read);
|
||||
ssize_t bytes_read_now =
|
||||
::read (fd_, buf+bytes_read, size-bytes_read);
|
||||
// read should always return some data as select reported it was
|
||||
// ready to read when we get to this point.
|
||||
if (bytes_read_now < 1)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user