-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommPort.h
136 lines (116 loc) · 4.56 KB
/
CommPort.h
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
//---------------------------------------------------------------------------
#ifndef CommPortH
#define CommPortH
///// comm.h
///// purpose : prototypes for for TCommPort, serial communictaions API encapsulation
///// copyright: Harold Howe, bcbdev.com 1996-1999.
///// notice : This file provides an object that encapsulates the win32 serial port routines.
///// This file may be distributed and used freely for program development,
///// but it may not be resold as part of a software development library.
///// In other words, don't take the source and attempt to sell it to other developers.
#include <windows.h>
#include <string>
#include <System.SysUtils.hpp>
// When the comm port class encounters an error, it throws an ECommError exception.
// The Error member of the exception object describes what went wrong. Some of the
// items should never happen. Others are fairly common place. You should pay special
// attention to the OPEN_ERROR type. This is the error that occurs when opening the port
// fails because another app already has the port open.
class ECommError : public Exception
{
public:
enum class ErrorType {
BAD_SERIAL_PORT ,
BAD_BAUD_RATE ,
BAD_PORT_NUMBER ,
BAD_STOP_BITS ,
BAD_PARITY ,
BAD_BYTESIZE ,
PORT_ALREADY_OPEN ,
PORT_NOT_OPEN ,
OPEN_ERROR ,
WRITE_ERROR ,
READ_ERROR ,
CLOSE_ERROR ,
PURGECOMM ,
FLUSHFILEBUFFERS ,
GETCOMMSTATE ,
SETCOMMSTATE ,
SETUPCOMM ,
SETCOMMTIMEOUTS ,
CLEARCOMMERROR
};
ECommError( ErrorType error );
ErrorType Error;
DWORD Errno; // Errno == return value from GetLastError. Can be used with FormatMessage
private:
static String FormatErrorMessage( ErrorType Err );
static const String ErrorString[19];
};
class TCommPort
{
public:
TCommPort();
~TCommPort();
void OpenCommPort();
void CloseCommPort();
void SetCommPort(const std::wstring & port);
std::wstring GetCommPort();
void SetBaudRate(unsigned int newBaud);
unsigned int GetBaudRate();
void SetParity(BYTE newParity); // see source for possible values
BYTE GetParity();
void SetByteSize(BYTE newByteSize);
BYTE GetByteSize();
void SetStopBits(BYTE newStopBits);
BYTE GetStopBits();
void SetCommDCBProperties(DCB &properties); // avoid using DCB interface
void GetCommDCBProperties(DCB &properties); // Use SetBaudRate et al instead
void GetCommProperties(COMMPROP &properties);
void WriteString(const char *outString);
void WriteBuffer(BYTE *buffer, unsigned int ByteCount);
void WriteBufferSlowly(BYTE *buffer, unsigned int ByteCount);
unsigned int ReadString(char *string, unsigned int MaxBytes);
unsigned int ReadBytes(BYTE *bytes, unsigned int byteCount);
void DiscardBytes(unsigned int MaxBytes);
void PurgeCommPort();
void FlushCommPort();
void PutByte(BYTE value);
BYTE GetByte();
unsigned int BytesAvailable();
bool GetConnected()
{
return m_CommOpen;
}
HANDLE GetHandle() // allow access to the handle in case the user needs to
{ // do something hardcore. Avoid this if possible
return m_hCom;
}
void SetRTS( bool State );
private:
// Note: the destructor of the commport class automatically closes the
// port. This makes copy construction and assignment impossible.
// That is why I privatize them, and don't define them. In order
// to make copy construction and assignment feasible, we would need
// to employ a reference counting scheme.
TCommPort(const TCommPort &); // privatize copy construction
TCommPort & operator=(const TCommPort&); // and assignment.
void VerifyOpen()
{
if(!m_CommOpen)
throw ECommError(ECommError::ErrorType::PORT_NOT_OPEN) ;
}
void VerifyClosed()
{
if(m_CommOpen)
throw ECommError(ECommError::ErrorType::PORT_ALREADY_OPEN) ;
}
// this stuff is private because we want to hide these details from clients
bool m_CommOpen;
COMMTIMEOUTS m_TimeOuts;
std::wstring m_CommPort;
DCB m_dcb; // a DCB is a windows structure used for configuring the port
HANDLE m_hCom; // handle to the comm port.
};
//---------------------------------------------------------------------------
#endif