serial/serialPort.h
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 #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)(serial_port_t* serialPort, int sleepMilliseconds);
62 
63 // Allows communicating over a serial port
64 struct serial_port_t
65 {
66  // platform specific handle
67  void* handle;
68 
69  // the port name (do not modify directly)
70  char port[MAX_SERIAL_PORT_NAME_LENGTH + 1];
71 
72  // optional error buffer to store errors
73  char* error;
74 
75  // length of error
76  int errorLength;
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 // check if the port is open
121 // returns 1 if open, 0 if not open
122 int serialPortIsOpen(serial_port_t* serialPort);
123 
124 // 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
125 int serialPortClose(serial_port_t* serialPort);
126 
127 // clear all buffers and pending reads and writes - returns 1 if success, 0 if failure
128 int serialPortFlush(serial_port_t* serialPort);
129 
130 // read up to readCount bytes into buffer
131 // call is forwarded to serialPortReadTimeout with timeoutMilliseconds of 0 for non-blocking, or SERIAL_PORT_DEFAULT_TIMEOUT for blocking
132 int serialPortRead(serial_port_t* serialPort, unsigned char* buffer, int readCount);
133 
134 // read up to thue number of bytes requested, returns number of bytes read which is less than or equal to readCount
135 int serialPortReadTimeout(serial_port_t* serialPort, unsigned char* buffer, int readCount, int timeoutMilliseconds);
136 
137 // start an async read - not all platforms will support an async read and may call the callback function immediately
138 // reads up to readCount bytes into buffer
139 // buffer must exist until callback is executed, if it needs to be freed, free it in the callback or later
140 // returns 1 if success, 0 if failed to start async operation
141 int serialPortReadTimeoutAsync(serial_port_t* serialPort, unsigned char* buffer, int readCount, pfnSerialPortAsyncReadCompletion callback);
142 
143 // read up until a \r\n sequence has been read
144 // result will be allocated using malloc and you will need to call free
145 // result will not contain \r\n sequence
146 // returns number of bytes read or -1 if timeout
147 int serialPortReadLine(serial_port_t* serialPort, unsigned char** result);
148 
149 // read up until a \r\n sequence has been read
150 // result will be allocated using malloc and you will need to call free
151 // result will not contain \r\n sequence
152 // returns number of bytes read or -1 if timeout
153 int serialPortReadLineTimeout(serial_port_t* serialPort, unsigned char** result, int timeoutMilliseconds);
154 
155 // read one char, waiting SERIAL_PORT_DEFAULT_TIMEOUT milliseconds to get a char
156 int serialPortReadChar(serial_port_t* serialPort, unsigned char* c);
157 
158 // read one char, waiting timeoutMilliseconds to get a char, returns number of chars read
159 int serialPortReadCharTimeout(serial_port_t* serialPort, unsigned char* c, int timeoutMilliseconds);
160 
161 // write, returns the number of bytes written
162 int serialPortWrite(serial_port_t* serialPort, const unsigned char* buffer, int writeCount);
163 
164 // write with a \r\n added at the end, \r\n should not be part of buffer, returns the number of bytes written
165 int serialPortWriteLine(serial_port_t* serialPort, const unsigned char* buffer, int writeCount);
166 
167 // 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
168 int serialPortWriteAscii(serial_port_t* serialPort, const char* buffer, int bufferLength);
169 
170 // write and wait for a response, returns 1 if success, 0 if failure
171 int serialPortWriteAndWaitFor(serial_port_t* serialPort, const unsigned char* buffer, int writeCount, const unsigned char* waitFor, int waitForLength);
172 int serialPortWriteAndWaitForTimeout(serial_port_t* serialPort, const unsigned char* buffer, int writeCount, const unsigned char* waitFor, int waitForLength, const int timeoutMilliseconds);
173 
174 // wait for a response, returns 0 if failure, 1 if success
175 int serialPortWaitFor(serial_port_t* serialPort, const unsigned char* waitFor, int waitForLength);
176 int serialPortWaitForTimeout(serial_port_t* serialPort, const unsigned char* waitFor, int waitForLength, int timeoutMilliseconds);
177 
178 // get available bytes in the receive buffer
180 
181 // get available bytes in the send buffer
183 
184 // sleep for the specified number of milliseconds if supported, returns 1 if success, 0 if failed to sleep
185 int serialPortSleep(serial_port_t* serialPort, int sleepMilliseconds);
186 
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 #endif // __IS_SERIALPORT_H
int(* pfnSerialPortGetByteCountAvailableToRead)(serial_port_t *serialPort)
int(* pfnSerialPortAsyncRead)(serial_port_t *serialPort, unsigned char *buf, int len, pfnSerialPortAsyncReadCompletion completion)
uint32_t bufferLength
Definition: USBD.h:91
int serialPortOpen(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
char port[MAX_SERIAL_PORT_NAME_LENGTH+1]
int serialPortFlush(serial_port_t *serialPort)
#define MAX_SERIAL_PORT_NAME_LENGTH
pfnSerialPortAsyncRead pfnAsyncRead
int(* pfnSerialPortIsOpen)(serial_port_t *serialPort)
int serialPortReadLineTimeout(serial_port_t *serialPort, unsigned char **result, int timeoutMilliseconds)
int serialPortIsOpen(serial_port_t *serialPort)
void(* pfnSerialPortAsyncReadCompletion)(serial_port_t *serialPort, unsigned char *buf, int len, int errorCode)
int(* pfnSerialPortGetByteCountAvailableToWrite)(serial_port_t *serialPort)
pfnSerialPortGetByteCountAvailableToWrite pfnGetByteCountAvailableToWrite
int(* pfnSerialPortWrite)(serial_port_t *serialPort, const unsigned char *buf, int len)
int(* pfnSerialPortOpen)(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
int serialPortWriteLine(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
void serialPortSetPort(serial_port_t *serialPort, const char *port)
int(* pfnSerialPortFlush)(serial_port_t *serialPort)
int serialPortWriteAndWaitFor(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength)
int serialPortWaitFor(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength)
int serialPortReadTimeoutAsync(serial_port_t *serialPort, unsigned char *buffer, int readCount, pfnSerialPortAsyncReadCompletion callback)
int serialPortWrite(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
int(* pfnSerialPortSleep)(serial_port_t *serialPort, int sleepMilliseconds)
int serialPortReadCharTimeout(serial_port_t *serialPort, unsigned char *c, int timeoutMilliseconds)
int serialPortWriteAndWaitForTimeout(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength, const int timeoutMilliseconds)
int serialPortRead(serial_port_t *serialPort, unsigned char *buffer, int readCount)
int serialPortWriteAscii(serial_port_t *serialPort, const char *buffer, int bufferLength)
int serialPortGetByteCountAvailableToRead(serial_port_t *serialPort)
int serialPortReadChar(serial_port_t *serialPort, unsigned char *c)
pfnSerialPortGetByteCountAvailableToRead pfnGetByteCountAvailableToRead
int serialPortClose(serial_port_t *serialPort)
int(* pfnSerialPortClose)(serial_port_t *serialPort)
int serialPortReadTimeout(serial_port_t *serialPort, unsigned char *buffer, int readCount, int timeoutMilliseconds)
int serialPortWaitForTimeout(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength, int timeoutMilliseconds)
int serialPortSleep(serial_port_t *serialPort, int sleepMilliseconds)
int serialPortGetByteCountAvailableToWrite(serial_port_t *serialPort)
int(* pfnSerialPortRead)(serial_port_t *serialPort, unsigned char *buf, int len, int timeoutMilliseconds)
int serialPortReadLine(serial_port_t *serialPort, unsigned char **result)


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