Go to the documentation of this file.00001
00002
00003
00004
00005 #include "robotican_hardware_interface/TransportLayer.h"
00006
00007 TransportLayer::TransportLayer(std::string port, unsigned int baudrate) : _ioService(), _serial(_ioService, port) {
00008 crcInit();
00009 _serial.set_option(boost::asio::serial_port_base::baud_rate(baudrate));
00010
00011
00012
00013
00014
00015 }
00016
00017 bool TransportLayer::read(byte *buff, byte buffLength) {
00018 byte size[1] = {0};
00019 byte message[128] = {0};
00020
00021 boost::asio::read(_serial, boost::asio::buffer(size, 1));
00022
00023 if(size[0] < buffLength) {
00024 boost::asio::read(_serial, boost::asio::buffer(message, size[0] - 1));
00025 buff[0] = size[0];
00026 for(int i = 1; i <= size[0] - 1; ++i) {
00027 buff[i] = message[i-1];
00028
00029 }
00030 return true;
00031 }
00032 return false;
00033 }
00034
00035 void TransportLayer::crcInit() {
00036
00037 crc remainder;
00038
00039
00040
00041 for (int dividend = 0; dividend < 256; ++dividend)
00042 {
00043
00044
00045
00046 remainder = dividend << (WIDTH - 8);
00047
00048
00049
00050
00051 for (uint8_t bit = 8; bit > 0; --bit)
00052 {
00053
00054
00055
00056 if (remainder & TOPBIT)
00057 {
00058 remainder = (remainder << 1) ^ POLYNOMIAL;
00059 }
00060 else
00061 {
00062 remainder = (remainder << 1);
00063 }
00064 }
00065
00066
00067
00068
00069 _crcTable[dividend] = remainder;
00070 }
00071
00072 }
00073
00074 bool TransportLayer::tryToRead(byte *buff, byte buffLength) {
00075 try {
00076 byte headerSignal[1] = {0};
00077 boost::asio::read(_serial, boost::asio::buffer(headerSignal, 1));
00078 if (headerSignal[0] == HEADER_SIGNAL) {
00079 return read(buff, buffLength);
00080 }
00081 }
00082 catch (boost::system::system_error const &e) {
00083 char errorBuff[128] = {'\0'};
00084 sprintf(errorBuff, "RiCBoard disconnected, Exception is: { %s }", e.what());
00085 ros_utils::rosError(errorBuff);
00086 ros::shutdown();
00087 }
00088 return false;
00089 }
00090
00091 void TransportLayer::write(byte *buff, byte buffLength) {
00092 try {
00093 byte headerSignal[1];
00094 headerSignal[0] = HEADER_SIGNAL;
00095 boost::asio::write(_serial, boost::asio::buffer(headerSignal, 1));
00096 boost::asio::write(_serial, boost::asio::buffer(buff, buffLength));
00097 }
00098 catch(boost::system::system_error const &e) {
00099 char errorBuff[128] = {'\0'};
00100 sprintf(errorBuff, "RiCBoard disconnected, Exception is: { %s }", e.what());
00101 ros_utils::rosError(errorBuff);
00102 ros::shutdown();
00103 }
00104 }
00105
00106 crc TransportLayer::calcChecksum(uint8_t const message[], int nBytes) {
00107 uint8_t data;
00108 crc remainder = 0;
00109
00110
00111
00112
00113 for (int byte = 0; byte < nBytes; ++byte)
00114 {
00115 data = message[byte] ^ (remainder >> (WIDTH - 8));
00116 remainder = _crcTable[data] ^ (remainder << 8);
00117 }
00118
00119
00120
00121
00122 return (remainder);
00123 }
00124
00125 TransportLayer::~TransportLayer() {
00126 _serial.close();
00127
00128 }