-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDataPort.pas
104 lines (86 loc) · 3.39 KB
/
DataPort.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
{
DataPort - thread-safe abstract port for data exchange
Sergey Bodrov ([email protected]) 2012-2016
TDataPort is abstract component for reading and writing data to some port.
It don't do anything and needs to be used as property or parent class for new components.
Properties:
Active - is port ready for data exchange
Methods:
Open() - Open data port. If InitStr specified, set parameters from InitStr
Push() - Send data to port
Pull() - Get data from port. Data readed from incoming buffer, and removed after that.
You can specify number of bytes for read. If incoming buffer have less bytes,
than specified, then will be returned while buffer.
By default, return whole buffer and clear it after.
Peek() - Read data from incoming buffer, but don't remove. You can specify number
of bytes for read. If incoming buffer have less bytes, than specified,
then will be returned while buffer. By default, return whole buffer.
PeekSize() - Returns number of bytes in incoming buffer of port.
Events:
OnDataAppear - Triggered in data appear in incoming buffer of dataport.
OnOpen - Triggered after sucсessful opening connection.
OnClose - Triggered when connection gracefully closed.
OnError - Triggered on error, contain error description.
}
unit DataPort;
interface
uses Classes;
type
TMsgEvent = procedure(Sender: TObject; const AMsg: AnsiString) of object;
{ TDataPort }
TDataPort = class(TComponent)
protected
FOnDataAppear: TNotifyEvent;
FOnOpen: TNotifyEvent;
FOnClose: TNotifyEvent;
FOnError: TMsgEvent;
FActive: Boolean;
procedure SetActive(Val: Boolean); virtual;
public
property Active: Boolean read FActive write SetActive;
{ Occurs when new data appears in incoming buffer }
property OnDataAppear: TNotifyEvent read FOnDataAppear write FOnDataAppear;
{ Occurs immediately after dataport has been sucsessfully opened }
property OnOpen: TNotifyEvent read FOnOpen write FOnOpen;
{ Occurs after dataport has been closed }
property OnClose: TNotifyEvent read FOnClose write FOnClose;
{ Occurs when dataport operations fails, contain error description }
property OnError: TMsgEvent read FOnError write FOnError;
{ Open dataport with specified initialization string
If AInitStr not specified, used default or designed settings }
procedure Open(const AInitStr: string = ''); virtual;
{ Close dataport }
procedure Close(); virtual;
{ Write data string to port }
function Push(const AData: AnsiString): Boolean; virtual; abstract;
{ Read and remove <size> bytes from incoming buffer. By default, read all data. }
function Pull(ASize: Integer = MaxInt): AnsiString; virtual; abstract;
{ Read, but not remove <size> bytes from incoming buffer. }
function Peek(ASize: Integer = MaxInt): AnsiString; virtual; abstract;
{ Get number of bytes waiting in incoming buffer }
function PeekSize(): Cardinal; virtual; abstract;
end;
implementation
{ TDataPort }
procedure TDataPort.SetActive(Val: Boolean);
begin
if FActive = Val then
Exit;
if Val then
Open()
else
Close();
end;
procedure TDataPort.Open(const AInitStr: string);
begin
FActive := True;
if Assigned(OnOpen) then
OnOpen(self);
end;
procedure TDataPort.Close();
begin
FActive := False;
if Assigned(OnClose) then
OnClose(self);
end;
end.