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
|
// SerialComm.h: interface for the Cserial class.
//
//////////////////////////////////////////////////////////////////////
#ifndef SERIAL_H
#define SERIAL_H
#include <string>
void com_to_tty(std::string& port);
void tty_to_com(std::string& port);
#ifndef __MINGW32__
#include <termios.h>
class Cserial {
public:
Cserial();
~Cserial();
//Methods
bool OpenPort();
bool IsOpen() { return fd < 0 ? 0 : 1; };
void ClosePort();
void Device (std::string dev) { device = dev;};
std::string Device() { return device;};
void Baud(int b) { baud = b;};
int Baud() { return baud;};
void Timeout(int tm) { timeout = tm;}
int Timeout() { return timeout; }
void Retries(int r) { retries = r;}
int Retries() { return retries;}
void RTS(bool r){rts = r;}
bool RTS(){return rts;}
void RTSptt(bool b){rtsptt = b;}
bool RTSptt(){return rtsptt;}
void DTR(bool d){dtr = d;}
bool DTR(){return dtr;}
void DTRptt(bool b){dtrptt = b;}
bool DTRptt(){return dtrptt;}
void RTSCTS(bool b){rtscts = b;}
bool RTSCTS(){return rtscts;}
void SetPTT(bool b);
void RestoreTIO(bool b) { restore_tio = b; }
bool RestoreTIO() { return restore_tio; }
void Stopbits(int n) {stopbits = (n == 1 ? 1 : 2);}
int Stopbits() { return stopbits;}
int ReadBuffer (unsigned char *b, int nbr);
int WriteBuffer(unsigned char *str, int nbr);
void FlushBuffer();
private:
//Members
std::string device;
int fd;
int baud;
int speed;
struct termios oldtio, newtio;
int timeout;
int retries;
int status, origstatus;
bool dtr;
bool dtrptt;
bool rts;
bool rtsptt;
bool rtscts;
bool restore_tio;
int stopbits;
char bfr[2048];
//Methods
bool IOselect();
};
#else //__MINGW32__
#include <windows.h>
class Cserial {
public:
Cserial() {
rts = dtr = false;
rtsptt = dtrptt = false;
rtscts = false;
baud = CBR_9600;
stopbits = 2;
};
Cserial( std::string portname) {
device = portname;
Cserial();
// OpenPort();
};
virtual ~Cserial() {};
//Methods
BOOL OpenPort();
void ClosePort();
BOOL ConfigurePort(DWORD BaudRate,BYTE ByteSize,DWORD fParity,BYTE Parity,BYTE StopBits);
bool IsBusy() { return busyflag; };
void IsBusy(bool val) { busyflag = val; };
bool IsOpen() { return hComm == INVALID_HANDLE_VALUE ? 0 : 1; };
BOOL ReadByte(unsigned char &resp);
int ReadData (unsigned char *b, int nbr);
int ReadBuffer (unsigned char *b, int nbr) {
return ReadData (b,nbr);
}
BOOL WriteByte(unsigned char bybyte);
int WriteBuffer(unsigned char *str, int nbr);
BOOL SetCommunicationTimeouts(
DWORD ReadIntervalTimeout,
DWORD ReadTotalTimeoutMultiplier,
DWORD ReadTotalTimeoutConstant,
DWORD WriteTotalTimeoutMultiplier,
DWORD WriteTotalTimeoutConstant);
BOOL SetCommTimeout();
void Timeout(int tm) { timeout = tm; return; };
int Timeout() { return timeout; };
void FlushBuffer();
void Device (std::string dev) { device = dev;};
std::string Device() { return device;};
void Baud(int b) { baud = b;};
int Baud() { return baud;};
void Retries(int r) { retries = r;}
int Retries() { return retries;}
void RTS(bool r){rts = r;}
bool RTS(){return rts;}
void RTSptt(bool b){rtsptt = b;}
bool RTSptt(){return rtsptt;}
void DTR(bool d){dtr = d;}
bool DTR(){return dtr;}
void DTRptt(bool b){dtrptt = b;}
bool DTRptt(){return dtrptt;}
void RTSCTS(bool b){rtscts = b;}
bool RTSCTS(){return rtscts;}
void SetPTT(bool b);
void RestoreTIO(bool b) { restore_tio = b; }
bool RestoreTIO() { return restore_tio; }
void Stopbits(int n) {stopbits = (n == 1 ? 1 : 2);}
int Stopbits() { return stopbits;}
//Members
private:
std::string device;
//For use by CreateFile
HANDLE hComm;
//DCB Defined in WinBase.h
DCB dcb;
COMMTIMEOUTS CommTimeoutsSaved;
COMMTIMEOUTS CommTimeouts;
//Is the Port Ready?
BOOL bPortReady;
//Number of Bytes Written to port
DWORD nBytesWritten;
//Number of Bytes Read from port
DWORD nBytesRead;
//Number of bytes Transmitted in the cur session
DWORD nBytesTxD;
int timeout;
bool busyflag;
int baud;
int retries;
bool dtr;
bool dtrptt;
bool rts;
bool rtsptt;
bool rtscts;
bool restore_tio;
int stopbits;
};
#endif // __MINGW32__
#endif // SERIAL_H
|