serialPort.h
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2021 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 #ifndef __IS_SERIALPORT_H
14 #define __IS_SERIALPORT_H
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
25 
26 #define MAX_SERIAL_PORT_NAME_LENGTH 63
27 
28 // Standard Baud Rates - FTDI Functional. // Bit period = 1/baudrate, Actual baud (FTDI,AVR,ARM)
29 #define BAUDRATE_300 300 // 3333 us
30 #define BAUDRATE_600 600 // 1667 us
31 #define BAUDRATE_1200 1200 // 833 us
32 #define BAUDRATE_2400 2400 // 417 us
33 #define BAUDRATE_4800 4800 // 208 us
34 #define BAUDRATE_9600 9600 // 104 us
35 #define BAUDRATE_19200 19200 // 52 us
36 #define BAUDRATE_38400 38400 // 26 us
37 #define BAUDRATE_57600 57600 // 17 us
38 #define BAUDRATE_115200 115200 // 8680 ns
39 #define BAUDRATE_230400 230400 // 4340 ns
40 #define BAUDRATE_460800 460800 // 2170 ns
41 #define BAUDRATE_921600 921600 // 1085 ns
42 #define BAUDRATE_1000000 1000000 // 1000 ns
43 #define BAUDRATE_1220000 1220000 // 820 ns
44 #define BAUDRATE_1440000 1440000 // 794 ns
45 #define BAUDRATE_1500000 1500000 // 667 ns (FTDI 1520, AFR 1500)
46 #define BAUDRATE_2000000 2000000 // 500 ns (FTDI 2080, AVR/ARM 2016)
47 #define BAUDRATE_3000000 3000000 // 333 ns (FTDI 3150, AVR/ARM 3030)
48 
50 
51 typedef int(*pfnSerialPortOpen)(serial_port_t* serialPort, const char* port, int baudRate, int blocking);
52 typedef int(*pfnSerialPortIsOpen)(serial_port_t* serialPort);
53 typedef int(*pfnSerialPortRead)(serial_port_t* serialPort, unsigned char* buf, int len, int timeoutMilliseconds);
54 typedef void(*pfnSerialPortAsyncReadCompletion)(serial_port_t* serialPort, unsigned char* buf, int len, int errorCode);
55 typedef int(*pfnSerialPortAsyncRead)(serial_port_t* serialPort, unsigned char* buf, int len, pfnSerialPortAsyncReadCompletion completion);
56 typedef int(*pfnSerialPortWrite)(serial_port_t* serialPort, const unsigned char* buf, int len);
57 typedef int(*pfnSerialPortClose)(serial_port_t* serialPort);
58 typedef int(*pfnSerialPortFlush)(serial_port_t* serialPort);
61 typedef int(*pfnSerialPortSleep)(int sleepMilliseconds);
62 
63 // Allows communicating over a serial port
65 {
66  // platform specific handle
67  void* handle;
68 
69  // the port name (do not modify directly)
71 
72  // optional error buffer to store errors
73  char* error;
74 
75  // length of error
77 
78  // open the serial port
80 
81  // is the serial port open?
83 
84  // read data synchronously
86 
87  // read data asynchronously
89 
90  // write data synchronously
92 
93  // close the serial port
95 
96  // remove all data from all buffers
98 
99  // get number of bytes in the receive buffer that can be read
101 
102  // get the number of available bytes in the send buffer
104 
105  // sleep for a specified number of milliseconds
107 };
108 
109 // set the port name for a serial port, in case you are opening it later
110 void serialPortSetPort(serial_port_t* serialPort, const char* port);
111 
112 // open a serial port
113 // port is null terminated, i.e. COM1\0, COM2\0, etc.
114 // use blocking = 0 when data is being streamed from the serial port rapidly and blocking = 1 for
115 // uses such as a boot loader where a write would then require n bytes to be read in a single operation.
116 // blocking simply determines the default timeout value of the serialPortRead function
117 // returns 1 if success, 0 if failure
118 int serialPortOpen(serial_port_t* serialPort, const char* port, int baudRate, int blocking);
119 
120 // open a serial port with retry
121 // port is null terminated, i.e. COM1\0, COM2\0, etc.
122 // use blocking = 0 when data is being streamed from the serial port rapidly and blocking = 1 for
123 // uses such as a boot loader where a write would then require n bytes to be read in a single operation.
124 // blocking simply determines the default timeout value of the serialPortRead function
125 // returns 1 if success, 0 if failure
126 int serialPortOpenRetry(serial_port_t* serialPort, const char* port, int baudRate, int blocking);
127 
128 // check if the port is open
129 // returns 1 if open, 0 if not open
130 int serialPortIsOpen(serial_port_t* serialPort);
131 
132 // close the serial port - this object can be re-used by calling open again, returns 1 if closed and returns 0 if the port was not closed
133 int serialPortClose(serial_port_t* serialPort);
134 
135 // clear all buffers and pending reads and writes - returns 1 if success, 0 if failure
136 int serialPortFlush(serial_port_t* serialPort);
137 
138 // read up to readCount bytes into buffer
139 // call is forwarded to serialPortReadTimeout with timeoutMilliseconds of 0 for non-blocking, or SERIAL_PORT_DEFAULT_TIMEOUT for blocking
140 int serialPortRead(serial_port_t* serialPort, unsigned char* buffer, int readCount);
141 
142 // read up to thue number of bytes requested, returns number of bytes read which is less than or equal to readCount
143 int serialPortReadTimeout(serial_port_t* serialPort, unsigned char* buffer, int readCount, int timeoutMilliseconds);
144 
145 // start an async read - not all platforms will support an async read and may call the callback function immediately
146 // reads up to readCount bytes into buffer
147 // buffer must exist until callback is executed, if it needs to be freed, free it in the callback or later
148 // returns 1 if success, 0 if failed to start async operation
149 int serialPortReadTimeoutAsync(serial_port_t* serialPort, unsigned char* buffer, int readCount, pfnSerialPortAsyncReadCompletion callback);
150 
151 // read up until a \r\n sequence has been read
152 // buffer will not contain \r\n sequence
153 // returns number of bytes read or -1 if timeout or buffer overflow, count does not include the null terminator
154 int serialPortReadLine(serial_port_t* serialPort, unsigned char* buffer, int bufferLength);
155 
156 // read up until a \r\n sequence has been read
157 // result will not contain \r\n sequence
158 // returns number of bytes read or -1 if timeout or buffer overflow, count does not include the null terminator
159 int serialPortReadLineTimeout(serial_port_t* serialPort, unsigned char* buffer, int bufferLength, int timeoutMilliseconds);
160 
161 // read ASCII data (starts with $ and ends with \r\n, based on NMEA format)
162 // will ignore data that fails checksum
163 // asciiData gets set to start of ASCII data
164 // return -1 if timeout or buffer overflow or checksum failure
165 int serialPortReadAscii(serial_port_t* serialPort, unsigned char* buffer, int bufferLength, unsigned char** asciiData);
166 
167 // read ASCII data (starts with $ and ends with \r\n, based on NMEA format)
168 // will ignore data that fails checksum
169 // asciiData gets set to start of ASCII data
170 // return -1 if timeout or buffer overflow or checksum failure
171 int serialPortReadAsciiTimeout(serial_port_t* serialPort, unsigned char* buffer, int bufferLength, int timeoutMilliseconds, unsigned char** asciiData);
172 
173 // read one char, waiting SERIAL_PORT_DEFAULT_TIMEOUT milliseconds to get a char
174 int serialPortReadChar(serial_port_t* serialPort, unsigned char* c);
175 
176 // read one char, waiting timeoutMilliseconds to get a char, returns number of chars read
177 int serialPortReadCharTimeout(serial_port_t* serialPort, unsigned char* c, int timeoutMilliseconds);
178 
179 // write, returns the number of bytes written
180 int serialPortWrite(serial_port_t* serialPort, const unsigned char* buffer, int writeCount);
181 
182 // write with a \r\n added at the end, \r\n should not be part of buffer, returns the number of bytes written
183 int serialPortWriteLine(serial_port_t* serialPort, const unsigned char* buffer, int writeCount);
184 
185 // write ascii data - if buffer does not start with $, a $ will be written first, followed by buffer, followed by *xx\r\n, where xx is a two hex character checksum
186 int serialPortWriteAscii(serial_port_t* serialPort, const char* buffer, int bufferLength);
187 
188 // write and wait for a response, returns 1 if success, 0 if failure
189 int serialPortWriteAndWaitFor(serial_port_t* serialPort, const unsigned char* buffer, int writeCount, const unsigned char* waitFor, int waitForLength);
190 int serialPortWriteAndWaitForTimeout(serial_port_t* serialPort, const unsigned char* buffer, int writeCount, const unsigned char* waitFor, int waitForLength, const int timeoutMilliseconds);
191 
192 // wait for a response, returns 0 if failure, 1 if success
193 int serialPortWaitFor(serial_port_t* serialPort, const unsigned char* waitFor, int waitForLength);
194 int serialPortWaitForTimeout(serial_port_t* serialPort, const unsigned char* waitFor, int waitForLength, int timeoutMilliseconds);
195 
196 // get available bytes in the receive buffer
198 
199 // get available bytes in the send buffer
201 
202 // sleep for the specified number of milliseconds if supported, returns 1 if success, 0 if failed to sleep
203 int serialPortSleep(serial_port_t* serialPort, int sleepMilliseconds);
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif // __IS_SERIALPORT_H
int serialPortReadAsciiTimeout(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, int timeoutMilliseconds, unsigned char **asciiData)
Definition: serialPort.c:150
void serialPortSetPort(serial_port_t *serialPort, const char *port)
Definition: serialPort.c:18
uint32_t bufferLength
Definition: USBD.h:91
int(* pfnSerialPortAsyncRead)(serial_port_t *serialPort, unsigned char *buf, int len, pfnSerialPortAsyncReadCompletion completion)
Definition: serialPort.h:55
pfnSerialPortIsOpen pfnIsOpen
Definition: serialPort.h:82
pfnSerialPortAsyncRead pfnAsyncRead
Definition: serialPort.h:88
int(* pfnSerialPortWrite)(serial_port_t *serialPort, const unsigned char *buf, int len)
Definition: serialPort.h:56
int serialPortOpen(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
Definition: serialPort.c:28
void(* pfnSerialPortAsyncReadCompletion)(serial_port_t *serialPort, unsigned char *buf, int len, int errorCode)
Definition: serialPort.h:54
int serialPortWriteAndWaitFor(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength)
Definition: serialPort.c:281
pfnSerialPortGetByteCountAvailableToWrite pfnGetByteCountAvailableToWrite
Definition: serialPort.h:103
pfnSerialPortOpen pfnOpen
Definition: serialPort.h:79
int serialPortRead(serial_port_t *serialPort, unsigned char *buffer, int readCount)
Definition: serialPort.c:84
pfnSerialPortRead pfnRead
Definition: serialPort.h:85
int(* pfnSerialPortFlush)(serial_port_t *serialPort)
Definition: serialPort.h:58
pfnSerialPortFlush pfnFlush
Definition: serialPort.h:97
int serialPortReadTimeoutAsync(serial_port_t *serialPort, unsigned char *buffer, int readCount, pfnSerialPortAsyncReadCompletion callback)
Definition: serialPort.c:106
char port[MAX_SERIAL_PORT_NAME_LENGTH+1]
Definition: serialPort.h:70
#define MAX_SERIAL_PORT_NAME_LENGTH
Definition: serialPort.h:26
int serialPortReadLineTimeout(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, int timeoutMilliseconds)
Definition: serialPort.c:121
char * error
Definition: serialPort.h:73
int serialPortReadAscii(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, unsigned char **asciiData)
Definition: serialPort.c:145
int(* pfnSerialPortRead)(serial_port_t *serialPort, unsigned char *buf, int len, int timeoutMilliseconds)
Definition: serialPort.h:53
int serialPortReadLine(serial_port_t *serialPort, unsigned char *buffer, int bufferLength)
Definition: serialPort.c:116
int serialPortIsOpen(serial_port_t *serialPort)
Definition: serialPort.c:57
int serialPortWaitForTimeout(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength, int timeoutMilliseconds)
Definition: serialPort.c:308
int SERIAL_PORT_DEFAULT_TIMEOUT
Definition: serialPort.c:16
int serialPortFlush(serial_port_t *serialPort)
Definition: serialPort.c:75
int serialPortWriteLine(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
Definition: serialPort.c:218
void * handle
Definition: serialPort.h:67
int(* pfnSerialPortGetByteCountAvailableToRead)(serial_port_t *serialPort)
Definition: serialPort.h:59
int serialPortGetByteCountAvailableToWrite(serial_port_t *serialPort)
Definition: serialPort.c:339
pfnSerialPortSleep pfnSleep
Definition: serialPort.h:106
int serialPortReadChar(serial_port_t *serialPort, unsigned char *c)
Definition: serialPort.c:191
int serialPortClose(serial_port_t *serialPort)
Definition: serialPort.c:66
int serialPortReadTimeout(serial_port_t *serialPort, unsigned char *buffer, int readCount, int timeoutMilliseconds)
Definition: serialPort.c:89
int(* pfnSerialPortOpen)(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
Definition: serialPort.h:51
pfnSerialPortClose pfnClose
Definition: serialPort.h:94
pfnSerialPortGetByteCountAvailableToRead pfnGetByteCountAvailableToRead
Definition: serialPort.h:100
int serialPortWriteAscii(serial_port_t *serialPort, const char *buffer, int bufferLength)
Definition: serialPort.c:230
int serialPortSleep(serial_port_t *serialPort, int sleepMilliseconds)
Definition: serialPort.c:349
int serialPortOpenRetry(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
Definition: serialPort.c:37
int serialPortReadCharTimeout(serial_port_t *serialPort, unsigned char *c, int timeoutMilliseconds)
Definition: serialPort.c:196
pfnSerialPortWrite pfnWrite
Definition: serialPort.h:91
int serialPortWriteAndWaitForTimeout(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength, const int timeoutMilliseconds)
Definition: serialPort.c:286
int(* pfnSerialPortSleep)(int sleepMilliseconds)
Definition: serialPort.h:61
int(* pfnSerialPortIsOpen)(serial_port_t *serialPort)
Definition: serialPort.h:52
int(* pfnSerialPortClose)(serial_port_t *serialPort)
Definition: serialPort.h:57
int serialPortGetByteCountAvailableToRead(serial_port_t *serialPort)
Definition: serialPort.c:329
int(* pfnSerialPortGetByteCountAvailableToWrite)(serial_port_t *serialPort)
Definition: serialPort.h:60
int serialPortWaitFor(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength)
Definition: serialPort.c:303
int serialPortWrite(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
Definition: serialPort.c:201


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58