-
Notifications
You must be signed in to change notification settings - Fork 2
/
SerialDevice.hh
executable file
·107 lines (78 loc) · 4.04 KB
/
SerialDevice.hh
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
/* Driver for reading data from a serial port
Aleksandr Kushleyev <akushley(at)seas(dot)upenn(dot)edu>
University of Pennsylvania, 2008
BSD license.
--------------------------------------------------------------------
Copyright (c) 2008 Aleksandr Kushleyev
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SERIAL_DEVICE_HH
#define SERIAL_DEVICE_HH
//#define SERIAL_DEVICE_DEBUG
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <sys/time.h>
#include <errno.h>
#define MAX_DEVICE_NAME_LENGTH 128
#define DEFAULT_READ_TIMEOUT_US 1000000
//list the io modes here
enum {IO_BLOCK_W_TIMEOUT,IO_NONBLOCK_POLL_W_DELAY_W_TIMEOUT,IO_BLOCK_WO_TIMEOUT,IO_NONBLOCK_WO_TIMEOUT,IO_BLOCK_W_TIMEOUT_W_TERM_SEQUENCE,
IO_BLOCK_WO_TIMEOUT_W_TERM_SEQUENCE};
#define DEFAULT_IO_MODE IO_BLOCK_W_TIMEOUT
#define MAX_NUM_TERM_CHARS 128
class SerialDevice{
public:
SerialDevice();
~SerialDevice();
int Connect(const char * device, int speed); //connect to the device and set the baud rate
int Disconnect(); //disconnect from the device
int SetBaudRate(int baud); //set the baud rate
bool IsConnected();
int FlushInputBuffer(); //flush input buffer
int ReadChars(char * data, int byte_count, int timeout_us=DEFAULT_READ_TIMEOUT_US); //read a number of characters
int WriteChars(const char * data, int byte_count, int delay_us=0); //write a number of characters
int Set_IO_BLOCK_W_TIMEOUT();
int Set_IO_NONBLOCK_POLL_W_DELAY_W_TIMEOUT(int delay);
int Set_IO_BLOCK_WO_TIMEOUT();
int Set_IO_NONBLOCK_WO_TIMEOUT();
int Set_IO_BLOCK_W_TIMEOUT_W_TERM_SEQUENCE(const char * termSequence, int numTermChars, bool retTermSequence=true);
int Set_IO_BLOCK_WO_TIMEOUT_W_TERM_SEQUENCE(const char * termSequence, int numTermChars, bool retTermSequence=true);
private:
char _device[MAX_DEVICE_NAME_LENGTH]; //device name
speed_t _baud; //baud rate
int _fd; //file descriptor
bool _connected; //status
int _block; //block / non-block IO
int _ioMode, _delay_us, _numTermChars;
char _termSequence[MAX_NUM_TERM_CHARS];
bool _retTermSequence;
int _SetBlockingIO(); //set blocking IO
int _SetNonBlockingIO(); //set non-blocking IO
int _SpeedToBaud(int speed, speed_t & baud); //convert integer speed to baud rate setting
struct termios _oldterm,_newterm; //terminal structs
};
#endif //SERIAL_DEVICE_HH