test_cht10.cpp
Go to the documentation of this file.
1 #include <string>
3 #include <cstdlib>
4 
5 #include <iostream>
6 #include <stdint.h>
7 #define BUFSIZE 17
8 
9 int main(int argc, char** argv){
10  cht10_seiral_func::Cht10Driver cht10driver_;
11 
12  std::string serialNumber_;
13  serialNumber_ = "/dev/ttyUSB0";
14  int baudRate_ = 115200;
15 
16  std::stringstream ostream;
17  int fd, len, rcv_cnt;
18  bool success_flag;
19  char buf[40], temp_buf[BUFSIZE],result_buf[BUFSIZE];
20  int laser_data=0;
21  char data_buf[4];
22  rcv_cnt = 0;
23  success_flag = false;
24  memset(buf, 0xba, sizeof(buf));
25  memset(temp_buf, 0xba, sizeof(temp_buf));
26  memset(result_buf, 0xba, sizeof(result_buf));
27 
28  fd = open(serialNumber_.c_str(), O_RDWR | O_NOCTTY | O_NDELAY );
29  if(fd < 0){
30  std::cout<<"Open Serial: "<<serialNumber_.c_str()<<" Error!";
31  exit(0);
32  }
33 
34  cht10driver_.UART0_Init(fd,baudRate_,0,8,1,'N');
35  while(1){
36  len = cht10driver_.UART0_Recv(fd, buf,40);
37  if(len>0){
38  for(int i = 0; i < len; i++){
39  if(rcv_cnt<=BUFSIZE){
40  result_buf[rcv_cnt++] = buf[i];
41  if(rcv_cnt == BUFSIZE){
42  success_flag = true;
43  }
44  }//end if
45  else{
46  /****
47  * checkout received data
48  */
49 // std::cout<<"Received data, with :[";
50 // for(int j=0;j<BUFSIZE;j++){
51 // printf("%c ",(unsigned char) result_buf[j]);
52 // }
53 // printf("] \n");
54  success_flag = false;
55 
56  for(int count = 0; count < 4; count++){
57  data_buf[count] = result_buf[9+count];
58  }
59  sscanf(data_buf, "%x", &laser_data);
60  std::cout<<"sensor data:"<<laser_data<<", Distance: "<<(double)laser_data/1000<<std::endl;
61  /****
62  * data writing end
63  */
64  if('~' == buf[i]){
65  rcv_cnt = 0;
66  result_buf[rcv_cnt++] = buf[i];
67  }
68  }//end else
69  }//end for
70  }
71  }
72 
73 
74 }
int UART0_Recv(int fd, char *rcv_buf, int data_len)
int main(int argc, char **argv)
Definition: test_cht10.cpp:9
int UART0_Init(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity)
#define BUFSIZE
Definition: test_cht10.cpp:7


cht10_node
Author(s): Carl <1271087623@qq.comd>
autogenerated on Mon Jun 10 2019 12:48:57