Delphi使用兩種方法獲取Windows系統的端口,還可測試發送消息,點擊獲取端口信息後,可依次得到如下信息:DCB結構大小、波特率大小、XON的臨界值、XOFF的臨界值、字符位數、奇偶檢驗位、停止位、XON字符等,也可設置端口信息,設置SPCOMM端口信息:
001
unit
Unit1;
002
interface
003
uses
004
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
005
Dialogs, SPComm, StdCtrls, ComCtrls;
006
type
007
TForm1 =
class
(TForm)
008
Edit1: TEdit;
009
Button2: TButton;
010
Memo1: TMemo;
011
Button3: TButton;
012
Button1: TButton;
013
Label1: TLabel;
014
Button4: TButton;
015
Comm1: TComm;
016
Edit2: TEdit;
017
Button5: TButton;
018
StatusBar1: TStatusBar;
019
procedure
Button2Click(Sender: TObject);
020
procedure
Button3Click(Sender: TObject);
021
procedure
Button1Click(Sender: TObject);
022
procedure
Button4Click(Sender: TObject);
023
procedure
Button5Click(Sender: TObject);
024
procedure
FormDestroy(Sender: TObject);
025
private
026
{ Private declarations }
027
public
028
{ Public declarations }
029
end
;
030
031
var
032
Form1: TForm1;
033
implementation
034
{$R *.dfm}
035
procedure
TForm1
.
Button2Click(Sender: TObject);
036
var
037
CommPort :
string
;
038
hCommFile : THandle;
039
hdcb:DCB;
040
begin
041
commport:=Edit1
.
Text;
042
hCommFile := CreateFile(
PChar
(CommPort),GENERIC_READ
or
GENERIC_WRITE,
043
0
,
nil
,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
0
);
044
//打開端口失敗的處理
045
if
hCommFile =INVALID_HANDLE_VALUE
then
046
Statusbar1
.
SimpleText:=
'不能打開端口'
;
047
GetCommState(hcommFile,hdcb);
//獲取DCB結構信息
048
Memo1
.
Clear;
049
Memo1
.
Lines
.
Add(
'DCB結構大小是:'
+IntToStr(hdcb
.
DCBlength)+#
13
#
10
+
050
'波特率大小是: '
+IntToStr(hdcb
.
BaudRate)+#
13
#
10
+
051
'XON的臨界值是:'
+IntToStr(hdcb
.
XonLim)+#
13
#
10
+
052
'XOFF的臨界值是:'
+IntToStr(hdcb
.
XoffLim)+#
13
#
10
+
053
'字符位數是:'
+IntToStr(hdcb
.
ByteSize)+#
13
#
10
+
054
'奇偶檢驗位:'
+IntToStr(hdcb
.
Parity)+#
13
#
10
+
055
'停止位:'
+IntToStr(hdcb
.
StopBits)+#
13
#
10
+
056
'XON字符:'
+hdcb
.
XonChar+#
13
#
10
+
057
'XOFF字符:'
+hdcb
.
XoffChar+#
13
#
10
+
058
'奇偶錯誤代替字符:'
+hdcb
.
ErrorChar+#
13
#
10
+
059
'結束字符:'
+hdcb
.
EofChar+#
13
#
10
+
060
'時間字符:'
+hdcb
.
EvtChar+#
13
#
10
);
061
CloseHandle(hcommfile);
062
//關閉端口句柄,否則重新執行程序時會調用空句柄,不能得到正確的參數值
063
end
;
064
procedure
TForm1
.
Button3Click(Sender: TObject);
065
var
066
CommPort :
string
;
067
hCommFile : THandle;
068
Buffer : PCommConfig;
069
size : DWord;
070
begin
071
CommPort :=
'COM2'
;
072
hCommFile := CreateFile(
PChar
(CommPort),
073
GENERIC_WRITE,
074
0
,
075
nil
,
076
OPEN_EXISTING,
077
FILE_ATTRIBUTE_NORMAL,
078
0
);
079
if
hCommFile=INVALID_HANDLE_VALUE
then
080
begin
081
Statusbar1
.
SimpleText:=
'不能打開 '
+ CommPort;
082
exit;
083
end
;
084
GetMem(Buffer, sizeof(TCommConfig));
085
GetCommConfig(hCommFile, Buffer^, size);
086
FreeMem(Buffer, sizeof(TCommConfig));
087
GetMem(Buffer, size);
088
GetCommConfig(hCommFile, Buffer^, size);
089
if
CommConfigDialog(
PChar
(CommPort),Handle,Buffer^) =
true
then
090
SetCommConfig(hCommFile, Buffer^, size);
091
FreeMem(Buffer, size);
092
CloseHandle(hCommFile);
093
end
;
094
procedure
TForm1
.
Button1Click(Sender: TObject);
095
var
096
setComm :
string
;
097
hsetFile : THandle;
098
hsetdcb:DCB;
099
begin
100
//設置斷口信息
101
setcomm:=Edit1
.
Text;
102
hsetFile := CreateFile(
PChar
(setComm),GENERIC_READ
or
GENERIC_WRITE,
103
0
,
nil
,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
0
);
104
//打開端口失敗的處理
105
if
hsetFile =INVALID_HANDLE_VALUE
then
106
Statusbar1
.
SimpleText:=
'不能打開端口'
;
107
GetCommState(hsetFile,hsetdcb);
108
hsetdcb
.
BaudRate:=
2400
;
109
hsetdcb
.
StopBits:=
2
;
110
hsetdcb
.
ByteSize:=
4
;
111
SetCommState(hsetFile,hsetdcb);
//獲取DCB結構信息
112
closeHandle(hsetfile);
//關閉端口
113
end
;
114
procedure
TForm1
.
Button4Click(Sender: TObject);
115
begin
116
comm1
.
StopComm;
//先關閉端口
117
{設置一些必要的參數}
118
Comm1
.
CommName:=Edit1
.
Text;
119
Comm1
.
BaudRate:=
9600
;
120
Comm1
.
ByteSize:=_8;
121
Comm1
.
StopBits:=_1;
122
Comm1
.
Parity:=None;
123
Try
124
comm1
.
StartComm;
//打開端口
125
except
126
raise
Exception
.
create(
'打開端口'
+comm1
.
CommName+
'出錯了'
);
127
end
;
128
Statusbar1
.
SimpleText:=Comm1
.
CommName+
'打開了'
;
129
Button5
.
Enabled:=
True
;
130
end
;
131
procedure
TForm1
.
Button5Click(Sender: TObject);
132
var
str:
string
;
133
count:
Word
;
134
begin
135
str:=
pchar
(Edit2
.
Text);
136
count:=length(str);
137
If
comm1
.
WriteCommData(
pchar
(str),count)
Then
138
Statusbar1
.
SimpleText:=
'已經發送'
+IntToStr(count)+
'字符'
139
Else
140
Statusbar1
.
SimpleText:=
'發送不成功'
;
141
end
;
142
procedure
TForm1
.
FormDestroy(Sender: TObject);
143
begin
144
Comm1
.
StopComm;
//關閉端口
145
end
;
146
end
.
最終運行截圖如下圖: