18 #ifndef HAND_BRIDGE_SERIAL_PORT_H_ 19 #define HAND_BRIDGE_SERIAL_PORT_H_ 23 #include <boost/lexical_cast.hpp> 24 #include <boost/chrono.hpp> 25 #include <boost/thread.hpp> 35 unsigned int baud = 57600;
36 if(
char * delim = strchr(param,
'@')){
38 baud = boost::lexical_cast<
unsigned int>(delim + 1);
40 ser_port =
serOpen(param, baud, 0);
41 printf(
"%s opened as %d\n", param, ser_port);
45 template<
typename Duration>
int waitData(
const Duration& duration){
46 boost::chrono::steady_clock::time_point deadline = boost::chrono::steady_clock::now() + duration;
48 while((len =
serDataAvailable(ser_port)) == 0 && boost::chrono::steady_clock::now() < deadline)
49 boost::this_thread::sleep_for(boost::chrono::milliseconds(1));
56 int read(
char *buf,
unsigned count){
57 return serRead(ser_port, buf, count);
59 bool write(uint8_t* data,
int length)
61 return serWrite(ser_port, reinterpret_cast<char*>(data), length) == 0;
63 bool write(
const std::string &line){
64 return serWrite(ser_port, const_cast<char *>(line.c_str()), line.size()) == 0;
76 #endif // HAND_BRIDGE_SERIAL_PORT_H_ bool write(uint8_t *data, int length)
int read(char *buf, unsigned count)
int serRead(unsigned handle, char *buf, unsigned count)
int waitData(const Duration &duration)
int serDataAvailable(unsigned handle)
int serClose(unsigned handle)
bool write(const std::string &line)
int serWrite(unsigned handle, char *buf, unsigned count)
int serReadByte(unsigned handle)
int serOpen(char *tty, unsigned serBaud, unsigned serFlags)