inertial-sense-sdk/src/serialPort.c
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2020 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include "serialPort.h"
14 #include <stdlib.h>
15 
17 
18 void serialPortSetPort(serial_port_t* serialPort, const char* port)
19 {
20  if (serialPort != 0 && port != 0 && port != serialPort->port)
21  {
22  int portLength = (int)strnlen(port, MAX_SERIAL_PORT_NAME_LENGTH);
23  memcpy(serialPort->port, port, portLength);
24  serialPort->port[portLength] = '\0';
25  }
26 }
27 
28 int serialPortOpen(serial_port_t* serialPort, const char* port, int baudRate, int blocking)
29 {
30  if (serialPort == 0 || port == 0 || serialPort->pfnOpen == 0)
31  {
32  return 0;
33  }
34  return serialPort->pfnOpen(serialPort, port, baudRate, blocking);
35 }
36 
37 int serialPortOpenRetry(serial_port_t* serialPort, const char* port, int baudRate, int blocking)
38 {
39  if (serialPort == 0 || port == 0 || serialPort->pfnOpen == 0)
40  {
41  return 0;
42  }
43 
44  serialPortClose(serialPort);
45  for (int retry = 0; retry < 30; retry++)
46  {
47  if (serialPortOpen(serialPort, port, baudRate, blocking))
48  {
49  return 1;
50  }
51  serialPortSleep(serialPort, 100);
52  }
53  serialPortClose(serialPort);
54  return 0;
55 }
56 
58 {
59  if (serialPort == 0 || serialPort->handle == 0)
60  {
61  return 0;
62  }
63  return (serialPort->pfnIsOpen ? serialPort->pfnIsOpen(serialPort) : 1);
64 }
65 
67 {
68  if (serialPort != 0 && serialPort->pfnClose != 0)
69  {
70  return serialPort->pfnClose(serialPort);
71  }
72  return 0;
73 }
74 
76 {
77  if (serialPort == 0 || serialPort->handle == 0 || serialPort->pfnFlush == 0)
78  {
79  return 0;
80  }
81  return serialPort->pfnFlush(serialPort);
82 }
83 
84 int serialPortRead(serial_port_t* serialPort, unsigned char* buffer, int readCount)
85 {
86  return serialPortReadTimeout(serialPort, buffer, readCount, -1);
87 }
88 
89 int serialPortReadTimeout(serial_port_t* serialPort, unsigned char* buffer, int readCount, int timeoutMilliseconds)
90 {
91  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || readCount < 1 || serialPort->pfnRead == 0)
92  {
93  return 0;
94  }
95 
96  int count = serialPort->pfnRead(serialPort, buffer, readCount, timeoutMilliseconds);
97 
98  if (count < 0)
99  {
100  return 0;
101  }
102 
103  return count;
104 }
105 
106 int serialPortReadTimeoutAsync(serial_port_t* serialPort, unsigned char* buffer, int readCount, pfnSerialPortAsyncReadCompletion completion)
107 {
108  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || readCount < 1 || serialPort->pfnAsyncRead == 0 || completion == 0)
109  {
110  return 0;
111  }
112 
113  return serialPort->pfnAsyncRead(serialPort, buffer, readCount, completion);
114 }
115 
116 int serialPortReadLine(serial_port_t* serialPort, unsigned char* buffer, int bufferLength)
117 {
118  return serialPortReadLineTimeout(serialPort, buffer, bufferLength, SERIAL_PORT_DEFAULT_TIMEOUT);
119 }
120 
121 int serialPortReadLineTimeout(serial_port_t* serialPort, unsigned char* buffer, int bufferLength, int timeoutMilliseconds)
122 {
123  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || bufferLength < 8 || serialPort->pfnRead == 0)
124  {
125  return 0;
126  }
127 
128  int prevCR = 0;
129  int bufferIndex = 0;
130  unsigned char c;
131  while (bufferIndex < bufferLength && serialPortReadCharTimeout(serialPort, &c, timeoutMilliseconds) == 1)
132  {
133  buffer[bufferIndex++] = c;
134  if (c == '\n' && prevCR)
135  {
136  // remove \r\n and null terminate and return count of chars
137  buffer[bufferIndex -= 2] = '\0';
138  return bufferIndex;
139  }
140  prevCR = (c == '\r');
141  }
142  return -1;
143 }
144 
145 int serialPortReadAscii(serial_port_t* serialPort, unsigned char* buffer, int bufferLength, unsigned char** asciiData)
146 {
147  return serialPortReadAsciiTimeout(serialPort, buffer, bufferLength, SERIAL_PORT_DEFAULT_TIMEOUT, asciiData);
148 }
149 
150 int serialPortReadAsciiTimeout(serial_port_t* serialPort, unsigned char* buffer, int bufferLength, int timeoutMilliseconds, unsigned char** asciiData)
151 {
152  int count = serialPortReadLineTimeout(serialPort, buffer, bufferLength, timeoutMilliseconds);
153  unsigned char* ptr = buffer;
154  unsigned char* ptrEnd = buffer + count;
155  while (*ptr != '$' && ptr < ptrEnd)
156  {
157  ptr++;
158  }
159 
160  // if at least 8 chars available
161  if (ptrEnd - ptr > 7)
162  {
163  if (asciiData != 0)
164  {
165  *asciiData = ptr;
166  }
167  int checksum = 0;
168  int existingChecksum;
169 
170  // calculate checksum, skipping leading $ and trailing *XX\r\n
171  unsigned char* ptrEndNoChecksum = ptrEnd - 3;
172  while (++ptr < ptrEndNoChecksum)
173  {
174  checksum ^= *ptr;
175  }
176 
177  if (*ptr == '*')
178  {
179  // read checksum from buffer, skipping the * char
180  existingChecksum = strtol((void*)++ptr, NULL, 16);
181  if (existingChecksum == checksum)
182  {
183  return count;
184  }
185  }
186  }
187 
188  return -1;
189 }
190 
191 int serialPortReadChar(serial_port_t* serialPort, unsigned char* c)
192 {
194 }
195 
196 int serialPortReadCharTimeout(serial_port_t* serialPort, unsigned char* c, int timeoutMilliseconds)
197 {
198  return serialPortReadTimeout(serialPort, c, 1, timeoutMilliseconds);
199 }
200 
201 int serialPortWrite(serial_port_t* serialPort, const unsigned char* buffer, int writeCount)
202 {
203  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || writeCount < 1 || serialPort->pfnWrite == 0)
204  {
205  return 0;
206  }
207 
208  int count = serialPort->pfnWrite(serialPort, buffer, writeCount);
209 
210  if (count < 0)
211  {
212  return 0;
213  }
214 
215  return count;
216 }
217 
218 int serialPortWriteLine(serial_port_t* serialPort, const unsigned char* buffer, int writeCount)
219 {
220  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || writeCount < 1)
221  {
222  return 0;
223  }
224 
225  int count = serialPortWrite(serialPort, buffer, writeCount);
226  count += serialPortWrite(serialPort, (unsigned char*)"\r\n", 2);
227  return count;
228 }
229 
230 int serialPortWriteAscii(serial_port_t* serialPort, const char* buffer, int bufferLength)
231 {
232  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || bufferLength < 2)
233  {
234  return 0;
235  }
236 
237  int checkSum = 0;
238  const unsigned char* ptr = (const unsigned char*)buffer;
239  int count = 0;
240 
241  if (*buffer == '$')
242  {
243  ptr++;
244  bufferLength--;
245  }
246  else
247  {
248  count += serialPortWrite(serialPort, (const unsigned char*)"$", 1);
249  }
250 
251  const unsigned char* ptrEnd = ptr + bufferLength;
252  unsigned char buf[16];
253 
254  count += serialPortWrite(serialPort, (const unsigned char*)buffer, bufferLength);
255 
256  while (ptr != ptrEnd)
257  {
258  checkSum ^= *ptr++;
259  }
260 
261 #ifdef _MSC_VER
262 
263 #pragma warning(push)
264 #pragma warning(disable: 4996)
265 
266 #endif
267 
268  snprintf((char*)buf, sizeof(buf), "*%.2x\r\n", checkSum);
269 
270 #ifdef _MSC_VER
271 
272 #pragma warning(pop)
273 
274 #endif
275 
276  count += serialPortWrite(serialPort, buf, 5);
277 
278  return count;
279 }
280 
281 int serialPortWriteAndWaitFor(serial_port_t* serialPort, const unsigned char* buffer, int writeCount, const unsigned char* waitFor, int waitForLength)
282 {
283  return serialPortWriteAndWaitForTimeout(serialPort, buffer, writeCount, waitFor, waitForLength, SERIAL_PORT_DEFAULT_TIMEOUT);
284 }
285 
286 int serialPortWriteAndWaitForTimeout(serial_port_t* serialPort, const unsigned char* buffer, int writeCount, const unsigned char* waitFor, int waitForLength, const int timeoutMilliseconds)
287 {
288  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || writeCount < 1 || waitFor == 0 || waitForLength < 1)
289  {
290  return 0;
291  }
292 
293  int actuallyWrittenCount = serialPortWrite(serialPort, buffer, writeCount);
294 
295  if (actuallyWrittenCount != writeCount)
296  {
297  return 0;
298  }
299 
300  return serialPortWaitForTimeout(serialPort, waitFor, waitForLength, timeoutMilliseconds);
301 }
302 
303 int serialPortWaitFor(serial_port_t* serialPort, const unsigned char* waitFor, int waitForLength)
304 {
305  return serialPortWaitForTimeout(serialPort, waitFor, waitForLength, SERIAL_PORT_DEFAULT_TIMEOUT);
306 }
307 
308 int serialPortWaitForTimeout(serial_port_t* serialPort, const unsigned char* waitFor, int waitForLength, int timeoutMilliseconds)
309 {
310  if (serialPort == 0 || serialPort->handle == 0 || waitFor == 0 || waitForLength < 1)
311  {
312  return 1;
313  }
314  else if (waitForLength > 128)
315  {
316  return 0;
317  }
318 
319  unsigned char buf[128] = { 0 };
320  int count = serialPortReadTimeout(serialPort, buf, waitForLength, timeoutMilliseconds);
321 
322  if (count == waitForLength && memcmp(buf, waitFor, waitForLength) == 0)
323  {
324  return 1;
325  }
326  return 0;
327 }
328 
330 {
331  if (serialPort == 0 || serialPort->handle == 0 || serialPort->pfnGetByteCountAvailableToRead == 0)
332  {
333  return 0;
334  }
335 
336  return serialPort->pfnGetByteCountAvailableToRead(serialPort);
337 }
338 
340 {
341  if (serialPort == 0 || serialPort->handle == 0 || serialPort->pfnGetByteCountAvailableToWrite == 0)
342  {
343  return 0;
344  }
345 
346  return serialPort->pfnGetByteCountAvailableToWrite(serialPort);
347 }
348 
349 int serialPortSleep(serial_port_t* serialPort, int sleepMilliseconds)
350 {
351  if (serialPort == 0 || serialPort->pfnSleep == 0)
352  {
353  return 0;
354  }
355 
356  return serialPort->pfnSleep(sleepMilliseconds);
357 }
int serialPortGetByteCountAvailableToRead(serial_port_t *serialPort)
uint32_t bufferLength
Definition: USBD.h:91
int serialPortReadAscii(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, unsigned char **asciiData)
int serialPortReadLine(serial_port_t *serialPort, unsigned char *buffer, int bufferLength)
char port[MAX_SERIAL_PORT_NAME_LENGTH+1]
int serialPortReadCharTimeout(serial_port_t *serialPort, unsigned char *c, int timeoutMilliseconds)
pfnSerialPortAsyncRead pfnAsyncRead
int serialPortFlush(serial_port_t *serialPort)
int serialPortReadTimeout(serial_port_t *serialPort, unsigned char *buffer, int readCount, int timeoutMilliseconds)
int serialPortOpen(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
#define MAX_SERIAL_PORT_NAME_LENGTH
int serialPortWriteAscii(serial_port_t *serialPort, const char *buffer, int bufferLength)
size_t count(InputIterator first, InputIterator last, T const &item)
Definition: catch.hpp:3206
pfnSerialPortGetByteCountAvailableToWrite pfnGetByteCountAvailableToWrite
void serialPortSetPort(serial_port_t *serialPort, const char *port)
#define NULL
Definition: nm_bsp.h:52
int serialPortOpenRetry(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
int serialPortGetByteCountAvailableToWrite(serial_port_t *serialPort)
void(* pfnSerialPortAsyncReadCompletion)(serial_port_t *serialPort, unsigned char *buf, int len, int errorCode)
int serialPortSleep(serial_port_t *serialPort, int sleepMilliseconds)
int serialPortWaitFor(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength)
int serialPortReadAsciiTimeout(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, int timeoutMilliseconds, unsigned char **asciiData)
int serialPortRead(serial_port_t *serialPort, unsigned char *buffer, int readCount)
int serialPortReadLineTimeout(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, int timeoutMilliseconds)
int serialPortWrite(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
int serialPortWriteAndWaitFor(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength)
int serialPortClose(serial_port_t *serialPort)
int serialPortWriteAndWaitForTimeout(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength, const int timeoutMilliseconds)
int serialPortIsOpen(serial_port_t *serialPort)
#define snprintf
Definition: test_suite.cpp:86
int serialPortWriteLine(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
pfnSerialPortGetByteCountAvailableToRead pfnGetByteCountAvailableToRead
int serialPortWaitForTimeout(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength, int timeoutMilliseconds)
int serialPortReadChar(serial_port_t *serialPort, unsigned char *c)
int serialPortReadTimeoutAsync(serial_port_t *serialPort, unsigned char *buffer, int readCount, pfnSerialPortAsyncReadCompletion completion)


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:05