uxbus_cmd_ser.cc
Go to the documentation of this file.
1 /* Copyright 2017 UFACTORY Inc. All Rights Reserved.
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Author: Jimy Zhang <jimy92@163.com>
6  ============================================================================*/
7 
12 
13 
14 UxbusCmdSer::UxbusCmdSer(SerialPort *arm_port) { arm_port_ = arm_port; }
16 
17 int UxbusCmdSer::check_xbus_prot(unsigned char *datas, int funcode) {
18  state_is_ready = !(datas[3] & 0x10);
19  if (datas[3] & 0x40)
20  {
21  return UXBUS_STATE::ERR_CODE;
22  }
23  else
24  if (datas[3] & 0x20)
25  {
26  return UXBUS_STATE::WAR_CODE;
27  }
28  else
29  {
30  return 0;
31  }
32 }
33 
34 int UxbusCmdSer::send_pend(int funcode, int num, int timeout, unsigned char *ret_data) {
35  int ret;
36  // unsigned char rx_data[arm_port_->que_maxlen_] = {0};
37  unsigned char *rx_data = new unsigned char[arm_port_->que_maxlen_];
38  long long expired = get_system_time() + (long long)timeout;
39  while (get_system_time() < expired) {
40  ret = arm_port_->read_frame(rx_data);
41  if (ret != -1) {
42  ret = check_xbus_prot(rx_data, funcode);
43  for (int i = 0; i < num; i++) { ret_data[i] = rx_data[i + 4]; }
44  delete[] rx_data;
45  return ret;
46  }
48  }
49  delete[] rx_data;
50  return UXBUS_STATE::ERR_TOUT;
51 }
52 
53 int UxbusCmdSer::send_xbus(int funcode, unsigned char *datas, int num) {
54  int i;
55  // unsigned char send_data[num + 4];
56  unsigned char *send_data = new unsigned char[num + 4];
57 
58  send_data[0] = UXBUS_CONF::MASTER_ID;
59  send_data[1] = UXBUS_CONF::SLAVE_ID;
60  send_data[2] = num + 1;
61  send_data[3] = funcode;
62  for (i = 0; i < num; i++) { send_data[4 + i] = datas[i]; }
63 
64  int crc = modbus_crc(send_data, 4 + num);
65  send_data[4 + num] = (unsigned char)(crc & 0xFF);
66  send_data[5 + num] = (unsigned char)((crc >> 8) & 0xFF);
67 
68  arm_port_->flush();
69  int ret = arm_port_->write_frame(send_data, num + 6);
70  delete[] send_data;
71  return ret;
72 }
73 
75 
76 int UxbusCmdSer::is_ok(void) { return arm_port_->is_ok(); }
void close_port(void)
Definition: ser.cc:141
void sleep_milliseconds(unsigned long milliseconds)
Definition: uxbus_cmd.h:24
void close(void)
bool state_is_ready
Definition: uxbus_cmd.h:215
int read_frame(unsigned char *data)
Definition: ser.cc:105
int send_xbus(int funcode, unsigned char *datas, int num)
int write_frame(unsigned char *data, int len)
Definition: ser.cc:125
void flush(void)
Definition: ser.cc:87
SerialPort * arm_port_
Definition: uxbus_cmd_ser.h:25
static const int SLAVE_ID
int check_xbus_prot(unsigned char *datas, int funcode)
int modbus_crc(unsigned char *data, int len)
Definition: crc16.cc:59
int is_ok(void)
static const int MASTER_ID
long long get_system_time()
Definition: uxbus_cmd.h:32
~UxbusCmdSer(void)
static const int ERR_TOUT
static const int ERR_CODE
int que_maxlen_
Definition: ser.h:28
static const int WAR_CODE
int send_pend(int funcode, int num, int timeout, unsigned char *ret_data)
int is_ok(void)
Definition: ser.cc:85
UxbusCmdSer(SerialPort *arm_port)
Definition: ser.h:17


xarm_api
Author(s):
autogenerated on Sat May 8 2021 02:51:23