00001 /* 00002 * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA) 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 00018 #ifndef SERIALIO_H 00019 #define SERIALIO_H 00020 00021 // serial connection includes 00022 #include <unistd.h> 00023 #include <fcntl.h> 00024 #include <errno.h> 00025 #include <termios.h> 00026 #include <time.h> 00027 #include <sys/time.h> 00028 00029 #include <string> 00030 00031 #include <concurrentQueue.h> 00032 #include <boost/thread.hpp> 00033 00034 typedef struct ioData{ 00035 const char* buf; 00036 size_t len; 00037 } ioData_t; 00038 00039 class SerialIO 00040 { 00041 public: 00042 // Constructor 00043 SerialIO(); 00044 // Destructor 00045 ~SerialIO(); 00046 00047 // Open Serial Port 00048 int openPort(std::string devicestring, int baudrate); 00049 00050 // Send Data to Serial Port 00051 int sendData(std::string value); 00052 00053 // Send Data to Serial Port 00054 int sendData(const char* data, size_t len); 00055 00056 // Read Data from Serial Port 00057 int readData(std::string &value, size_t nBytes); 00058 00059 void enqueueData(std::vector<ioData_t> data); 00060 00061 void enqueueData(const char* data, size_t len); 00062 00063 // Check if Serial Port is opened 00064 bool isOpen(); 00065 00066 // Close Serial Port 00067 void closePort(); 00068 00069 bool recover(); 00070 00071 void start(); 00072 void stop(); 00073 00074 private: 00075 //ioQueue 00076 ConcurrentQueue<std::vector<struct ioData> > _oQueue; 00077 00078 boost::shared_ptr<boost::thread> _thread; 00079 boost::mutex _mutex; 00080 boost::condition_variable _condition; 00081 00082 // filedescriptor 00083 int _fd; 00084 // serial port settings 00085 struct termios port_settings; 00086 // device string 00087 std::string _device_string; 00088 // baudrate 00089 int _baudrate; 00090 00091 // resolve int to baudrate 00092 speed_t getBaudFromInt(int baud); 00093 00094 static const int maxUpdateRate = 50; 00095 00096 void run(); 00097 }; 00098 00099 #endif