вообще, конечно, порты еще настраивать нужно
А что их настраивать,одним железом записывает и читает
Добавлено через 15 минут
| C++ | 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
| //=============================================================================
// General component library for WIN32
// Copyright (C) 2000, UAB BBDSoft ( [url]http://www.bbdsoft.com/[/url] )
//
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// The author of this program may be contacted at [email]developers@bbdsoft.com[/email]
//=============================================================================
#ifndef _COMPORT_
#define _COMPORT_
//-----------------------------------------------------------------------------
class COMPort
{
public:
enum Parity
{
None = 0
, Odd
, Even
, Mark
, Space
};
enum DataBits
{
db4 = 4
, db5
, db6
, db7
, db8
};
enum StopBits
{
sb1 = 0,
sb15,
sb2
};
enum BitRate
{
br110 = 110,
br300 = 300,
br600 = 600,
br1200 = 1200,
br2400 = 2400,
br4800 = 4800,
br9600 = 9600,
br19200 = 19200,
br38400 = 38400,
br56000 = 56000,
br57600 = 57600,
br115200 = 115200,
br256000 = 256000
};
// for function getModemSignals
struct MSPack
{
unsigned char DTR : 1;
unsigned char RTS : 1;
unsigned char : 2;
unsigned char CTS : 1;
unsigned char DSR : 1;
unsigned char RI : 1;
unsigned char DCD : 1;
};
COMPort ( const char * const portName );
~COMPort ();
// I/O operations
char read ();
COMPort & write (const char inChar);
unsigned long read ( void *
, const unsigned long count
);
unsigned long write ( const void *
, const unsigned long count
);
COMPort& setBitRate ( unsigned long Param );
unsigned long bitRate () const;
COMPort& setParity ( Parity Param );
Parity parity () const;
COMPort& setDataBits ( DataBits Param );
DataBits dataBits () const;
COMPort& setStopBits ( StopBits Param );
StopBits stopBits () const;
COMPort & setHandshaking ( bool inHandshaking = true );
COMPort& setLineCharacteristics ( char * Param );
unsigned long getMaximumBitRate () const;
COMPort & setxONxOFF ( bool Param = true);
bool isxONxOFF () const;
MSPack getModemSignals () const;
COMPort& setBlockingMode ( unsigned long inReadInterval =50
, unsigned long inReadMultiplyer =1000
, unsigned long inReadConstant =5
);
protected:
private:
// disable copy constructor and assignment operator
COMPort (const COMPort &);
COMPort& operator= (const COMPort &);
void getState () const;
COMPort& setState ();
unsigned thePortHandle;
char * theDCB;
}; // End of COMPort class declaration
#endif |
|
| C++ | 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
| //=============================================================================
// General component library for WIN32
// Copyright (C) 2000, UAB BBDSoft ( [url]http://www.bbdsoft.com/[/url] )
//
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// The author of this program may be contacted at [email]developers@bbdsoft.com[/email]
//=============================================================================
#ifndef _COMPORT_
#include "ComPort.hpp"
#endif
#ifndef _WINDOWS_
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifndef _STDEXCEPT_
#include <stdexcept>
#endif
#include <iostream.h>
using namespace std;
//----------------------------------------------------------------------------
COMPort::COMPort ( const char * const portName )
: theDCB (NULL)
{
thePortHandle = (unsigned ) CreateFile ( portName
, GENERIC_READ | GENERIC_WRITE
, 0
, NULL
, OPEN_EXISTING
, FILE_FLAG_NO_BUFFERING
, NULL
);
if (thePortHandle == HFILE_ERROR)
{
throw runtime_error ("COMPort: failed to open.");
} // endif
theDCB = new char [sizeof(DCB)];
getState();
setBlockingMode();
setHandshaking();
} // end constructor
//----------------------------------------------------------------------------
COMPort::~COMPort()
{
delete [] theDCB;
// close serial port device
if (CloseHandle ((HANDLE)thePortHandle) == FALSE )
{
throw runtime_error ("COMPort: failed to close.");
} // endif
} // end destructor
//----------------------------------------------------------------------------
void COMPort::getState () const
{
if (!GetCommState ( (HANDLE) thePortHandle
, (LPDCB) theDCB
)
)
{
throw runtime_error ("COMPort: could not retrieve serial port state.");
} // endif
} // end COMPort::getState () const
//----------------------------------------------------------------------------
COMPort& COMPort::setState ()
{
if (!SetCommState ( (HANDLE) thePortHandle
, (LPDCB) theDCB
)
)
{
throw runtime_error ("COMPort: could not modify serial port state.");
} // endif
return *this;
} // end COMPort::setState ()
//-----------------------------------------------------------------------------
COMPort& COMPort::setBitRate ( unsigned long Param )
{
DCB & aDCB = *((LPDCB)theDCB);
aDCB.BaudRate = Param;
return setState();
} // end COMPort::setBitRate (..)
//-----------------------------------------------------------------------------
unsigned long COMPort::bitRate() const
{
DCB & aDCB = *((LPDCB)theDCB);
return aDCB.BaudRate;
} // end COMPort::bitRate () const
//-----------------------------------------------------------------------------
COMPort& COMPort::setLineCharacteristics( char * inConfig )
{
COMMTIMEOUTS aTimeout;
if ( !BuildCommDCBAndTimeouts ( inConfig
, (LPDCB)theDCB
, &aTimeout
)
)
{
throw runtime_error ("COMPort: could not set line characteristics.");
} // endif
if ( ! SetCommTimeouts ( (HANDLE(thePortHandle))
, &aTimeout
)
)
{
throw runtime_error ("COMPort: could not set line characteristics.");
} // endif
return setState();
}
//----------------------------------------------------------------------------
char COMPort::read ()
{
char buffer;
DWORD charsRead = 0;
do
{
if (! ReadFile ( (HANDLE(thePortHandle))
, &buffer
, sizeof(char)
, &charsRead
, NULL
)
)
{
throw runtime_error ("COMPort: read failed.");
} // endif
} while ( !charsRead );
return buffer;
} // end COMPort::read()
//----------------------------------------------------------------------------
unsigned long COMPort::read ( void *inBuffer
, const unsigned long inCharsReq
)
{
DWORD charsRead = 0;
if ( !ReadFile ( (HANDLE(thePortHandle))
, inBuffer
, inCharsReq
, &charsRead
, NULL
)
)
{
throw runtime_error ("COMPort: read failed.");
} // endif
return charsRead;
} // end COMPort::read (..)
//----------------------------------------------------------------------------
COMPort & COMPort::write ( const char inChar )
{
char buffer = inChar;
DWORD charsWritten = 0;
if ( !WriteFile ( (HANDLE(thePortHandle))
, &buffer
, sizeof(char)
, &charsWritten
, NULL
)
)
{
throw runtime_error ("COMPort: write failed.");
} // endif
return *this;
} // end COMPort::write (..)
//----------------------------------------------------------------------------
unsigned long COMPort::write ( const void *inBuffer
, const unsigned long inBufSize
)
{
DWORD charsWritten = 0;
if ( !WriteFile ( (HANDLE(thePortHandle))
, inBuffer
, inBufSize
, &charsWritten
, NULL
)
)
{
throw runtime_error ("COMPort: write failed.");
} // endif
return charsWritten;
} // end COMPort::write()
//-----------------------------------------------------------------------------
COMPort& COMPort::setxONxOFF ( bool Param )
{
DCB & aDCB = *((LPDCB)theDCB);
aDCB.fOutX = Param ? 1 : 0;
aDCB.fInX = Param ? 1 : 0;
return setState();
} // end COMPort::setxONxOFF (..)
//-----------------------------------------------------------------------------
bool COMPort::isxONxOFF () const
{
DCB & aDCB = *((LPDCB)theDCB);
return (aDCB.fOutX && aDCB.fInX);
} // end COMPort::isxONxOFF () const
//----------------------------------------------------------------------------
COMPort& COMPort::setBlockingMode ( unsigned long inReadInterval
, unsigned long inReadMultiplyer
, unsigned long inReadConstant
)
{
COMMTIMEOUTS commTimeout;
if ( !GetCommTimeouts ( (HANDLE(thePortHandle))
, &commTimeout
)
)
{
throw runtime_error ("COMPort: failed to retrieve timeouts.");
} // endif
commTimeout.ReadIntervalTimeout = inReadInterval;
if ( inReadInterval==MAXDWORD )
{
commTimeout.ReadTotalTimeoutMultiplier = 0;
commTimeout.ReadTotalTimeoutConstant = 0;
}
else
{
commTimeout.ReadTotalTimeoutMultiplier = inReadMultiplyer;
commTimeout.ReadTotalTimeoutConstant = inReadConstant;
} // endifelse
if ( !SetCommTimeouts ( (HANDLE(thePortHandle))
, &commTimeout
)
)
{
throw runtime_error ("COMPort: failed to modify timeouts.");
} // endif
return *this;
} // end COMPort::setBlockingMode (..)
//-----------------------------------------------------------------------------
COMPort & COMPort::setHandshaking ( bool inHandshaking )
{
DCB & aDCB = *((LPDCB)theDCB);
if (inHandshaking)
{
aDCB.fOutxCtsFlow = TRUE;
aDCB.fOutxDsrFlow = FALSE;
aDCB.fRtsControl = RTS_CONTROL_HANDSHAKE;
}
else
{
aDCB.fOutxCtsFlow = FALSE;
aDCB.fOutxDsrFlow = FALSE;
aDCB.fRtsControl = RTS_CONTROL_ENABLE;
} // endifelse
return setState();
} // end COMPort::setHandshaking (..)
//-----------------------------------------------------------------------------
unsigned long COMPort::getMaximumBitRate() const
{
COMMPROP aProp;
if ( !GetCommProperties ( (HANDLE)thePortHandle
, &aProp )
)
{
throw runtime_error ("COMPort: failed to retrieve port properties.");
} // endif
return aProp.dwMaxBaud;
} // end COMPort::getMaximumBitRate () const
//-----------------------------------------------------------------------------
COMPort::MSPack COMPort::getModemSignals() const
{
MSPack aPack;
// 1 bit - DTR, 2 - bit RTS (output signals)
// 4 bit - CTS, 5 bit - DSR, 6 bit - RI, 7 bit - DCD (input signals)
if ( !GetCommModemStatus ( (HANDLE)thePortHandle
, (LPDWORD)&aPack )
)
{
throw runtime_error ("COMPort: failed to retrieve modem signals.");
} // endif
return aPack;
} // end COMPort::getModemSignals () const
//-----------------------------------------------------------------------------
COMPort& COMPort::setParity ( Parity Param )
{
DCB & aDCB = *((LPDCB)theDCB);
aDCB.Parity = Param;
return setState();
} // end COMPort::setParity (..)
//-----------------------------------------------------------------------------
COMPort& COMPort::setDataBits ( DataBits Param )
{
DCB & aDCB = *((LPDCB)theDCB);
aDCB.ByteSize = Param;
return setState();
} // end COMPort::setDataBits (..)
//-----------------------------------------------------------------------------
COMPort& COMPort::setStopBits ( StopBits Param )
{
DCB & aDCB = *((LPDCB)theDCB);
aDCB.StopBits = Param;
return setState();
} // end COMPort::setStopBits (..)
//-----------------------------------------------------------------------------
COMPort::Parity COMPort::parity () const
{
DCB & aDCB = *((LPDCB)theDCB);
return (COMPort::Parity)aDCB.Parity;
} // end COMPort::parity () const
//-----------------------------------------------------------------------------
COMPort::DataBits COMPort::dataBits () const
{
DCB & aDCB = *((LPDCB)theDCB);
return (COMPort::DataBits)aDCB.ByteSize;
} // end COMPort::dataBits () const
//-----------------------------------------------------------------------------
COMPort::StopBits COMPort::stopBits () const
{
DCB & aDCB = *((LPDCB)theDCB);
return (COMPort::StopBits)aDCB.StopBits;
} // end COMPort::stopBits () cosnt |
|
Вот готовый класс,проверял с микроконтролером работает
2
|