1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

working on tests and stuff

This commit is contained in:
William Woodall 2012-01-23 09:54:31 -06:00
parent 976307626d
commit 241daf3073
6 changed files with 86 additions and 64 deletions

View File

@ -1,9 +1,11 @@
#include <string>
#include <iostream>
#include <boost/thread.hpp>
#include "serial/serial.h"
int main(int argc, char **argv)
int run(int argc, char **argv)
{
if(argc < 2) {
std::cerr << "Usage: test_serial <serial port address>" << std::endl;
@ -11,6 +13,7 @@ int main(int argc, char **argv)
}
std::string port(argv[1]);
// port, baudrate, timeout in milliseconds
serial::Serial serial(port, 115200, 250);
std::cout << "Is the serial port open?";
@ -23,11 +26,20 @@ int main(int argc, char **argv)
while (count >= 0) {
int bytes_wrote = serial.write("Testing.");
std::string result = serial.read(8);
if(count % 10 == 0)
std::cout << ">" << count << ">" << bytes_wrote << ">" << result << std::endl;
std::cout << ">" << count << ">" << bytes_wrote << ">";
std::cout << result.length() << "<" << result << std::endl;
count += 1;
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
}
return 0;
}
int main(int argc, char **argv) {
try {
return run(argc, argv);
} catch (std::exception &e) {
std::cerr << "Unhandled Exception: " << e.what() << std::endl;
}
}

View File

@ -643,12 +643,6 @@ private:
// exact comparator function
static bool
_exactly (const std::string& token, std::string exact_str) {
std::cout << token << " == " << exact_str << ": ";
if (token == exact_str)
std::cout << "True";
else
std::cout << "False";
std::cout << std::endl;
return token == exact_str;
}
// startswith comparator function

View File

@ -116,7 +116,7 @@ Serial::SerialImpl::reconfigurePort ()
else if (bytesize_ == FIVEBITS)
options.c_cflag |= CS5;
else
throw invalid_argument ("Invalid char len");
throw invalid_argument ("invalid char len");
// setup stopbits
if (stopbits_ == STOPBITS_ONE)
options.c_cflag &= (unsigned long) ~(CSTOPB);

View File

@ -50,8 +50,8 @@ SerialListener::callback() {
std::pair<FilterPtr,TokenPtr> pair;
while (this->listening) {
if (this->callback_queue.timed_wait_and_pop(pair, 10)) {
std::cout << "Got something off the callback queue: ";
std::cout << (*pair.second) << std::endl;
std::cerr << "Got something off the callback queue: ";
std::cerr << (*pair.second) << std::endl;
if (this->listening) {
try {
pair.first->callback_((*pair.second));
@ -118,6 +118,10 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) {
this->handle_exc(SerialListenerException("Serial port not open."));
}
temp = this->serial_port_->read(this_many);
// if (temp.length() > 0) {
std::cerr << "SerialListener read (" << temp.length() << "): ";
std::cerr << temp << std::endl;
// }
}
void

View File

@ -0,0 +1 @@
#include ""

View File

@ -1,3 +1,24 @@
/* To run these tests you need to change the define below to the serial port
* with a loop back device attached.
*
* Alternatively you could use an Arduino:
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0) {
Serial.write(Serial.read());
}
}
*/
#define SERIAL_PORT_NAME "/dev/tty.usbserial"
#include "gtest/gtest.h"
#include <boost/bind.hpp>
@ -13,62 +34,47 @@ static size_t global_count, global_listen_count;
void default_handler(std::string line) {
global_count++;
// std::cout << "default_handler got: " << line << std::endl;
std::cout << "default_handler got: " << line << std::endl;
}
namespace {
// class SerialListenerTests : public ::testing::Test {
// protected:
// virtual void SetUp() {
// listener.listening = true;
// listener.setDefaultHandler(default_handler);
// listener.callback_thread =
// boost::thread(boost::bind(&SerialListener::callback, &listener));
// }
//
// virtual void TearDown() {
// listener.listening = false;
// listener.callback_thread.join();
// }
//
// void stopCallbackThread() {
// while (true) {
// boost::this_thread::sleep(boost::posix_time::milliseconds(1));
// boost::mutex::scoped_lock lock(listener.callback_queue.the_mutex);
// if (listener.callback_queue.the_queue.empty())
// break;
// }
// listener.listening = false;
// listener.callback_thread.join();
// }
//
// void execute_listenForStringOnce() {
// listener.listenForStringOnce("?$1E", 50);
// }
//
// void simulate_loop(std::string input_str) {
// std::vector<TokenPtr> new_tokens;
// listener.tokenize(input_str, new_tokens);
// listener.filterNewTokens(new_tokens);
// }
//
// SerialListener listener;
//
// };
//
// TEST_F(SerialListenerTests, handlesPartialMessage) {
// global_count = 0;
// std::string input_str = "?$1E\r$1E=Robo";
//
// simulate_loop(input_str);
//
// // give some time for the callback thread to finish
// stopCallbackThread();
//
// ASSERT_EQ(global_count, 1);
// }
//
class SerialListenerTests : public ::testing::Test {
protected:
virtual void SetUp() {
port1 = new Serial(SERIAL_PORT_NAME, 115200, 250);
listener.setDefaultHandler(default_handler);
listener.startListening((*port1));
}
virtual void TearDown() {
listener.stopListening();
port1->close();
delete port1;
}
SerialListener listener;
Serial * port1;
};
void my_sleep(long milliseconds) {
boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds));
}
TEST_F(SerialListenerTests, handlesPartialMessage) {
global_count = 0;
std::string input_str = "?$1E\r$1E=Robo";
port1->write(input_str);
// give some time for the callback thread to finish
my_sleep(1000);
ASSERT_EQ(1, global_count);
}
// TEST_F(SerialListenerTests, listenForOnceWorks) {
// global_count = 0;
//
@ -147,6 +153,11 @@ namespace {
} // namespace
int main(int argc, char **argv) {
try {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
} catch (std::exception &e) {
std::cerr << "Unhandled Exception: " << e.what() << std::endl;
}
return 1;
}