4 using namespace controller;
10 : io_(),serial_(io_),timer_(io_),is_canceled_(false),comm_err_(false)
23 boost::system::error_code error_code;
29 serial_.set_option(serial_port_base::baud_rate(_baud_rate));
43 serial_.async_write_some( buffer( _send_data ), [](boost::system::error_code, std::size_t){});
51 if (_error && _error != boost::asio::error::eof) {
53 std::cout <<
"receive failed: " << std::endl;
80 boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
85 timer_.expires_from_now(boost::posix_time::milliseconds(_timeout));
94 _receive_data.clear();
95 _receive_data.resize(_length);
96 fill(_receive_data.begin(),_receive_data.end(),0);
101 std::cerr <<
"Read Timeout" << std::endl;
114 ::tcflush(
serial_.lowest_layer().native_handle(),TCIOFLUSH);
121 :is_open_(false),check_sum_(0),count_(0),length_(0),serial_com_()
160 send_data_[1] = 0xDF;
162 send_data_[3] = 0x01;
163 send_data_[4] = _number;
165 for(
unsigned int i = 0;i < (
length_-6)/2; ++i){
166 send_data_[i*2+5] = _max;
167 send_data_[i*2+6] = _down;
191 send_data_[1] = 0xDF;
193 send_data_[3] = 0x21;
194 send_data_[4] = _number;
196 for(
unsigned int i = 0;i < (
length_-6)/2; ++i){
197 send_data_[i*2+5] = _data >> 8;
198 send_data_[i*2+6] = _data;
221 send_data_[1] = 0xDF;
223 send_data_[3] = 0x41;
224 send_data_[4] = _number;
233 std::vector<uint8_t> receive_data;
234 if(_number == 0) receive_data.resize(68);
235 else receive_data.resize(8);
236 fill(receive_data.begin(),receive_data.end(),0);
240 std::vector<int16_t> parse_data;
241 if(_number==0) parse_data.resize(30);
242 else parse_data.resize(1);
243 fill(parse_data.begin(),parse_data.end(),0);
244 for(
size_t i=0; i < parse_data.size() ; ++i){
245 parse_data[i] =
static_cast<int16_t
>((receive_data[i*2+5] << 8) + receive_data[i*2+6]);
262 send_data_[1] = 0xDF;
264 send_data_[3] = 0x42;
265 send_data_[4] = _number;
274 std::vector<uint8_t> receive_data;
275 if(_number == 0) receive_data.resize(68);
276 else receive_data.resize(8);
277 fill(receive_data.begin(),receive_data.end(),0);
281 std::vector<uint16_t> parse_data;
282 if(_number==0) parse_data.resize(31);
283 else parse_data.resize(1);
284 fill(parse_data.begin(),parse_data.end(),0);
285 for(
size_t i=0; i < parse_data.size() ; ++i){
286 parse_data[i] =
static_cast<uint16_t
>((receive_data[i*2+5] << 8) + receive_data[i*2+6]);
303 send_data_[1] = 0xDF;
305 send_data_[3] = 0x43;
306 send_data_[4] = _number;
315 std::vector<uint8_t> receive_data;
316 if(_number == 0) receive_data.resize(68);
317 else receive_data.resize(8);
318 fill(receive_data.begin(),receive_data.end(),0);
322 std::vector<uint16_t> parse_data;
323 if(_number==0) parse_data.resize(31);
324 else parse_data.resize(1);
325 fill(parse_data.begin(),parse_data.end(),0);
326 for(
size_t i=0; i < parse_data.size() ; ++i){
327 parse_data[i] =
static_cast<uint16_t
>((receive_data[i*2+5] << 8) + receive_data[i*2+6]);
344 send_data_[1] = 0xDF;
346 send_data_[3] = 0x51;
347 send_data_[4] = _number;
356 std::vector<uint8_t> receive_data;
357 receive_data.resize(11);
358 fill(receive_data.begin(),receive_data.end(),0);
363 std::string version =
"";
365 for(
size_t i=0; i < 5 ; ++i){
366 sprintf(data,
"%02X", receive_data[i+5]);
383 send_data_[1] = 0xDF;
385 send_data_[3] = 0x52;
386 send_data_[4] = _number;
395 std::vector<uint8_t> receive_data;
396 if(_number == 0) receive_data.resize(68);
397 else receive_data.resize(8);
398 fill(receive_data.begin(),receive_data.end(),0);
403 std::vector<uint16_t> parse_data;
404 if(_number==0) parse_data.resize(31);
405 else parse_data.resize(1);
406 fill(parse_data.begin(),parse_data.end(),0);
407 for(
size_t i=0; i < parse_data.size() ; ++i){
408 parse_data[i] =
static_cast<uint16_t
>((receive_data[i*2+5] << 8) + receive_data[i*2+6]);
416 uint8_t _data1, uint8_t _data2, uint8_t _data3, uint8_t _data4, uint8_t _data5)
425 send_data_[1] = 0xDF;
427 send_data_[3] = 0x5F;
428 send_data_[4] = _send_no;
430 send_data_[5] = _command;
431 send_data_[6] = _data1;
432 send_data_[7] = _data2;
433 send_data_[8] = _data3;
434 send_data_[9] = _data4;
435 send_data_[10] = _data5;
455 send_data_[1] = 0xDF;
457 send_data_[3] = 0x14;
458 send_data_[4] = 0x00;
460 for (
int i = 0; i < 30; i++){
461 send_data_[i*2 + 5] = _data[i] >> 8;
462 send_data_[i*2 + 6] = _data[i] ;
466 send_data_[65] = _time >>8;
467 send_data_[66] = _time;
476 std::vector<uint8_t> receive_data;
477 receive_data.resize(68);
478 fill(receive_data.begin(),receive_data.end(),0);
483 std::vector<int16_t> parse_data;
484 parse_data.resize(31);
485 fill(parse_data.begin(),parse_data.end(),0);
486 for(
size_t i=0; i < parse_data.size() ; ++i){
487 parse_data[i] =
static_cast<int16_t
>((receive_data[i*2+5] << 8) + receive_data[i*2+6]);
503 send_data_[1] = 0xDF;
505 send_data_[3] = 0x15;
506 send_data_[4] = 0x00;
508 for (
int i = 0; i < 30; i++){
509 send_data_[i*2 + 5] = _data[i] >> 8;
510 send_data_[i*2 + 6] = _data[i] ;
520 std::vector<uint8_t> receive_data;
521 receive_data.resize(68);
522 fill(receive_data.begin(),receive_data.end(),0);
527 std::vector<int16_t> parse_data;
528 parse_data.resize(31);
529 fill(parse_data.begin(),parse_data.end(),0);
530 for(
size_t i=0; i < parse_data.size() ; ++i){
531 parse_data[i] =
static_cast<int16_t
>((receive_data[i*2+5] << 8) + receive_data[i*2+6]);
550 send_data_[1] = 0xDF;
552 send_data_[3] = 0x22;
553 send_data_[4] = _number;
555 for(
unsigned int i = 0;i < (
length_-6)/2;++i) send_data_[i*2+6] = _data;
void readBufferAsync(uint8_t _size, uint16_t _timeout)
std::string receive_buffer_
void writeAsync(std::vector< uint8_t > &_send_data)
std::vector< uint16_t > getStatus(uint8_t _number)
void readBuffer(std::vector< uint8_t > &_receive_data, uint8_t _size)
std::vector< uint16_t > getTemperatureVoltage(uint8_t _number)
std::vector< int16_t > actuateByPosition(uint16_t _time, int16_t *_data)
std::string getVersion(uint8_t _number)
std::vector< int16_t > getPosition(uint8_t _number)
void runScript(uint8_t _number, uint16_t _data)
void onTimer(const boost::system::error_code &_error)
void throughCAN(uint8_t _send_no, uint8_t _command, uint8_t _data1, uint8_t _data2, uint8_t _data3, uint8_t _data4, uint8_t _data5)
bool openPort(std::string _port, unsigned int _baud_rate)
boost::asio::streambuf stream_buffer_
bool openPort(std::string _port, unsigned int _baud_rate)
void setCurrent(uint8_t _number, uint8_t _max, uint8_t _down)
std::vector< uint16_t > getCurrent(uint8_t _number)
void onReceive(const boost::system::error_code &_error, size_t _bytes_transferred)
std::vector< uint8_t > send_data_
SerialCommunication serial_com_
std::vector< int16_t > actuateBySpeed(int16_t *_data)
void onServo(uint8_t _number, uint16_t _data)