Tetrapod Project
serial_communication_test.cpp
Go to the documentation of this file.
1 #include "ros/ros.h"
2 
3 #include <libserial/SerialPort.h>
4 #include <libserial/SerialStream.h>
5 
6 #include <string>
7 #include <stdio.h>
8 #include <iomanip>
9 
10 int main(int argc, char **argv)
11 {
12  //ros::init(argc, argv, "serial_communication_node");
13 
14  //ros::NodeHandle node_handle;
15 
16  //ros::Rate loop_rate(1000000);
17 
18  LibSerial::SerialPort serial_port;
19 
20  serial_port.Open("/dev/ttyACM0");
21 
22  serial_port.SetBaudRate(LibSerial::BaudRate::BAUD_9600);
23 
24  serial_port.SetCharacterSize(LibSerial::CharacterSize::CHAR_SIZE_8);
25 
26  serial_port.SetFlowControl(LibSerial::FlowControl::FLOW_CONTROL_NONE);
27 
28  serial_port.SetStopBits(LibSerial::StopBits::STOP_BITS_1);
29 
30  //const int NUMBER_OF_STATES = 8;
31 
32  //const int BYTES_PER_STATE = 8;
33 
34  //const int BUFFER_SIZE = NUMBER_OF_STATES*BYTES_PER_STATE;
35 
36  //int timeout_ms = 10;
37 
38  //std::string recv_buffer = "";
39 
40  /*
41  while(ros::ok())
42  {
43  ros::spinOnce();
44 
45  loop_rate.sleep();
46 
47  if(serial_port.IsDataAvailable())
48  {
49  serial_port.Read(recv_buffer, BUFFER_SIZE, timeout_ms);
50 
51 
52  ROS_INFO("%s", recv_buffer.c_str());
53  }
54 
55  }
56  */
57 
58  /*
59  double send_time = ros::Time::now().toSec();
60  serial_port.Write("Hello");
61  while(!serial_port.IsDataAvailable())
62  {
63  loop_rate.sleep();
64  }
65  double recv_time = ros::Time::now().toSec();
66  serial_port.Read(recv_buffer, BUFFER_SIZE, timeout_ms);
67 
68  ROS_INFO("Message received: %s", recv_buffer.c_str());
69  ROS_INFO("Total time: %f", recv_time - send_time);
70  */
71 
72  /*
73  double arr[3] = {0.421, 23.09991, 3.0};
74 
75  std::ostringstream stream_object;
76 
77  stream_object << std::fixed;
78 
79  stream_object << std::width
80 
81  stream_object << std::setprecision(4);
82 
83  recv_buffer += "toString(8.0);";
84  recv_buffer += " ";
85  recv_buffer += "toString(7.0);";
86 
87 
88  std::cout << recv_buffer << "\n";
89  */
90 
91  /*
92  int data_elements = 3;
93 
94  //int data_size = 8;
95 
96  //char tx_buffer[data_elements*data_size];
97 
98  //double data[data_elements] = {2.045, 105.632, 3.103993};
99 
100  for(int i = 0; i < data_elements; i++)
101  {
102  for(int j = 0; j < data_size; j++)
103  {
104  tx_buffer[i*data_size + j] = ((char *)data)[j];
105  }
106  }
107  */
108  /*
109  int n_motors = 3;
110 
111  int msg_size = 32;
112 
113  char send_frame[msg_size];
114 
115  double my_new_fucking_data[n_motors + 1] = {-100.66, 12.338954, -0.3442234, 3.9291};
116 
117  for (int i = 0; i < msg_size; i++)
118  {
119  send_frame[i] = ((char *) my_new_fucking_data)[i];
120  }
121 
122  double buffer[n_motors];
123 
124  for(int i = 0; i < n_motors; i++)
125  {
126  double val;
127 
128  val = *((double*)send_frame + i + 1);
129 
130  buffer[i] = val;
131  }*/
132  /*
133  ROS_INFO("%f", buffer[0]);
134  ROS_INFO("%f", buffer[1]);
135  ROS_INFO("%f", buffer[2]);
136  */
137  //std::string buffer(1, tx_buffer);
138 
139  //serial_port.Write(send_frame);
140  /*
141  ROS_INFO("Write complete");
142 
143  while(!serial_port.IsDataAvailable())
144  {
145  loop_rate.sleep();
146  }
147 
148  ROS_INFO("Reply received");
149 
150 
151  std::string rx_buffer = "";
152 
153  serial_port.Read(rx_buffer, 72, 10000);
154 
155  ROS_INFO("Read complete");
156 
157  char rx_str[72];
158 
159  std::strcpy(rx_str, rx_buffer.c_str());
160 
161  for(int k = 0; k < 3*3; k++)
162  {
163  double val;
164 
165  val = *((double*)rx_str + k);
166 
167  ROS_INFO("%f\n", val);
168  }
169  */
170  serial_port.Close();
171 
172  return 0;
173 }
int main(int argc, char **argv)