30 #include <boost/format.hpp> 31 #include <boost/asio.hpp> 54 port = open(devname.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
58 throw std::runtime_error((boost::format(
"Failed to open serial port [%s]\n") % devname).str());
62 fcntl(
port, F_SETFL, 0);
64 struct termios port_settings;
66 cfsetispeed(&port_settings,
getSpeed(speed));
67 cfsetospeed(&port_settings,
getSpeed(speed));
69 cfmakeraw(&port_settings);
72 port_settings.c_cflag |= PARENB;
74 port_settings.c_cflag &= ~PARODD;
77 port_settings.c_cflag |= PARODD;
80 else port_settings.c_cflag &= ~PARENB;
84 port_settings.c_cflag &= ~CSTOPB;
85 port_settings.c_cflag &= ~CSIZE;
86 port_settings.c_cflag |= CS8;
88 port_settings.c_lflag &= ~ECHO;
99 port_settings.c_lflag &= ~ICANON;
101 port_settings.c_cc[VMIN] = 0;
102 port_settings.c_cc[VTIME] = 0;
104 tcsetattr(
port, TCSANOW, &port_settings);
112 write(
port, str.c_str(), str.size());
122 str = std::string(
"");
128 len = read(
port, &c, 1);
132 case '\n':
return true;
138 std::cerr << boost::format(
"read returned %d errno = %d\n")
144 if (itercount > 100000) {
145 std::cerr << itercount << std::endl;
155 case 9600:
return B9600;
156 case 2400:
return B2400;
157 case 4800:
return B4800;
158 case 19200:
return B19200;
159 case 38400:
return B38400;
160 case 57600:
return B57600;
161 case 115200:
return B115200;
162 default:
return B9600;
168 tcflush(
port, TCIFLUSH);
173 tcflush(
port, TCOFLUSH);
181 int stlen = str.size();
183 std::string exp = (boost::format(
"OK [%s]") % (str.substr(0, stlen-1))).str();
187 for(i = 0; i < 10; i++) {
188 std::cerr << boost::format(
"loop %d start\n") % i;
193 if((v = exp.compare(0, stlen + 3, resp)) == 0) {
194 std::cerr <<
"YAY!" << std::endl;
198 std::cerr << boost::format(
"Expected [%s] got [%s]. stlen = %d compare = %d\n")
199 % exp % resp % stlen % v;
201 for(j = 0; j < stlen+5; j++) {
202 int cmp = (exp[i] == resp[i]);
203 std::cerr << boost::format(
"%c %c (%d %d) =? %d\n")
204 % exp[j] % resp[j] % ((int) exp[j]) % ((int) resp[j]) % cmp;
207 if(resp.compare(0, 3,
"BAD") == 0) {
208 std::cerr <<
"flushing input buffer" << std::endl;
211 std::cerr << boost::format(
"Resend [%s]\n") % str;
213 std::cerr << boost::format(
"loop %d end\n") % i;
bool getString(std::string &str, unsigned int maxlen)
SerialDev(const std::string &devname, unsigned int speed=9600, bool par_ena=false, bool par_even=true)
bool palindromeCommand(std::string &str)
bool putString(const std::string &str)