-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOMM.H
More file actions
164 lines (140 loc) · 5.64 KB
/
COMM.H
File metadata and controls
164 lines (140 loc) · 5.64 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
///// 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.
#ifndef COMM_H
#define COMM_H
#include <windows.h>
#include <string>
#include <list>
#include <tchar.h>
namespace std
{
#ifdef _UNICODE
typedef wstring tstring;
#else
typedef string tstring;
#endif
};
bool isAblePort(const std::tstring & port);
// 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:
enum 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 ,
GET_COMM_MODEM_STATUS_ERROR
};
ECommError( ErrorType error);
ErrorType Error;
DWORD Errno; // Errno == return value from GetLastError. Can be used with FormatMessage
};
class TCommPort
{
public:
TCommPort();
~TCommPort();
// bool isAblePort(const std::string & port);
int OpenCommPort(void);
int CloseCommPort(void);
int SetCommPort(const std::tstring & port);
std::tstring GetCommPort(void);
int SetBaudRate(DWORD newBaud);//CBR_110 (300,600,1200,2400,4800,9600,14400,19200,
DWORD GetBaudRate(void); // 38400,56000,57600,115200,128000,256000)
int SetParity(BYTE newParity);//NOPARITY ODDPARITY EVENPARITY MARKPARITY SPACEPARITY
BYTE GetParity(void);
int SetByteSize(BYTE newByteSize); // 4-8
BYTE GetByteSize(void);
int SetStopBits(BYTE newStopBits);//ONESTOPBIT ONE5STOPBITS TWOSTOPBITS
BYTE GetStopBits(void);
DWORD GetCommModemStatus(); // MS_CTS_ON The CTS is on.
// MS_DSR_ON The DSR is on.
// MS_RING_ON The ring indicator signal is on.
// MS_RLSD_ON The RLSD (receive-line-signal-detect) signal is on.
void SetDTR(){ if (VerifyOpen()) return; EscapeCommFunction(m_hCom, SETDTR); }
void ClrDTR(){ if (VerifyOpen()) return; EscapeCommFunction(m_hCom, CLRDTR); }
void SetRTS(){ if (VerifyOpen()) return; EscapeCommFunction(m_hCom, SETRTS); }
void ClrRTS(){ if (VerifyOpen()) return; EscapeCommFunction(m_hCom, CLRRTS); }
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);
int WriteBuffer(BYTE *buffer, unsigned int ByteCount);
int WriteBufferSlowly(BYTE *buffer, unsigned int ByteCount);
int ReadString(char *string, unsigned int MaxBytes);
int ReadBytes(BYTE *bytes, unsigned int byteCount);
void DiscardBytes(unsigned int MaxBytes);
void Flush_RX(void);
void PurgeCommPort(void);
void FlushCommPort(void);
void PutByte(BYTE value);
int GetByte(); // -1 ¯à¨ ®âáãá⢨¨
int GetByte(int Time); // ¦¤ âì ¡ ©â ¥ ¬¥¥¥ Time ms
int BytesAvailable(void);
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;
}
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.
int VerifyOpen(void)
{
if (!m_CommOpen)
//throw ECommError(ECommError::PORT_NOT_OPEN) ;
return -1;
return 0;
}
int VerifyClosed(void)
{
if(m_CommOpen)
//throw ECommError(ECommError::PORT_ALREADY_OPEN) ;
return -1;
return 0;
}
// this stuff is private because we want to hide these details from clients
bool m_CommOpen;
COMMTIMEOUTS m_TimeOuts;
std::tstring m_CommPort;
DCB m_dcb; // a DCB is a windows structure used for configuring the port
DCB old_m_dcb;
HANDLE m_hCom; // handle to the comm port.
};
int Get_port_list(std::list<_TCHAR*> &port_list);
void Free_port_list(std::list<_TCHAR*> &port_list);
#endif