Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00029
00030 #include "driver_svh/SVHSerialInterface.h"
00031 #include "driver_svh/Logging.h"
00032
00033 #include <icl_comm/ByteOrderConversion.h>
00034
00035 using icl_core::TimeSpan;
00036 using icl_comm::serial::SerialFlags;
00037
00038 namespace driver_svh {
00039
00040 SVHSerialInterface::SVHSerialInterface(ReceivedPacketCallback const & received_packet_callback) :
00041 m_connected(false),
00042 m_received_packet_callback(received_packet_callback),
00043 m_packets_transmitted(0)
00044 {
00045 }
00046
00047 SVHSerialInterface::~SVHSerialInterface()
00048 {
00049
00050 }
00051
00052 bool SVHSerialInterface::connect(const std::string &dev_name)
00053 {
00054
00055 close();
00056
00057
00058 m_serial_device.reset(new Serial(dev_name.c_str(), SerialFlags(SerialFlags::eBR_921600, SerialFlags::eDB_8)));
00059
00060 if (m_serial_device)
00061 {
00062
00063 if (!m_serial_device->Open())
00064 {
00065 LOGGING_ERROR_C(DriverSVH, SVHSerialInterface, "Could not open serial device: " << dev_name.c_str() << endl);
00066 return false;
00067 }
00068 }
00069 else
00070 {
00071 LOGGING_ERROR_C(DriverSVH, SVHSerialInterface, "Could not create serial device handle: " << dev_name.c_str() << endl);
00072 return false;
00073 }
00074
00075
00076 m_receive_thread.reset(new SVHReceiveThread(TimeSpan(0, 500000), m_serial_device, m_received_packet_callback));
00077
00078 if (m_receive_thread)
00079 {
00080
00081 if (!m_receive_thread->start())
00082 {
00083 LOGGING_ERROR_C(DriverSVH, SVHSerialInterface, "Could not start the receive thread for the serial device!" << endl);
00084 return false;
00085 }
00086 }
00087 else
00088 {
00089 LOGGING_ERROR_C(DriverSVH, SVHSerialInterface, "Could not create the receive thread for the serial device!" << endl);
00090 return false;
00091 }
00092
00093 m_connected = true;
00094 LOGGING_TRACE_C(DriverSVH, SVHSerialInterface, "Serial device " << dev_name.c_str() << " opened and receive thread started. Communication can now begin." << endl);
00095
00096 return true;
00097 }
00098
00099 void SVHSerialInterface::close()
00100 {
00101 m_connected = false;
00102
00103
00104 if (m_receive_thread)
00105 {
00106
00107 m_receive_thread->stop();
00108 m_receive_thread->join();
00109
00110 m_receive_thread.reset();
00111
00112 LOGGING_TRACE_C(DriverSVH, SVHSerialInterface, "Serial device receive thread was terminated." << endl);
00113 }
00114
00115
00116 if (m_serial_device)
00117 {
00118 m_serial_device->Close();
00119
00120 m_serial_device.reset();
00121 LOGGING_TRACE_C(DriverSVH, SVHSerialInterface, "Serial device handle was closed and terminated." << endl);
00122 }
00123 }
00124
00125 bool SVHSerialInterface::sendPacket(SVHSerialPacket& packet)
00126 {
00127 if (m_serial_device != NULL)
00128 {
00129 uint8_t check_sum1 = 0;
00130 uint8_t check_sum2 = 0;
00131
00132
00133 for (size_t i = 0; i < packet.data.size(); i++)
00134 {
00135 check_sum1 += packet.data[i];
00136 check_sum2 ^= packet.data[i];
00137 }
00138
00139
00140 packet.index = static_cast<uint8_t>(m_packets_transmitted % uint8_t(-1));
00141
00142 if (m_serial_device->IsOpen())
00143 {
00144
00145 size_t size = packet.data.size() + cPACKET_APPENDIX_SIZE;
00146 icl_comm::ArrayBuilder send_array(size);
00147
00148 send_array << PACKET_HEADER1 << PACKET_HEADER2 << packet << check_sum1 << check_sum2;
00149
00150
00151 size_t bytes_send = 0;
00152 while (bytes_send < size)
00153 {
00154 bytes_send += m_serial_device->Write(send_array.array.data() + bytes_send, size - bytes_send);
00155 }
00156
00157
00158
00159 icl_core::os::usleep(8000);
00160
00161 }
00162 else
00163 {
00164 LOGGING_TRACE_C(DriverSVH, SVHSerialInterface, "sendPacket failed, serial device was not properly initialized." << endl);
00165 return false;
00166 }
00167
00168 m_packets_transmitted++;
00169 }
00170
00171 return true;
00172 }
00173
00174 void SVHSerialInterface::resetTransmitPackageCount()
00175 {
00176 m_packets_transmitted = 0;
00177
00178 m_receive_thread->resetReceivedPackageCount();
00179 }
00180
00181 void SVHSerialInterface::printPacketOnConsole(SVHSerialPacket &packet)
00182 {
00183
00184 uint8_t check_sum1 = 0;
00185 uint8_t check_sum2 = 0;
00186
00187
00188 for (size_t i = 0; i < packet.data.size(); i++)
00189 {
00190 check_sum1 += packet.data[i];
00191 check_sum2 ^= packet.data[i];
00192 }
00193
00194
00195 packet.index = static_cast<uint8_t>(m_dummy_packets_printed % uint8_t(-1));
00196
00197
00198
00199 size_t size = packet.data.size() + cPACKET_APPENDIX_SIZE;
00200 icl_comm::ArrayBuilder send_array(size);
00201
00202 send_array << PACKET_HEADER1 << PACKET_HEADER2 << packet << check_sum1 << check_sum2;
00203
00204 std::cout << send_array << std::endl;
00205
00206 m_dummy_packets_printed++;
00207 }
00208
00209 }