-
Notifications
You must be signed in to change notification settings - Fork 5
/
USerial.pas
193 lines (160 loc) · 5.21 KB
/
USerial.pas
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
unit USerial;
interface
uses Windows;
type
TSerialFlag = (RTS_ENABLE, DTR_ENABLE, TWO_STOPBITS, EVEN_PARITY, ODD_PARITY);
TSerialFlags = set of TSerialFlag;
TSerial = class(TObject)
public
function Read(var onebyte: Byte): Boolean; overload;
function Read(buffer: pointer; uiSize: Cardinal): Cardinal; overload;
procedure Write(var buffer: array of byte; uiSize: Cardinal); overload;
procedure Write(buffer: PByte; uiSize: Cardinal); overload;
procedure Write(onebyte: byte); overload;
procedure Write(const str: String); overload;
constructor Create(uPort: Byte; uiBaud: Cardinal; flags: TSerialFlags);
destructor Destroy; override;
private
hSerial: Cardinal;
end;
implementation
uses SysUtils, UUtils;
destructor TSerial.Destroy;
begin
if (hSerial <> INVALID_HANDLE_VALUE) then
CloseHandle(hSerial);
hSerial := INVALID_HANDLE_VALUE;
end;
constructor TSerial.Create(uPort: Byte; uiBaud: Cardinal; flags: TSerialFlags);
var
ourDCB: DCB;
ourTimeouts: COMMTIMEOUTS;
begin
inherited Create;
hSerial := 1;
hSerial := CreateFile(PAnsiChar('\\.\COM' + IntToStr(uPort)),
GENERIC_READ or GENERIC_WRITE,
0 {exclusive access}, nil {no security attribs},
OPEN_EXISTING, 0 {not overlapped I/O}, 0 {no template});
if(hSerial = INVALID_HANDLE_VALUE) then
raise Exception.create('Failed to open COM'+IntToStr(uPort)+': '
+ errMsg(GetLastError));
if (not GetCommState(hSerial, ourDCB)) then
raise Exception.create('Failed to get CommState for COM'+IntToStr(uPort)+': '
+ errMsg(GetLastError));
// Set required flags - delphi's copy of DCB doesn't contain bit flags...
ourDCB.Flags := ourDCB.Flags or
((1 shl 0) // fBinary
//or (1 shl 1) // fParity
//or (1 shl 2) // fOutxCtxFlow
//or (1 shl 3) // fOutxDsrFlow
// or (1 shl 4) // fDtrControl1
// or (1 shl 5) // fDtrControl2
//or (1 shl 6) // fDtrSensitivity
//or (1 shl 7) // fTXContinueOnXOff
//or (1 shl 8) // fOutX
//or (1 shl 9) // fInX
//or (1 shl 10) // fErrorChar
//or (1 shl 11) // fNull
//or (1 shl 12) // fRtsControl1
//or (1 shl 13) // fRtsControl2
//or (1 shl 14) // fAbortOnError
//or (1 shl 15)); // fDummy2
);
// reset unneeded flags
ourDCB.Flags := ourDCB.Flags and not
(//(1 shl 0) // fBinary
(1 shl 1) // fParity
or (1 shl 2) // fOutxCtxFlow
or (1 shl 3) // fOutxDsrFlow
or (1 shl 4) // fDtrControl1
or (1 shl 5) // fDtrControl2
or (1 shl 6) // fDtrSensitivity
//or (1 shl 7) // fTXContinueOnXOff
or (1 shl 8) // fOutX
or (1 shl 9) // fInX
or (1 shl 10) // fErrorChar
or (1 shl 11) // fNull
or (1 shl 12) // fRtsControl1
or (1 shl 13) // fRtsControl2
or (1 shl 14) // fAbortOnError
//or (1 shl 15) // fDummy2
);
if (DTR_ENABLE in flags) then
ourDCB.Flags := ourDCB.Flags or (1 shl 4);
if (RTS_ENABLE in flags) then
ourDCB.Flags := ourDCB.Flags or (1 shl 12);
ourDCB.BaudRate := uiBaud;
ourDCB.ByteSize := 8;
if (ODD_PARITY in flags) then
ourDCB.Parity := ODDPARITY
else if (EVEN_PARITY in flags) then
ourDCB.Parity := EVENPARITY
else
ourDCB.Parity := NOPARITY;
if (TWO_STOPBITS in flags) then
ourDCB.StopBits := TWOSTOPBITS
else
ourDCB.StopBits := ONESTOPBIT;
if (not SetCommState(hSerial, ourDCB)) then
raise Exception.create('Failed to set CommState for COM'+IntToStr(uPort)+': '
+ errMsg(GetLastError));
if(not GetCommTimeouts(hSerial, ourTimeouts)) then
raise Exception.create('Failed to get CommTimeouts for COM'+IntToStr(uPort)+': '
+ errMsg(GetLastError));
ourTimeouts.ReadIntervalTimeout:=2;
ourTimeouts.ReadTotalTimeoutMultiplier:=0;
ourTimeouts.ReadTotalTimeoutConstant:=50;
ourTimeouts.WriteTotalTimeoutMultiplier:=0;
ourTimeouts.WriteTotalTimeoutConstant:=200;
if (not SetCommTimeouts(hSerial, ourTimeouts)) then
raise Exception.create('Failed to set CommTimeouts for COM'+IntToStr(uPort)+': '
+ errMsg(GetLastError));
// Give serial device enough time to boot.
// [Some devices are powered by the RTS/DTR lines and would have just been
// powered up and are now booting.]
Sleep(500);
end;
function TSerial.Read(buffer: pointer; uiSize: Cardinal): Cardinal;
var
bytesRead: Cardinal;
p: PByte;
begin
p := buffer;
bytesRead := 0;
if (not ReadFile(hSerial, p^, uiSize, bytesRead, nil)) then
begin
raise Exception.Create('read failed: '+errMsg(GetLastError()));
end;
Result := bytesRead;
end;
function TSerial.Read(var onebyte: Byte): Boolean;
begin
if (Read(@onebyte, 1) = 1) then
Result := true
else
Result := false;
end;
procedure TSerial.Write(buffer: PByte; uiSize: Cardinal);
var
bytesWritten: Cardinal;
begin
if (not WriteFile(hSerial, buffer^, uiSize, bytesWritten, nil)) then
begin
raise Exception.Create('write failed: '+errMsg(GetLastError()));
end;
end;
procedure TSerial.Write(var buffer: array of byte; uiSize: Cardinal);
begin
Write(@buffer[0], uiSize);
end;
procedure TSerial.Write(onebyte: byte);
begin
Write(@onebyte, 1);
end;
procedure TSerial.Write(const str: String);
begin
if (Length(str)>0) then
Write(@str[1], Length(str));
end;
end.