VectorNav C Library
serialport.h
1 #ifndef VN_SERIALPORT_H
2 #define VN_SERIALPORT_H
3 
6 #include "vn/int.h"
7 #include "vn/error.h"
8 #include "vn/bool.h"
9 #include "vn/xplat/thread.h"
11 
12 #if defined(_WIN32)
13 
14  /* Disable some warnings for Visual Studio with -Wall. */
15  #if defined(_MSC_VER)
16  #pragma warning(push)
17  #pragma warning(disable:4668)
18  #pragma warning(disable:4820)
19  #pragma warning(disable:4255)
20  #endif
21 
22  #include <Windows.h>
23 
24  #if defined(_MSC_VER)
25  #pragma warning(pop)
26  #endif
27 #endif
28 
29 #ifdef __linux__
30  #include <linux/serial.h>
31 #elif defined __APPLE__
32  #include <dirent.h>
33 #endif
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
40 typedef void (*VnSerialPort_DataReceivedHandler)(void *userData);
41 
42 #ifdef _WIN32
43 #pragma warning(push)
44 #pragma warning(disable : 4820)
45 #endif
46 
48 typedef struct
49 {
50  #ifdef _WIN32
51  HANDLE handle;
52  /* Windows appears to need single-thread access to read/write API functions. */
53  VnCriticalSection readWriteCS;
54  #elif (defined __linux__ || defined __APPLE__ || defined __CYGWIN__ || defined __QNXNTO__)
55  int handle;
56  #else
57  #error "Unknown System"
58  #endif
59 
60  bool purgeFirstDataBytesWhenSerialPortIsFirstOpened;
61 
62  VnSerialPort_DataReceivedHandler dataReceivedHandler;
63 
64  void *dataReceivedHandlerUserData;
65 
66  VnThread serialPortNotificationsThread;
67 
68  bool continueHandlingSerialPortEvents;
69 
70  size_t numberOfReceiveDataDroppedSections;
71 
72  VnCriticalSection dataReceivedHandlerCriticalSection;
73 
74  char portName[50];
75 
76 } VnSerialPort;
77 
78 #ifdef _WIN32
79 #pragma warning(pop)
80 #endif
81 
86 VnError VnSerialPort_initialize(VnSerialPort *serialport);
87 
94 VnError VnSerialPort_open(VnSerialPort *serialport, char const *portName, uint32_t baudrate);
95 
100 VnError VnSerialPort_close(VnSerialPort *serialport);
101 
106 bool VnSerialPort_isOpen(VnSerialPort *serialport);
107 
115 VnError VnSerialPort_read(VnSerialPort *serialport, char *buffer, size_t numOfBytesToRead, size_t *numOfBytesActuallyRead);
116 
123 VnError VnSerialPort_write(VnSerialPort *serialport, char const *data, size_t numOfBytesToWrite);
124 
130 VnError VnSerialPort_changeBaudrate(VnSerialPort *serialport, uint32_t baudrate);
131 
138 VnError VnSerialPort_registerDataReceivedHandler(VnSerialPort *serialPort, VnSerialPort_DataReceivedHandler handler, void *userData);
139 
143 VnError VnSerialPort_unregisterDataReceivedHandler(VnSerialPort *serialPort);
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #endif
Provides access to a serial port.
Definition: serialport.h:48
Definition: criticalsection.h:38
Structure for working with threads.
Definition: thread.h:40