serialDevice_lin.cpp
00001
00002
00003
00004
00005
00006
00007 #include "serialDevice.h"
00008 #include <string.h>
00009 #include <unistd.h>
00010 #include <stdio.h>
00011 #include <fcntl.h>
00012
00013
00014 SerialDevice::~SerialDevice(void) {
00015 raw_disconnect();
00016 }
00017
00018
00019 const char* SerialDevice::what(void) {
00020 return error_message.c_str();
00021 }
00022
00023
00024 SerialDevice::SerialDevice(int buffer_size)
00025 : ConnectionDevice(buffer_size), fd(-1),
00026 error_message("Connection device is not specified") {
00027 }
00028
00029
00030 int SerialDevice::raw_connect(const char* device, long baudrate) {
00031
00032 fd = open(device, O_RDWR | O_SYNC);
00033 if (fd < 0) {
00034 perror(device);
00035 error_message = std::string("Couldn't open device: ") + device;
00036 return DeviceOpenFail;
00037 }
00038
00039 tcgetattr(fd, &sio);
00040 sio.c_iflag = IGNPAR;
00041 sio.c_oflag = 0;
00042 sio.c_cflag = CS8 | CREAD | CLOCAL;
00043 sio.c_lflag = 0;
00044
00045 sio.c_cc[VMIN] = 0;
00046 sio.c_cc[VTIME] = 0;
00047 tcsetattr(fd, TCSAFLUSH, &sio);
00048
00049
00050 nfds.fd = fd;
00051 nfds.events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
00052 nfds.revents = 0;
00053
00054 error_message = "Success";
00055 return 0;
00056 }
00057
00058
00059 void SerialDevice::raw_disconnect(void) {
00060 close(fd);
00061 fd = -1;
00062 }
00063
00064
00065 bool SerialDevice::isConnected(void) {
00066 return (fd < 0) ? false : true;
00067 }
00068
00069
00070 int SerialDevice::raw_setBaudrate(long baudrate) {
00071 long baudrate_value;
00072 switch (baudrate) {
00073
00074 case 9600:
00075 baudrate_value = B9600;
00076 break;
00077
00078 case 19200:
00079 baudrate_value = B19200;
00080 break;
00081
00082 case 38400:
00083 baudrate_value = B38400;
00084 break;
00085
00086 case 57600:
00087 baudrate_value = B57600;
00088 break;
00089
00090 case 115200:
00091 baudrate_value = B115200;
00092 break;
00093
00094 case B230400:
00095 baudrate_value = B230400;
00096 break;
00097
00098 default:
00099 char* message = new char [80 + 12];
00100 sprintf(message, "Invalid baudrate value: %ld", baudrate);
00101 error_message = message;
00102 delete [] message;
00103 return InvalidBaudrate;
00104 break;
00105 }
00106
00107 cfsetospeed(&sio, baudrate_value);
00108 cfsetispeed(&sio, baudrate_value);
00109 tcsetattr(fd, TCSADRAIN, &sio);
00110
00111 error_message = "Success";
00112 return 0;
00113 }
00114
00115
00116 int SerialDevice::raw_send(const char* data, int len) {
00117 #if 0
00118 for (int i = 0; i < len; ++i) {
00119 if (data[i] == '\r') {
00120 fprintf(stderr, "\n");
00121 } else {
00122 fprintf(stderr, "%c", data[i]);
00123 }
00124 }
00125 #endif
00126 return write(fd, data, len);
00127 }
00128
00129
00130 void SerialDevice::raw_flush(void) {
00131 #if 0
00132 tcflush(fd, TCIOFLUSH);
00133 raw_check(BufferSize, 0);
00134 recv_buffer->clear();
00135 #endif
00136
00137 tcflush(fd, TCIOFLUSH);
00138 do {
00139 recv_buffer->clear();
00140 raw_check(BufferSize, 10);
00141 } while (recv_buffer->size() > 0);
00142 }
00143
00144
00145 void SerialDevice::raw_check(int size, long timeout) {
00146
00147 int filled = 0;
00148 while (filled < size) {
00149 if (poll(&nfds, 1, timeout) <= 0) {
00150 break;
00151 }
00152 char buffer[BufferSize];
00153 int n = read(fd, buffer, size);
00154
00155 #if 0
00156 for (int i = 0; i < n; ++i) {
00157 if (buffer[i] == '\r') {
00158 fprintf(stderr, "\n");
00159 } else {
00160 fprintf(stderr, "%c", buffer[i]);
00161 }
00162 }
00163 #endif
00164
00165 if (n <= 0) {
00166 return;
00167 }
00168 recv_buffer->put(buffer, n);
00169 filled += n;
00170 }
00171 }
00172