ping.cpp
Go to the documentation of this file.
1 
2 #include "serial_mip_device.hpp"
3 
5 #include <mip/mip.hpp>
6 #include <mip/mip_device.hpp>
7 #include <serial/serial.h>
8 
9 #include <cstring>
10 #include <stdio.h>
11 
12 namespace mip
13 {
14  namespace C {
15  CmdResult mipcmd_base_getDeviceInfo(struct C::MipInterfaceState* device, const struct MipCmd_Base_GetDeviceInfo* cmd, struct MipCmd_Base_GetDeviceInfo_Response* response);
16  CmdResult mipcmd_base_getDeviceInfo(struct C::MipInterfaceState* device, struct MipBaseDeviceInfo* info);
17  }
18 }
19 
20 int usage(const char* argv[])
21 {
22  fprintf(stderr, "Usage: %s <port> <baudrate>\n", argv[0]);
23  return 1;
24 }
25 
26 
27 int main(int argc, const char* argv[])
28 {
29  uint32_t baud = 0;
30 
31  if( argc == 1 )
32  {
33  printf("Available serial ports:\n");
34  std::vector<serial::PortInfo> ports = serial::list_ports();
35 
36  for(const serial::PortInfo& port : ports)
37  {
38  printf(" %s %s %s\n", port.port.c_str(), port.description.c_str(), port.hardware_id.c_str());
39  }
40  return 0;
41  }
42  else if( argc == 3 )
43  {
44  baud = std::strtoul(argv[2], nullptr, 10);
45  if( baud == 0 )
46  {
47  fprintf(stderr, "Error: invalid baud rate '%s'\n", argv[2]);
48  return 1;
49  }
50  }
51  else
52  {
53  return usage(argv);
54  }
55 
56  try
57  {
58 #define METHOD 8
59 
60 #if METHOD == 1 || METHOD == 2 || METHOD == 3
61  serial::Serial port(argv[1], baud, serial::Timeout::simpleTimeout(10));
62 
63  uint8_t buffer[PACKET_LENGTH_MAX];
64 
65  mip::Packet packet(buffer, sizeof(buffer), mip::MIP_BASE_COMMAND_DESC_SET);
66 
67  uint8_t* payload;
68  mip::RemainingCount available = packet.allocField(mip::MIP_CMD_DESC_BASE_PING, 0, &payload);
69 
70  #if METHOD == 1
71  mip::MipCmd_Base_GetDeviceInfo info;
72  size_t used = mip::insert_MipCmd_Base_GetDeviceInfo(payload, available, 0, &info);
73  #elif METHOD == 2
74  mip::MipCmd_Base_GetDeviceInfo info;
75  size_t used = mip::insert(payload, available, 0, info);
76  #elif METHOD == 3
77  size_t used = mip::insert_MipCmd_Base_GetDeviceInfo_args(payload, available, 0);
78  #endif
79 
80  // Skip error checking as this field will always fit in this buffer.
81  packet.reallocLastField(payload, used);
82 
83  packet.finalize();
84 
85  printf("Send bytes [");
86  for(size_t i=0; i<packet.totalLength(); i++)
87  printf("%02X", packet.pointer()[i]);
88  printf("]\n");
89 
90  port.write(packet.pointer(), packet.totalLength());
91 
92  size_t read = port.read(buffer, sizeof(buffer));
93 
94  printf("Received bytes [");
95  for(size_t i=0; i<read; i++)
96  printf("%02X", buffer[i]);
97  printf("]\n");
98 
99 #elif METHOD == 4
100 
101  MipDevice device(argv[1], baud);
102 
103  uint8_t responseBuffer[MIP_FIELD_PAYLOAD_LENGTH_MAX];
104  mip::C::MipPendingCmd cmd;
105  mip::C::MipPendingCmd_initWithResponse(&cmd, mip::MIP_BASE_COMMAND_DESC_SET, mip::MIP_CMD_DESC_BASE_GET_DEVICE_INFO, mip::MIP_REPLY_DESC_BASE_DEVICE_INFO, responseBuffer, sizeof(responseBuffer));
106  mip::C::MipCmdQueue_enqueue(device.cmdQueue(), &cmd);
107 
108  uint8_t packetBuffer[PACKET_LENGTH_MAX];
109 
110  mip::Packet packet(packetBuffer, sizeof(packetBuffer), mip::MIP_BASE_COMMAND_DESC_SET);
111 
112  packet.addField(mip::MIP_CMD_DESC_BASE_GET_DEVICE_INFO, NULL, 0);
113  packet.finalize();
114 
115  device.sendToDevice(packet);
116 
117  mip::CmdResult result = device.waitForReply(cmd);
118 
119 // MipBase_DeviceInfo info;
120 // CmdResult result = MipInterface_getDeviceInfo(device, &info);
121 
122  if( result == mip::MIP_ACK_OK )
123  {
124  const size_t responseSize = mip::C::MipPendingCmd_responseLength(&cmd);
125  printf("Success: command completed with ACK: responseLength=%ld\n", responseSize);
126 
127  mip::MipCmd_Base_GetDeviceInfo_Response response;
128  size_t used = mip::extract_MipCmd_Base_GetDeviceInfo_Response(mip::C::MipPendingCmd_response(&cmd), responseSize, 0, &response);
129  if( used == responseSize )
130  {
131  auto print_info = [](const char* name, const char info[16])
132  {
133  char msg[17] = {0};
134  std::strncpy(msg, info, 16);
135  printf(" %s%s\n", name, msg);
136  };
137 
138  print_info("Model name: ", response.device_info.model_name);
139  print_info("Model number: ", response.device_info.model_number);
140  print_info("Serial Number: ", response.device_info.serial_number);
141  print_info("Device Options: ", response.device_info.device_options);
142  print_info("Lot Number: ", response.device_info.lot_number);
143 
144  printf( " Firmware version: %d.%d.%d\n\n",
145  (response.device_info.firmware_version / 1000),
146  (response.device_info.firmware_version / 100) % 10,
147  (response.device_info.firmware_version / 1) % 100
148  );
149  }
150  else
151  {
152  printf("Error: response size does not match expected (at least %ld bytes)\n", used);
153  }
154  }
155  else
156  {
157  printf("Error: command completed with NACK: %s (%d)\n", mip::MipCmdResult_toString(result), result);
158  }
159 #elif METHOD >= 5
160  MipDevice device(argv[1], baud);
161 
162  mip::MipCmd_Base_GetDeviceInfo cmd;
163  mip::MipCmd_Base_GetDeviceInfo_Response response;
164 
165  #if METHOD == 5
166  mip::CmdResult result = exec_MipCmd_Base_GetDeviceInfo(&device, &cmd, &response);
167  #elif METHOD == 6
168  mip::CmdResult result = mipcmd_base_getDeviceInfo(&device, &response.device_info);
169  #elif METHOD == 7
170  mip::CmdResult result = mip::runCommand(&device, cmd, response);
171  #elif METHOD == 8
172  mip::DeviceInterface* device2 = &device;
173  mip::C::MipInterfaceState* device3 = device2;
174  mip::MipInterfaceState* device4 = device3;
175  static_assert(std::is_same<mip::MipInterfaceState, mip::C::MipInterfaceState>::value, "Not the same");
176  mip::CmdResult result = mip::get_device_information(device4, &response.device_info);
177  #endif
178 
179  if( result == mip::MIP_ACK_OK )
180  {
181  printf("Success:\n");
182 
183  auto print_info = [](const char* name, const char info[16])
184  {
185  char msg[17] = {0};
186  std::strncpy(msg, info, 16);
187  printf(" %s%s\n", name, msg);
188  };
189 
190  print_info("Model name: ", response.device_info.model_name);
191  print_info("Model number: ", response.device_info.model_number);
192  print_info("Serial Number: ", response.device_info.serial_number);
193  print_info("Device Options: ", response.device_info.device_options);
194  print_info("Lot Number: ", response.device_info.lot_number);
195 
196  printf( " Firmware version: %d.%d.%d\n\n",
197  (response.device_info.firmware_version / 1000),
198  (response.device_info.firmware_version / 100) % 10,
199  (response.device_info.firmware_version / 1) % 100
200  );
201  }
202  else
203  {
204  printf("Error: command completed with NACK: %s (%d)\n", mip::MipCmdResult_toString(result), result);
205  }
206 
207 #endif
208  }
209  catch(const std::exception& ex)
210  {
211  fprintf(stderr, "Could not open serial port: %s", ex.what());
212  return 1;
213  }
214 
215  return 0;
216 }
217 //
218 // namespace mip
219 // {
220 // namespace C
221 // {
222 //
223 // CmdResult mipcmd_base_getDeviceInfo(struct C::MipInterfaceState* device, struct MipBaseDeviceInfo* info)
224 // {
225 // uint8_t payload[MIP_FIELD_LENGTH_MAX];
226 //
227 // size_t payloadLength = insert_MipCmd_Base_GetDeviceInfo(payload, sizeof(payload), 0, NULL);
228 // assert(payloadLength <= sizeof(payload));
229 //
230 // uint8_t responseLength;
231 // CmdResult result = C::MipInterface_runCommandWithResponse(device, MIP_BASE_COMMAND_DESC_SET, MIP_CMD_DESC_BASE_GET_DEVICE_INFO, payload, payloadLength, MIP_REPLY_DESC_BASE_DEVICE_INFO, payload, &responseLength);
232 // if( result == MIP_ACK_OK )
233 // {
234 // size_t used = extract_MipBaseDeviceInfo(payload, responseLength, 0, info);
235 // if( used != responseLength )
236 // result = MIP_STATUS_ERROR;
237 // }
238 //
239 // return result;
240 // }
241 //
242 // CmdResult mipcmd_base_getDeviceInfo(struct C::MipInterfaceState* device, const struct MipCmd_Base_GetDeviceInfo* cmd, struct MipCmd_Base_GetDeviceInfo_Response* response)
243 // {
244 // uint8_t buffer[PACKET_LENGTH_MAX];
245 //
246 // struct C::Packet packet;
247 // MipPacket_create(&packet, buffer, sizeof(buffer), MIP_BASE_COMMAND_DESC_SET);
248 //
249 // uint8_t* payload;
250 // RemainingCount available = MipPacket_allocField(&packet, MIP_CMD_DESC_BASE_GET_DEVICE_INFO, 0, &payload);
251 //
252 // size_t used = insert_MipCmd_Base_GetDeviceInfo(payload, available, 0, cmd);
253 // assert( used <= available );
254 // MipPacket_reallocLastField(&packet, payload, used);
255 //
256 // MipPacket_finalize(&packet);
257 //
258 //
259 // struct C::MipPendingCmd pending;
260 // C::MipPendingCmd_initWithResponse(&pending, MIP_BASE_COMMAND_DESC_SET, MIP_CMD_DESC_BASE_GET_DEVICE_INFO, MIP_REPLY_DESC_BASE_DEVICE_INFO, buffer, MIP_FIELD_PAYLOAD_LENGTH_MAX);
261 //
262 // C::MipCmdQueue_enqueue( C::MipInterface_cmdQueue(device), &pending );
263 //
264 // if( !C::MipInterface_sendToDevice(device, MipPacket_pointer(&packet), MipPacket_totalLength(&packet)) )
265 // {
266 // C::MipCmdQueue_dequeue(C::MipInterface_cmdQueue(device), &pending);
267 // return MIP_STATUS_ERROR;
268 // }
269 //
270 // CmdResult result = C::MipInterface_waitForReply(device, &pending);
271 //
272 // if( result == MIP_ACK_OK )
273 // {
274 // size_t responseLength = C::MipPendingCmd_responseLength(&pending);
275 //
276 // used = extract_MipCmd_Base_GetDeviceInfo_Response( C::MipPendingCmd_response(&pending), responseLength, 0, response);
277 //
278 // if( used!= responseLength )
279 // return MIP_STATUS_ERROR;
280 // }
281 //
282 // return result;
283 // }
284 //
285 // }
286 // }
response
const std::string response
mip
Definition: ping.cpp:12
msg
msg
MIP_CMD_DESC_BASE_PING
@ MIP_CMD_DESC_BASE_PING
Definition: commands_base.h:36
MIP_CMD_DESC_BASE_GET_DEVICE_INFO
@ MIP_CMD_DESC_BASE_GET_DEVICE_INFO
Definition: commands_base.h:38
MIP_REPLY_DESC_BASE_DEVICE_INFO
@ MIP_REPLY_DESC_BASE_DEVICE_INFO
Definition: commands_base.h:48
port
int port
Definition: CV7_example.c:45
mip::C::mipcmd_base_getDeviceInfo
CmdResult mipcmd_base_getDeviceInfo(struct C::MipInterfaceState *device, struct MipBaseDeviceInfo *info)
mip::commands_3dm::insert
void insert(Serializer &serializer, const NmeaMessage &self)
Definition: commands_3dm.cpp:27
device
mip_interface device
Definition: CV7_example.c:47
MIP_ACK_OK
@ MIP_ACK_OK
Command completed successfully.
Definition: mip_result.h:38
MIP_FIELD_PAYLOAD_LENGTH_MAX
@ MIP_FIELD_PAYLOAD_LENGTH_MAX
Definition: mip_offsets.h:35
commands_base.h
usage
int usage(const char *argv[])
Definition: ping.cpp:20
mip::C::mipcmd_base_getDeviceInfo
CmdResult mipcmd_base_getDeviceInfo(struct C::MipInterfaceState *device, const struct MipCmd_Base_GetDeviceInfo *cmd, struct MipCmd_Base_GetDeviceInfo_Response *response)
main
int main(int argc, const char *argv[])
Definition: ping.cpp:27
stderr
stderr
cmd
string cmd


microstrain_inertial_driver
Author(s): Brian Bingham, Parker Hannifin Corp
autogenerated on Mon Jun 24 2024 02:51:40