mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-22 19:54:57 +08:00
Merge branch 'boostless' of https://github.com/wjwwood/serial into boostless
This commit is contained in:
commit
2be24ab23c
@ -1,5 +1,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <boost/thread.hpp>
|
#include <boost/thread.hpp>
|
||||||
|
|
||||||
@ -7,14 +8,17 @@
|
|||||||
|
|
||||||
int run(int argc, char **argv)
|
int run(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if(argc < 2) {
|
if(argc < 3) {
|
||||||
std::cerr << "Usage: test_serial <serial port address>" << std::endl;
|
std::cerr << "Usage: test_serial <serial port address> <baudrate>" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
std::string port(argv[1]);
|
std::string port(argv[1]);
|
||||||
|
unsigned long baud = 0;
|
||||||
|
|
||||||
|
sscanf(argv[2], "%lu", &baud);
|
||||||
|
|
||||||
// port, baudrate, timeout in milliseconds
|
// port, baudrate, timeout in milliseconds
|
||||||
serial::Serial serial(port, 115200, 250);
|
serial::Serial serial(port, baud, 250);
|
||||||
|
|
||||||
std::cout << "Is the serial port open?";
|
std::cout << "Is the serial port open?";
|
||||||
if(serial.isOpen())
|
if(serial.isOpen())
|
||||||
|
|||||||
@ -83,38 +83,52 @@ typedef enum {
|
|||||||
FLOWCONTROL_HARDWARE
|
FLOWCONTROL_HARDWARE
|
||||||
} flowcontrol_t;
|
} flowcontrol_t;
|
||||||
|
|
||||||
class SerialExecption : public std::exception {
|
class SerialExecption : public std::exception
|
||||||
const char * e_what;
|
{
|
||||||
|
const char* e_what_;
|
||||||
public:
|
public:
|
||||||
SerialExecption(const char *description) {e_what=description;};
|
SerialExecption (const char *description) : e_what_ (description) {}
|
||||||
virtual const char* what() const throw() {
|
|
||||||
|
virtual const char* what () const throw ()
|
||||||
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "SerialException " << this->e_what << " failed.";
|
ss << "SerialException " << e_what_ << " failed.";
|
||||||
return ss.str().c_str();
|
return ss.str ().c_str ();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class IOException : public std::exception {
|
class IOException : public std::exception
|
||||||
const char * e_what;
|
{
|
||||||
|
const char* e_what_;
|
||||||
|
int errno_;
|
||||||
public:
|
public:
|
||||||
IOException(const char * description) {e_what = description;}
|
explicit IOException (int errnum) : e_what_ (strerror (errnum)), errno_(errnum) {}
|
||||||
|
explicit IOException (const char * description) : e_what_ (description), errno_(0) {}
|
||||||
|
|
||||||
virtual const char* what() const throw() {
|
int getErrorNumber () { return errno_; }
|
||||||
|
|
||||||
|
virtual const char* what () const throw ()
|
||||||
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << "IO Exception " << this->e_what << " failed.";
|
if (errno_ == 0)
|
||||||
return ss.str().c_str();
|
ss << "IO Exception " << e_what_ << " failed.";
|
||||||
|
else
|
||||||
|
ss << "IO Exception " << errno_ << ":" << e_what_ << " failed.";
|
||||||
|
return ss.str ().c_str ();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class PortNotOpenedException : public std::exception {
|
class PortNotOpenedException : public std::exception
|
||||||
const char * e_what;
|
{
|
||||||
|
const char * e_what_;
|
||||||
public:
|
public:
|
||||||
PortNotOpenedException(const char * description) {e_what = description;}
|
PortNotOpenedException (const char * description) : e_what_ (description) {}
|
||||||
|
|
||||||
virtual const char* what() const throw() {
|
virtual const char* what () const throw ()
|
||||||
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << e_what << " called before port was opened.";
|
ss << e_what_ << " called before port was opened.";
|
||||||
return ss.str().c_str();
|
return ss.str ().c_str ();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -64,7 +64,19 @@ Serial::SerialImpl::open ()
|
|||||||
|
|
||||||
if (fd_ == -1)
|
if (fd_ == -1)
|
||||||
{
|
{
|
||||||
throw IOException ("invalid file descriptor");
|
switch (errno)
|
||||||
|
{
|
||||||
|
case EINTR:
|
||||||
|
// Recurse because this is a recoverable error.
|
||||||
|
open ();
|
||||||
|
return;
|
||||||
|
case ENFILE:
|
||||||
|
case EMFILE:
|
||||||
|
throw IOException ("to many file handles open");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw IOException (errno);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reconfigurePort();
|
reconfigurePort();
|
||||||
@ -215,7 +227,7 @@ Serial::SerialImpl::available ()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw IOException ("ioctl");
|
throw IOException (errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,8 +256,7 @@ Serial::SerialImpl::read (char* buf, size_t size)
|
|||||||
|
|
||||||
if (r == -1)
|
if (r == -1)
|
||||||
{
|
{
|
||||||
perror("select()");
|
throw IOException (errno);
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,12 +287,27 @@ Serial::SerialImpl::read (char* buf, size_t size)
|
|||||||
size_t
|
size_t
|
||||||
Serial::SerialImpl::write (const string &data)
|
Serial::SerialImpl::write (const string &data)
|
||||||
{
|
{
|
||||||
if (isOpen_ == false) {
|
if (isOpen_ == false)
|
||||||
|
{
|
||||||
throw PortNotOpenedException ("Serial::write");
|
throw PortNotOpenedException ("Serial::write");
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t n = ::write (fd_, data.c_str (), data.length ());
|
ssize_t n = ::write (fd_, data.c_str (), data.length ());
|
||||||
if (n == -1) {
|
|
||||||
throw IOException ("Write");
|
if (n != static_cast<ssize_t> (data.length ()))
|
||||||
|
{
|
||||||
|
throw IOException ("Write did not complete");
|
||||||
|
}
|
||||||
|
else if (n == -1)
|
||||||
|
{
|
||||||
|
if (errno == EINTR)
|
||||||
|
{
|
||||||
|
return write (data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw IOException (errno);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return static_cast<size_t> (n);
|
return static_cast<size_t> (n);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -167,7 +167,7 @@ Serial::readlines(string eol)
|
|||||||
{
|
{
|
||||||
if (pimpl_->getTimeout () < 0)
|
if (pimpl_->getTimeout () < 0)
|
||||||
{
|
{
|
||||||
throw "Error, must be set for readlines";
|
throw invalid_argument ("Error, must be set for readlines");
|
||||||
}
|
}
|
||||||
size_t leneol = eol.length ();
|
size_t leneol = eol.length ();
|
||||||
vector<string> lines;
|
vector<string> lines;
|
||||||
@ -339,3 +339,4 @@ bool Serial::getCD ()
|
|||||||
{
|
{
|
||||||
return pimpl_->getCD ();
|
return pimpl_->getCD ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
tests/proof_of_concepts/python_serial_test.py
Normal file
15
tests/proof_of_concepts/python_serial_test.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import serial, sys
|
||||||
|
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print "python: Usage_serial_test <port name like: /dev/ttyUSB0>"
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
sio = serial.Serial(sys.argv[1], 115200)
|
||||||
|
sio.timeout = 250
|
||||||
|
|
||||||
|
while True:
|
||||||
|
sio.write("Testing.")
|
||||||
|
print sio.read(8)
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user