[go: up one dir, main page]

Menu

[r888]: / trunk / src / common / com.h  Maximize  Restore  History

Download this file

310 lines (239 with data), 6.6 kB

  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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*!
* \file com.h
* \brief interface for the Linux comm class.
* \author Ake Hedman, CC Systems AB,
* \author akhe@users.sourceforge.net
* \date 2002-2003
*
// Copyright (C) 2000-2008 Ake Hedman, eurosource, <akhe@eurosource.se>
//
// This software is placed into
// the public domain and may be used for any purpose. However, this
// notice must not be changed or removed and no warranty is either
// expressed or implied by its publication or distribution.
*
* This class wraps and extends Minicom.
* see ftp://ftp.sunsite.unc.edu/pub/Linux/apps/serialcomm/dialout
*
* Some changes by akhe@brattberg.com ( www.brattberg.com ) done for the
* M.U.M.I.N. protocol.
*
* - Sorry no more sysdep. Works only on Linux systems ( maybe on others ).
*
*/
// $RCSfile: com.h,v $
// $Date: 2005/08/30 11:00:13 $
// $Author: akhe $
// $Revision: 1.3 $
#ifndef H_COM_H
#define H_COM_H
#include <termios.h>
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
/*! \class CComm
* \brief A class that handles serial port communication.
* \author Mickael Ericsson
* \version 1
* \date 2002-2003
*/
class Comm
{
public:
// Constructors
/// Constructor
Comm( void );
/*!
Constructor
\param Device Open the named device
*/
Comm(char *Device) { m_fd = 0; open( Device ); }
/*!
Constructor
\param fd Open a device from a descriptor
*/
Comm(int fd) { m_fd = fd; }
/// Destructor
virtual ~Comm( void );
/*!
Open the communication device
\parm szDevice Name of communication device to open.
\return True on success.
*/
virtual bool open( char* szDevice = NULL );
/*!
Close the communication device
*/
void close( void ) { ::close( m_fd ); m_fd = 0; }
/*!
Get file descriptor
*/
int getFD( void ) { return m_fd; }
/*!
Set port parameters
\param baud Pointer to baudrate string.
\param parity Parity (N for none, E for even, O for odd, M for mark, S for space).
\param bits Number of bits.
\param HWFlow Set to zero to disable hardware flow control.
\param SWFlow Set to zero to disable software flow control.
*/
void setParam( char *baud,
char *parity,
char *bits,
int HWFlow=0,
int SWFlow=0 );
/*!
Set hardware flow control
\param on set to non-zeroe to enable hardware handshake.
*/
void setHWFlow( int on = true );
/*!
Get maximum baud rate
\return Return the maximum supported baudrate.
*/
int getMaxBaud( void );
/*!
Is there a character waiting in the inque
\return No-zero if there is a character to read from the communication channel.
*/
int isCharReady( void );
/*!
Turn on DTR
*/
void DtrOn( void );
/*!
Turn off DTR
*/
void DtrOff( void );
/*!
Turn on RTS
*/
void RtsOn( void );
/*!
Turn off RTS
*/
void RtsOff( void );
/*!
Turn of DTR for a specific period.
\param delay Time that DTR should be off.
*/
void ToggleDTR( int delay );
/*!
Get Data Carrier Detect status
*/
int getDCD( void );
/*!
Send break
*/
void SendBreak( void );
/*!
Flush the send que
*/
void Flush( void );
/*!
Drain the send que
*/
void Drain( void );
/*!
Flush inQue
*/
void FlushInQue( void );
/*!
Get charcter
\return Get character from communication channel.
*/
unsigned char comm_getc( void );
/*!
Send a character
\param c Character to send on communication channel.
\param bDrain Set to TRUE if we should wait for the character to be sent.
*/
void comm_putc( unsigned char c, bool bDrain = false );
/*!
Get a string of characters
\param Buffer Buffer for received data.
\param max Maximum number of characters for the buffer.
\return Non zeror if OK, zeror on failure.
*/
int comm_gets( char *Buffer, int max );
/*!
Get a string of characters with timeout
\param Buffer for received data.
\param max Max number of characters the buffer can hold.
\param timeout Time in millieconds we should wait.
\return Non zero if OK, zeror on failure.
*/
int comm_gets( char *Buffer, int nChars, long timeout );
/*!
Send a string of characters
\param Buffer Data to send.
\param bDrain Set to TRUE if we should wait for the character to be sent.
\return Non zero if OK, zeror on failure.
*/
int comm_puts( char *Buffer, bool bDrain=false );
/*!
Send an array of characters
\param Buffer Buffer for data to send.
\paran len Number of bytes to send.
\param bDrain Set to TRUE if we should wait for the character to be sent.
\return Non zero if OK, zeror on failure.
*/
int comm_puts( char *Buffer, int len, bool bDrain=false );
/*!
Get Line Status Register Transmitter Empty bit.
\return Non zero if UART transmitter is empty.
*/
int isTransmitterEmpty( void );
/*!
Get the current millisecond tick count.
\return Current CPU tick count.
*/
long msGettick( void );
/*!
Delay for at least 'len' ms
\param len Number of milliseconds to delay.
*/
void msDelay( int len );
// Windows compatible methods
/*!
Write a character out on the communication channel
\param b Character to write.
*/
void writeChar( unsigned char b ) { comm_putc( b ); };
/*!
Read a character form the communication channel
\param cnt 0 if no character to read, 1 if character read.
\return Character read .
*/
char readChar( int *cnt );
/*!
Drain the input queue
*/
void drainInput( void );
protected:
/*!
Set the error string (Internal)
\param p Pointer to error string to set
*/
void setErrorStr( char *p ) { strncpy( m_szerr, p, 256 ); };
public:
/// mutex for exclusive use of the Com object
pthread_mutex_t m_mutex;
protected:
/// Serial device name
char m_szDevice[64];
/// Serial device handle
int m_fd;
/// Holder for error string
char m_szerr[256];
};
#endif // H_COM_H