uxbus_cmd_tcp.cc
Go to the documentation of this file.
1 /*
2 # Software License Agreement (MIT License)
3 #
4 # Copyright (c) 2020, UFACTORY, Inc.
5 # All rights reserved.
6 #
7 # Author: Vinman <vinman.wen@ufactory.cc> <vinman.cub@gmail.com>
8 */
9 
13 
15  arm_port_ = arm_port;
18 }
19 
21 
22 int UxbusCmdTcp::check_xbus_prot(unsigned char *datas, int funcode) {
23  unsigned char *data_fp = &datas[4];
24 
25  int sizeof_data = bin8_to_32(datas);
26  if (sizeof_data < 8 || sizeof_data >= arm_port_->que_maxlen_)
27  {
28  return UXBUS_STATE::ERR_LENG;
29  }
30 
31  int num = bin8_to_16(&data_fp[0]);
32  int prot = bin8_to_16(&data_fp[2]);
33  int len = bin8_to_16(&data_fp[4]);
34  int fun = data_fp[6];
35  int state = data_fp[7];
36 
37  int bus_flag = bus_flag_;
38  if (bus_flag == TX2_BUS_FLAG_MIN_)
39  {
40  bus_flag = TX2_BUS_FLAG_MAX_;
41  }
42  else
43  {
44  bus_flag -= 1;
45  }
46 
47  if (num != bus_flag) { return UXBUS_STATE::ERR_NUM; }
48  if (prot != TX2_PROT_CON_) { return UXBUS_STATE::ERR_PROT; }
49  if (fun != funcode) { return UXBUS_STATE::ERR_FUN; }
50  state_is_ready = !(state & 0x10);
51  if (state & 0x40) { return UXBUS_STATE::ERR_CODE; }
52  if (state & 0x20) { return UXBUS_STATE::WAR_CODE; }
53  if (sizeof_data != len + 6) { return UXBUS_STATE::ERR_LENG; }
54  // if (state & 0x10) { return UXBUS_STATE::STATE_NOT_READY; }
55  return 0;
56 }
57 
58 int UxbusCmdTcp::send_pend(int funcode, int num, int timeout, unsigned char *ret_data) {
59  int i;
60  int ret = UXBUS_STATE::ERR_TOUT;
61  int ret2;
62  // unsigned char rx_data[arm_port_->que_maxlen_] = {0};
63  unsigned char *rx_data = new unsigned char[arm_port_->que_maxlen_];
64  long long expired = get_system_time() + (long long)timeout;
65  while (get_system_time() < expired) {
66  ret2 = arm_port_->read_frame(rx_data);
67  if (ret2 != -1) {
68  // print_hex("recv:", rx_data, arm_port_->que_maxlen_);
69  ret = check_xbus_prot(rx_data, funcode);
70  if (ret == 0 || ret == UXBUS_STATE::ERR_CODE || ret == UXBUS_STATE::WAR_CODE) {
71  int n = num;
72  if (num == -1) {
73  n = rx_data[9] - 2;
74  }
75  for (i = 0; i < n; i++) { ret_data[i] = rx_data[i + 8 + 4]; }
76  // print_hex(" 3", rx_data, num + 8 + 4);
77  break;
78  }
79  else if (ret != UXBUS_STATE::ERR_NUM) {
80  break;
81  }
82  }
84  }
85  delete[] rx_data;
86  return ret;
87 }
88 
89 int UxbusCmdTcp::send_xbus(int funcode, unsigned char *datas, int num) {
90  int len = num + 7;
91  // unsigned char send_data[len];
92  unsigned char *send_data = new unsigned char[len];
93 
94  bin16_to_8(bus_flag_, &send_data[0]);
95  bin16_to_8(prot_flag_, &send_data[2]);
96  bin16_to_8(num + 1, &send_data[4]);
97  send_data[6] = funcode;
98 
99  for (int i = 0; i < num; i++) { send_data[7 + i] = datas[i]; }
100  arm_port_->flush();
101  // print_hex("send:", send_data, num + 7);
102  int ret = arm_port_->write_frame(send_data, len);
103  delete[] send_data;
104  if (ret != len) { return -1; }
105 
106  bus_flag_ += 1;
108 
109  return 0;
110 }
111 
113 
114 int UxbusCmdTcp::is_ok(void) { return arm_port_->is_ok(); }
void sleep_milliseconds(unsigned long milliseconds)
Definition: uxbus_cmd.h:24
int is_ok(void)
bool state_is_ready
Definition: uxbus_cmd.h:215
void close(void)
SocketPort * arm_port_
Definition: uxbus_cmd_tcp.h:25
int write_frame(unsigned char *data, int len)
Definition: socket.cc:131
int check_xbus_prot(unsigned char *datas, int funcode)
int TX2_BUS_FLAG_MAX_
Definition: uxbus_cmd_tcp.h:31
int read_frame(unsigned char *data)
Definition: socket.cc:122
int bin8_to_32(unsigned char *a)
Definition: data_type.h:38
static const int ERR_FUN
void flush(void)
Definition: socket.cc:120
int que_maxlen_
Definition: socket.h:25
void close_port(void)
Definition: socket.cc:136
int is_ok(void)
Definition: socket.cc:118
void bin16_to_8(int a, unsigned char *b)
Definition: data_type.h:32
static const int ERR_LENG
static const int ERR_PROT
UxbusCmdTcp(SocketPort *arm_port)
long long get_system_time()
Definition: uxbus_cmd.h:32
~UxbusCmdTcp(void)
int send_pend(int funcode, int num, int timeout, unsigned char *ret_data)
static const int ERR_NUM
static const int ERR_TOUT
static const int ERR_CODE
static const int WAR_CODE
int send_xbus(int funcode, unsigned char *datas, int num)
int bin8_to_16(unsigned char *a)
Definition: data_type.h:42
int TX2_BUS_FLAG_MIN_
Definition: uxbus_cmd_tcp.h:30


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