55 std::string device_port;
57 device_port =
"/dev/";
58 sub_path = path.substr(0, path.find(
":"));
59 device_port += sub_path;
60 serialfd = open(device_port.c_str(), O_RDWR);
64 std::cout <<
"Port not open. Unable to open port " << device_port
73 std::cout <<
"Port " << device_port <<
" is open.\n";
77 std::cout <<
"Error configuring port, check specific output."
100 int s_bytes = write(
serialfd, msg.c_str(), msg.size());
101 if (s_bytes >=
static_cast<int>(msg.size())) {
112 int n_bytes = read(
serialfd, wpos, maxlen);
128 std::size_t curs = 0, curs_d = 0;
130 memset(&
tty, 0,
sizeof(
tty));
139 while ((curs = path.find(
":", curs)) != std::string::npos) {
145 std::cout <<
"ERROR: too few number of parameters for serial port."
147 std::cout <<
"num_param: " << num_param << std::endl;
149 }
else if (num_param > 4) {
150 std::cout <<
"ERROR: too many params for serial port. Check path or "
151 "add options in code."
153 std::cout <<
"num_param: " << num_param << std::endl;
157 curs_d = path.find(
":");
158 curs = path.find(
":") + 1;
160 while ((curs_d = path.find(
":", curs_d)) != std::string::npos) {
161 if (path.at(curs) ==
'p') {
162 if (path.at(curs + 2) ==
'0') {
163 tty.c_cflag &= ~PARENB;
164 }
else if (path.at(curs + 2) ==
'1') {
165 tty.c_cflag |= PARENB;
167 }
else if (path.at(curs) ==
's') {
168 if (path.at(curs + 2) ==
'0') {
169 tty.c_cflag &= ~CSTOPB;
170 }
else if (path.at(curs + 2) ==
'1') {
171 tty.c_cflag |= CSTOPB;
173 }
else if (path.at(curs) ==
'f') {
174 if (path.at(curs + 2) ==
'0') {
175 tty.c_cflag &= ~CRTSCTS;
176 }
else if (path.at(curs + 2) ==
'1') {
177 tty.c_cflag |= CRTSCTS;
179 }
else if (path.at(curs) ==
'b') {
180 sub = path.substr(curs + 2, (path.find(
":", curs) - (curs + 2)));
181 baud = std::stoi(sub);
183 if (baud == 115200) {
184 if ((cfsetispeed(&
tty, B115200)) != 0 &&
185 (cfsetospeed(&
tty, B115200) != 0)) {
186 std::cout <<
"Baud(" << baud <<
") is not a valid speed."
189 }
else if (baud == 1200) {
190 if ((cfsetispeed(&
tty, B1200)) != 0 &&
191 (cfsetospeed(&
tty, B1200) != 0)) {
192 std::cout <<
"Baud(" << baud <<
") is not a valid speed."
195 }
else if (baud == 2400) {
196 if ((cfsetispeed(&
tty, B2400)) != 0 &&
197 (cfsetospeed(&
tty, B2400) != 0)) {
198 std::cout <<
"Baud(" << baud <<
") is not a valid speed."
201 }
else if (baud == 4800) {
202 if ((cfsetispeed(&
tty, B4800)) != 0 &&
203 (cfsetospeed(&
tty, B4800) != 0)) {
204 std::cout <<
"Baud(" << baud <<
") is not a valid speed."
207 }
else if (baud == 19200) {
208 if ((cfsetispeed(&
tty, B19200)) != 0 &&
209 (cfsetospeed(&
tty, B19200) != 0)) {
210 std::cout <<
"Baud(" << baud <<
") is not a valid speed."
213 }
else if (baud == 38400) {
214 if ((cfsetispeed(&
tty, B38400)) != 0 &&
215 (cfsetospeed(&
tty, B38400) != 0)) {
216 std::cout <<
"Baud(" << baud <<
") is not a valid speed."
219 }
else if (baud == 57600) {
220 if ((cfsetispeed(&
tty, B57600)) != 0 &&
221 (cfsetospeed(&
tty, B57600) != 0)) {
222 std::cout <<
"Baud(" << baud <<
") is not a valid speed."
226 std::cout <<
"Baud rate [" << baud <<
"] not valid" << std::endl;
230 std::cout <<
"ERROR: flag not defined." << std::endl;
232 curs = path.find(
":", curs + 1) + 1;
238 tty.c_cflag |= CREAD | CLOCAL;
239 tty.c_lflag &= ~ICANON;
240 tty.c_lflag &= ~ECHO;
241 tty.c_lflag &= ~ECHOE;
242 tty.c_lflag &= ~ECHONL;
243 tty.c_lflag &= ~ISIG;
244 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
245 tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
246 tty.c_oflag &= ~OPOST;
247 tty.c_oflag &= ~ONLCR;
249 tty.c_cc[VTIME] = 10;
253 std::cout <<
"ERROR: Port configuration error." << std::endl;
268 std::cout <<
"Connection not established, cannot refresh." << std::endl;
Class UwConnector allows to specify an interface between the UwDriver object and the device.
UwSerial()
Constructor of the UwSerial class.
virtual int configurePort(const std::string &path)
Method that loads the termios struct with the serial port parameters.
virtual ~UwSerial()
Destructor of the UwSerial class.
virtual const bool isConnected()
Returns true if serial port fd differs from -1, that means the connection is up.
virtual int writeToDevice(const std::string &msg)
Method that writes a command to the port interface.
int serialfd
Integer value that stores the serial port descriptor as generated by the function UwSerial:openConnec...
virtual bool closeConnection()
Method that closes an active connection to a device.
virtual bool openConnection(const std::string &path)
Method that opens a serial connection, the input string has to be with a format like <port_name>:pari...
virtual bool refreshConnection(const std::string &path)
Method that refreshes an existing connection creating a new file descriptor.
virtual int readFromDevice(void *wpos, int maxlen)
Function that receives data from the device's port to a backup buffer.
This implements a generic serial connector .