serial/serialPort.c
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014 Inertial Sense, LLC - 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)
31  {
32  return 0;
33  }
34  else if (serialPort->pfnOpen != 0)
35  {
36  if (!serialPort->pfnOpen(serialPort, port, baudRate, blocking))
37  {
38  serialPortClose(serialPort);
39  return 0;
40  }
41  return 1;
42  }
43  return 0;
44 }
45 
47 {
48  if (serialPort == 0 || serialPort->handle == 0)
49  {
50  return 0;
51  }
52  return (serialPort->pfnIsOpen ? serialPort->pfnIsOpen(serialPort) : 1);
53 }
54 
56 {
57  if (serialPort != 0 && serialPort->pfnClose != 0)
58  {
59  return serialPort->pfnClose(serialPort);
60  }
61  return 0;
62 }
63 
65 {
66  if (serialPort == 0 || serialPort->handle == 0 || serialPort->pfnFlush == 0)
67  {
68  return 0;
69  }
70  return serialPort->pfnFlush(serialPort);
71 }
72 
73 int serialPortRead(serial_port_t* serialPort, unsigned char* buffer, int readCount)
74 {
75  return serialPortReadTimeout(serialPort, buffer, readCount, -1);
76 }
77 
78 int serialPortReadTimeout(serial_port_t* serialPort, unsigned char* buffer, int readCount, int timeoutMilliseconds)
79 {
80  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || readCount < 1 || serialPort->pfnRead == 0)
81  {
82  return 0;
83  }
84 
85  int count = serialPort->pfnRead(serialPort, buffer, readCount, timeoutMilliseconds);
86 
87  if (count < 0)
88  {
89  return 0;
90  }
91 
92  return count;
93 }
94 
95 int serialPortReadTimeoutAsync(serial_port_t* serialPort, unsigned char* buffer, int readCount, pfnSerialPortAsyncReadCompletion completion)
96 {
97  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || readCount < 1 || serialPort->pfnAsyncRead == 0 || completion == 0)
98  {
99  return 0;
100  }
101 
102  return serialPort->pfnAsyncRead(serialPort, buffer, readCount, completion);
103 }
104 
105 int serialPortReadLine(serial_port_t* serialPort, unsigned char** result)
106 {
107  return serialPortReadLineTimeout(serialPort, result, SERIAL_PORT_DEFAULT_TIMEOUT);
108 }
109 
110 int serialPortReadLineTimeout(serial_port_t* serialPort, unsigned char** result, int timeoutMilliseconds)
111 {
112  if (serialPort == 0 || serialPort->handle == 0 || result == 0 || serialPort->pfnRead == 0)
113  {
114  return 0;
115  }
116 
117  int prevCR = 0;
118  unsigned int memIndex = 0;
119  unsigned int memCapacity = 512;
120  unsigned char b;
121  unsigned char* mem = (unsigned char*)malloc(memCapacity);
122 
123  while (serialPortReadCharTimeout(serialPort, &b, timeoutMilliseconds) == 1)
124  {
125  if (memIndex == memCapacity)
126  {
127  memCapacity = (unsigned int)(memCapacity * 1.5);
128  mem = (unsigned char*)realloc(mem, memCapacity);
129  }
130  mem[memIndex++] = b;
131  if (b == '\n' && prevCR)
132  {
133  mem[memIndex -= 2] = '\0';
134  *result = mem;
135  return memIndex;
136  }
137  prevCR = (b == '\r');
138  }
139  free(mem);
140  mem = 0;
141  return -1;
142 }
143 
144 int serialPortReadChar(serial_port_t* serialPort, unsigned char* c)
145 {
147 }
148 
149 int serialPortReadCharTimeout(serial_port_t* serialPort, unsigned char* c, int timeoutMilliseconds)
150 {
151  return serialPortReadTimeout(serialPort, c, 1, timeoutMilliseconds);
152 }
153 
154 int serialPortWrite(serial_port_t* serialPort, const unsigned char* buffer, int writeCount)
155 {
156  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || writeCount < 1 || serialPort->pfnWrite == 0)
157  {
158  return 0;
159  }
160 
161  int count = serialPort->pfnWrite(serialPort, buffer, writeCount);
162 
163  if (count < 0)
164  {
165  return 0;
166  }
167 
168  return count;
169 }
170 
171 int serialPortWriteLine(serial_port_t* serialPort, const unsigned char* buffer, int writeCount)
172 {
173  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || writeCount < 1)
174  {
175  return 0;
176  }
177 
178  int count = serialPortWrite(serialPort, buffer, writeCount);
179  count += serialPortWrite(serialPort, (unsigned char[2]) { '\r', '\n' }, 2);
180  return count;
181 }
182 
183 int serialPortWriteAscii(serial_port_t* serialPort, const char* buffer, int bufferLength)
184 {
185  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || bufferLength < 2)
186  {
187  return 0;
188  }
189 
190  int checkSum = 0;
191  const unsigned char* ptr = (const unsigned char*)buffer;
192  const unsigned char* ptrEnd = ptr + bufferLength;
193  unsigned char buf[8];
194 
195  int count = 0;
196 
197  if (*buffer == '$')
198  {
199  ptr++;
200  }
201  else
202  {
203  count += serialPortWrite(serialPort, (const unsigned char*)"$", 1);
204  }
205  count += serialPortWrite(serialPort, (const unsigned char*)buffer, bufferLength);
206 
207  while (ptr != ptrEnd)
208  {
209  checkSum ^= *ptr++;
210  }
211 
212 #ifdef _MSC_VER
213 
214 #pragma warning(push)
215 #pragma warning(disable: 4996)
216 
217 #endif
218 
219  sprintf((char*)buf, "*%.2x\r\n", checkSum);
220 
221 #ifdef _MSC_VER
222 
223 #pragma warning(pop)
224 
225 #endif
226 
227  count += serialPortWrite(serialPort, buf, 5);
228 
229  return count;
230 }
231 
232 int serialPortWriteAndWaitFor(serial_port_t* serialPort, const unsigned char* buffer, int writeCount, const unsigned char* waitFor, int waitForLength)
233 {
234  return serialPortWriteAndWaitForTimeout(serialPort, buffer, writeCount, waitFor, waitForLength, SERIAL_PORT_DEFAULT_TIMEOUT);
235 }
236 
237 int serialPortWriteAndWaitForTimeout(serial_port_t* serialPort, const unsigned char* buffer, int writeCount, const unsigned char* waitFor, int waitForLength, const int timeoutMilliseconds)
238 {
239  if (serialPort == 0 || serialPort->handle == 0 || buffer == 0 || writeCount < 1 || waitFor == 0 || waitForLength < 1)
240  {
241  return 0;
242  }
243 
244  int actuallyWrittenCount = serialPortWrite(serialPort, buffer, writeCount);
245 
246  if (actuallyWrittenCount != writeCount)
247  {
248  return 0;
249  }
250 
251  return serialPortWaitForTimeout(serialPort, waitFor, waitForLength, timeoutMilliseconds);
252 }
253 
254 int serialPortWaitFor(serial_port_t* serialPort, const unsigned char* waitFor, int waitForLength)
255 {
256  return serialPortWaitForTimeout(serialPort, waitFor, waitForLength, SERIAL_PORT_DEFAULT_TIMEOUT);
257 }
258 
259 int serialPortWaitForTimeout(serial_port_t* serialPort, const unsigned char* waitFor, int waitForLength, int timeoutMilliseconds)
260 {
261  if (serialPort == 0 || serialPort->handle == 0 || waitFor == 0 || waitForLength < 1)
262  {
263  return 1;
264  }
265  else if (waitForLength > 128)
266  {
267  return 0;
268  }
269 
270  unsigned char buf[128] = { 0 };
271  int count = serialPortReadTimeout(serialPort, buf, waitForLength, timeoutMilliseconds);
272 
273  if (count == waitForLength && memcmp(buf, waitFor, waitForLength) == 0)
274  {
275  return 1;
276  }
277  return 0;
278 }
279 
281 {
282  if (serialPort == 0 || serialPort->handle == 0 || serialPort->pfnGetByteCountAvailableToRead == 0)
283  {
284  return 0;
285  }
286 
287  return serialPort->pfnGetByteCountAvailableToRead(serialPort);
288 }
289 
291 {
292  if (serialPort == 0 || serialPort->handle == 0 || serialPort->pfnGetByteCountAvailableToWrite == 0)
293  {
294  return 0;
295  }
296 
297  return serialPort->pfnGetByteCountAvailableToWrite(serialPort);
298 }
299 
300 int serialPortSleep(serial_port_t* serialPort, int sleepMilliseconds)
301 {
302  if (serialPort == 0 || serialPort->handle == 0 || serialPort->pfnSleep == 0)
303  {
304  return 0;
305  }
306 
307  return serialPort->pfnSleep(serialPort, sleepMilliseconds);
308 }
int serialPortReadTimeoutAsync(serial_port_t *serialPort, unsigned char *buffer, int readCount, pfnSerialPortAsyncReadCompletion completion)
int serialPortOpen(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
uint32_t bufferLength
Definition: USBD.h:91
char port[MAX_SERIAL_PORT_NAME_LENGTH+1]
int serialPortSleep(serial_port_t *serialPort, int sleepMilliseconds)
pfnSerialPortAsyncRead pfnAsyncRead
#define sprintf
Definition: test_suite.cpp:72
int serialPortWrite(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
#define MAX_SERIAL_PORT_NAME_LENGTH
size_t count(InputIterator first, InputIterator last, T const &item)
Definition: catch.hpp:3206
pfnSerialPortGetByteCountAvailableToWrite pfnGetByteCountAvailableToWrite
int serialPortWriteAndWaitForTimeout(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength, const int timeoutMilliseconds)
int serialPortReadTimeout(serial_port_t *serialPort, unsigned char *buffer, int readCount, int timeoutMilliseconds)
int serialPortRead(serial_port_t *serialPort, unsigned char *buffer, int readCount)
int serialPortFlush(serial_port_t *serialPort)
void(* pfnSerialPortAsyncReadCompletion)(serial_port_t *serialPort, unsigned char *buf, int len, int errorCode)
int serialPortReadLineTimeout(serial_port_t *serialPort, unsigned char **result, int timeoutMilliseconds)
int serialPortWriteAndWaitFor(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength)
void free(void *ptr)
void * realloc(void *ptr, size_t size)
int serialPortWriteLine(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
void serialPortSetPort(serial_port_t *serialPort, const char *port)
int serialPortReadCharTimeout(serial_port_t *serialPort, unsigned char *c, int timeoutMilliseconds)
int serialPortReadLine(serial_port_t *serialPort, unsigned char **result)
int SERIAL_PORT_DEFAULT_TIMEOUT
int serialPortIsOpen(serial_port_t *serialPort)
int serialPortClose(serial_port_t *serialPort)
void * malloc(size_t size)
int serialPortGetByteCountAvailableToWrite(serial_port_t *serialPort)
int serialPortWaitFor(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength)
int serialPortWriteAscii(serial_port_t *serialPort, const char *buffer, int bufferLength)
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 serialPortGetByteCountAvailableToRead(serial_port_t *serialPort)


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