chenke
2017-08-09 4d22d8d9332535a9c2f65e0e501a3eefbbd0ae30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright 2012 - 2014 Thomas Buck <xythobuz@xythobuz.de>
 */
 
#ifndef _SERIAL_H_
#define _SERIAL_H_
 
/*
 * Configuration
 */
 
/*!
 * \brief Enable XON/XOFF flow control.
 *
 * If you uncomment this definition, the serial port code will
 * stop sending when a XOFF was received, and start again upon
 * receiving XON. However, you need to use the blocking
 * read/write functions!
 */
//#define XONXOFF
#define XON 0x11  //!< XON flow control character
#define XOFF 0x13 //!< XOFF flow control character
 
/*!
 * \brief Search term to filter the list of available ports.
 *
 * If you define SEARCH, instead of simply returning a list of
 * files in /dev/, getSerialPorts() will only return items that
 * contain the string defined to SEARCH.
 */
#define SEARCH "tty"
 
/*!
 * \brief Only list real serial ports.
 *
 * If you uncomment this definition, getSerialPorts() will try to
 * open every port, only returning the name if it is a real serial
 * port. This could cause a big delay, if eg. your system tries to
 * open non-existing bluetooth devices, waiting for their timeout.
 * Also, if your console tty is probed, it might change it's settings.
 */
//#define TRY_TO_OPEN_PORTS
 
/*!
 * \brief The timeout in seconds for raw reading/writing.
 *
 * If this amount of time passes without being able to write/read
 * a character, the raw I/O functions will return 0.
 */
#define TIMEOUT 2
 
/*
 * Setup
 */
 
/*!
 * \brief open a serial port
 * \param port name of port
 * \param baud baudrate
 * \returns file handle or -1 on error
 */
int serialOpen(const char *port, unsigned int baud);
 
/*!
 * \brief close an open serial port
 * \param fd file handle of port to close
 */
void serialClose(int fd);
 
/*!
 * \brief query available serial ports
 * \returns string array with serial port names.
 * Last element is NULL. Don't forget to free()
 * after using it!
 */
char **getSerialPorts(void);
 
/*
 * Raw, non-blocking I/O
 */
 
/*!
 * \brief read from an open serial port
 * \param fd file handle of port to read from
 * \param data buffer big enough to fit all read data
 * \param length maximum number of bytes to read
 * \returns number of bytes really read
 */
unsigned int serialReadRaw(int fd, char *data, int length);
 
/*!
 * \brief write to an open serial port
 * \param fd file handle of port to write to
 * \param data buffer containing data to write
 * \param length number of bytes to write
 * \returns number of bytes really written
 */
unsigned int serialWriteRaw(int fd, const char *data, int length);
 
/*!
 * \brief wait until data is sent
 * \param fd file handle of port to wait for
 */
void serialWaitUntilSent(int fd);
 
/*
 * Blocking I/O
 */
 
/*!
 * \brief check if a character has arrived and can be read
 * \param fd file handle of port to check
 * \returns 1 if a character is available, 0 if not
 */
int serialHasChar(int fd);
 
/*!
 * \brief read a single character
 * \param fd file handle of port to read from
 * \param c where read character will be stored
 */
void serialReadChar(int fd, char *c);
 
/*!
 * \brief write a single character
 * \param fd file handle to write to
 * \param c character to write
 */
void serialWriteChar(int fd, char c);
 
/*!
 * \brief write a string
 * \param fd file handle to write to
 * \param s C string to be written
 */
void serialWriteString(int fd, const char *s);
 
#endif