Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include "serialIO.h"
00056 #include "sys/select.h"
00057 #include <iostream>
00058 #include <cstring>
00059
00060 SerialIO::SerialIO() :
00061 _fd(-1)
00062 {
00063 }
00064
00065 SerialIO::~SerialIO()
00066 {
00067 closePort();
00068 }
00069
00070
00071 int SerialIO::openPort(std::string devicestring, int baudrate)
00072 {
00073 if(_fd != -1) return _fd;
00074
00075 speed_t baud = getBaudFromInt(baudrate);
00076 std::memset(&port_settings,0,sizeof(port_settings));
00077 port_settings.c_iflag = 0;
00078 port_settings.c_oflag = 0;
00079 port_settings.c_cflag = CS8|CREAD|CLOCAL;
00080 port_settings.c_lflag = 0;
00081 port_settings.c_cc[VMIN]=1;
00082 port_settings.c_cc[VTIME]=5;
00083
00084 _fd=open(devicestring.c_str(), O_RDWR | O_NONBLOCK);
00085 cfsetospeed(&port_settings, baud);
00086 cfsetispeed(&port_settings, baud);
00087
00088 tcsetattr(_fd, TCSANOW, &port_settings);
00089
00090 return _fd;
00091 }
00092
00093
00094 int SerialIO::sendData(std::string value)
00095 {
00096 int wrote = -1;
00097 if(_fd != -1)
00098 wrote = write(_fd, value.c_str(), value.length());
00099 return wrote;
00100 }
00101
00102
00103 int SerialIO::sendData(const char* data, size_t len)
00104 {
00105 int wrote = -1;
00106 if(_fd != -1)
00107 wrote = write(_fd, data, len);
00108 return wrote;
00109 }
00110
00111
00112 int SerialIO::readData(std::string &value, size_t nBytes)
00113 {
00114 fd_set fds;
00115 FD_ZERO(&fds);
00116 FD_SET(_fd, &fds);
00117 struct timeval timeout = {0, 100000};
00118 char buffer[32];
00119 size_t rec = -1;
00120 if(select(_fd+1, &fds, NULL, NULL, &timeout))
00121 {
00122 rec = read(_fd, buffer, nBytes);
00123 value = std::string(buffer);
00124 }
00125
00126 return rec;
00127 }
00128
00129
00130 bool SerialIO::isOpen()
00131 {
00132 return (_fd != -1);
00133 }
00134
00135
00136 void SerialIO::closePort()
00137 {
00138 if(_fd != -1)
00139 close(_fd);
00140 }
00141
00142
00143 speed_t SerialIO::getBaudFromInt(int baud)
00144 {
00145 speed_t ret;
00146 switch(baud)
00147 {
00148 case 0: ret=B0; break;
00149 case 50: ret=B50; break;
00150 case 75: ret=B75; break;
00151 case 110: ret=B110; break;
00152 case 134: ret=B134; break;
00153 case 150: ret=B150; break;
00154 case 200: ret=B200; break;
00155 case 300: ret=B300; break;
00156 case 1200: ret=B1200; break;
00157 case 1800: ret=B1800; break;
00158 case 2400: ret=B2400; break;
00159 case 4800: ret=B4800; break;
00160 case 9600: ret=B9600; break;
00161 case 19200: ret=B19200; break;
00162 case 38400: ret=B38400; break;
00163 case 57600: ret=B57600; break;
00164 case 115200: ret=B115200; break;
00165 case 230400: ret=B230400; break;
00166 default:
00167 ret=B230400;
00168 break;
00169 }
00170 return ret;
00171 }