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:
parent
976307626d
commit
241daf3073
@ -1,9 +1,11 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <boost/thread.hpp>
|
||||||
|
|
||||||
#include "serial/serial.h"
|
#include "serial/serial.h"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int run(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if(argc < 2) {
|
if(argc < 2) {
|
||||||
std::cerr << "Usage: test_serial <serial port address>" << std::endl;
|
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]);
|
std::string port(argv[1]);
|
||||||
|
|
||||||
|
// port, baudrate, timeout in milliseconds
|
||||||
serial::Serial serial(port, 115200, 250);
|
serial::Serial serial(port, 115200, 250);
|
||||||
|
|
||||||
std::cout << "Is the serial port open?";
|
std::cout << "Is the serial port open?";
|
||||||
@ -23,11 +26,20 @@ int main(int argc, char **argv)
|
|||||||
while (count >= 0) {
|
while (count >= 0) {
|
||||||
int bytes_wrote = serial.write("Testing.");
|
int bytes_wrote = serial.write("Testing.");
|
||||||
std::string result = serial.read(8);
|
std::string result = serial.read(8);
|
||||||
if(count % 10 == 0)
|
std::cout << ">" << count << ">" << bytes_wrote << ">";
|
||||||
std::cout << ">" << count << ">" << bytes_wrote << ">" << result << std::endl;
|
std::cout << result.length() << "<" << result << std::endl;
|
||||||
|
|
||||||
count += 1;
|
count += 1;
|
||||||
|
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -643,12 +643,6 @@ private:
|
|||||||
// exact comparator function
|
// exact comparator function
|
||||||
static bool
|
static bool
|
||||||
_exactly (const std::string& token, std::string exact_str) {
|
_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;
|
return token == exact_str;
|
||||||
}
|
}
|
||||||
// startswith comparator function
|
// startswith comparator function
|
||||||
|
|||||||
@ -116,7 +116,7 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
else if (bytesize_ == FIVEBITS)
|
else if (bytesize_ == FIVEBITS)
|
||||||
options.c_cflag |= CS5;
|
options.c_cflag |= CS5;
|
||||||
else
|
else
|
||||||
throw invalid_argument ("Invalid char len");
|
throw invalid_argument ("invalid char len");
|
||||||
// setup stopbits
|
// setup stopbits
|
||||||
if (stopbits_ == STOPBITS_ONE)
|
if (stopbits_ == STOPBITS_ONE)
|
||||||
options.c_cflag &= (unsigned long) ~(CSTOPB);
|
options.c_cflag &= (unsigned long) ~(CSTOPB);
|
||||||
|
|||||||
@ -50,8 +50,8 @@ SerialListener::callback() {
|
|||||||
std::pair<FilterPtr,TokenPtr> pair;
|
std::pair<FilterPtr,TokenPtr> pair;
|
||||||
while (this->listening) {
|
while (this->listening) {
|
||||||
if (this->callback_queue.timed_wait_and_pop(pair, 10)) {
|
if (this->callback_queue.timed_wait_and_pop(pair, 10)) {
|
||||||
std::cout << "Got something off the callback queue: ";
|
std::cerr << "Got something off the callback queue: ";
|
||||||
std::cout << (*pair.second) << std::endl;
|
std::cerr << (*pair.second) << std::endl;
|
||||||
if (this->listening) {
|
if (this->listening) {
|
||||||
try {
|
try {
|
||||||
pair.first->callback_((*pair.second));
|
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."));
|
this->handle_exc(SerialListenerException("Serial port not open."));
|
||||||
}
|
}
|
||||||
temp = this->serial_port_->read(this_many);
|
temp = this->serial_port_->read(this_many);
|
||||||
|
// if (temp.length() > 0) {
|
||||||
|
std::cerr << "SerialListener read (" << temp.length() << "): ";
|
||||||
|
std::cerr << temp << std::endl;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
1
tests/proof_of_concepts/mdc2250.cc
Normal file
1
tests/proof_of_concepts/mdc2250.cc
Normal file
@ -0,0 +1 @@
|
|||||||
|
#include ""
|
||||||
@ -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 "gtest/gtest.h"
|
||||||
|
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
@ -13,62 +34,47 @@ static size_t global_count, global_listen_count;
|
|||||||
|
|
||||||
void default_handler(std::string line) {
|
void default_handler(std::string line) {
|
||||||
global_count++;
|
global_count++;
|
||||||
// std::cout << "default_handler got: " << line << std::endl;
|
std::cout << "default_handler got: " << line << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// class SerialListenerTests : public ::testing::Test {
|
class SerialListenerTests : public ::testing::Test {
|
||||||
// protected:
|
protected:
|
||||||
// virtual void SetUp() {
|
virtual void SetUp() {
|
||||||
// listener.listening = true;
|
port1 = new Serial(SERIAL_PORT_NAME, 115200, 250);
|
||||||
// listener.setDefaultHandler(default_handler);
|
|
||||||
// listener.callback_thread =
|
listener.setDefaultHandler(default_handler);
|
||||||
// boost::thread(boost::bind(&SerialListener::callback, &listener));
|
listener.startListening((*port1));
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// virtual void TearDown() {
|
virtual void TearDown() {
|
||||||
// listener.listening = false;
|
listener.stopListening();
|
||||||
// listener.callback_thread.join();
|
port1->close();
|
||||||
// }
|
delete port1;
|
||||||
//
|
}
|
||||||
// void stopCallbackThread() {
|
|
||||||
// while (true) {
|
SerialListener listener;
|
||||||
// boost::this_thread::sleep(boost::posix_time::milliseconds(1));
|
Serial * port1;
|
||||||
// boost::mutex::scoped_lock lock(listener.callback_queue.the_mutex);
|
|
||||||
// if (listener.callback_queue.the_queue.empty())
|
};
|
||||||
// break;
|
|
||||||
// }
|
void my_sleep(long milliseconds) {
|
||||||
// listener.listening = false;
|
boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds));
|
||||||
// listener.callback_thread.join();
|
}
|
||||||
// }
|
|
||||||
//
|
TEST_F(SerialListenerTests, handlesPartialMessage) {
|
||||||
// void execute_listenForStringOnce() {
|
global_count = 0;
|
||||||
// listener.listenForStringOnce("?$1E", 50);
|
std::string input_str = "?$1E\r$1E=Robo";
|
||||||
// }
|
|
||||||
//
|
port1->write(input_str);
|
||||||
// void simulate_loop(std::string input_str) {
|
|
||||||
// std::vector<TokenPtr> new_tokens;
|
// give some time for the callback thread to finish
|
||||||
// listener.tokenize(input_str, new_tokens);
|
my_sleep(1000);
|
||||||
// listener.filterNewTokens(new_tokens);
|
|
||||||
// }
|
ASSERT_EQ(1, global_count);
|
||||||
//
|
}
|
||||||
// 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);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// TEST_F(SerialListenerTests, listenForOnceWorks) {
|
// TEST_F(SerialListenerTests, listenForOnceWorks) {
|
||||||
// global_count = 0;
|
// global_count = 0;
|
||||||
//
|
//
|
||||||
@ -147,6 +153,11 @@ namespace {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
try {
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
|
} catch (std::exception &e) {
|
||||||
|
std::cerr << "Unhandled Exception: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user