此代码出现的问题是:1 字符串发送不出去. 2 接收不到数据. 已经调了1天了,快疯了.期盼各位高手能给予帮助!万分感谢!![不要求用控件]
代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Registry, ExtCtrls;const
WM_COMMNOTIFY=WM_USER+1;type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
SendEdit: TEdit;
Memo1: TMemo;
Button1: TButton;
CkHComboBox: TComboBox;
Button2: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
Procedure Comminitialize(ComName: string);
public end; TCommThread=Class(TThread)
protected
procedure Execute;override;
end;var
Form1: TForm1;
hcom,Post_Event: Thandle;
lpol: Poverlapped;
CommThread: TCommThread;implementation{$R *.DFM}procedure TCommThread.Execute; //线程监听过程
var
dwEvtMask: Dword;
Begin
while True do
Begin
dwEvtMask:=EV_RXCHAR;
if WaitCommEvent(hcom,dwEvtMask,lpol) Then
form1.Timer1.Enabled:=True;
end;
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
Clear: Boolean;
Coms: TComStat;
cbNum,ReadNumber,lpErrors: Cardinal;
Read_Buffer: array[0..2047]of char;
Ls: string;
Begin
Clear:=ClearCommError(hcom,lpErrors,@Coms);
if Clear Then
Begin
//FillChar(Read_Buffer,2047,0);
cbNum:=Coms.cbInQue;
ReadFile(hCom,Read_Buffer,cbNum,ReadNumber,lpol);
if Read_Buffer[cbNum-1]=#6 then
begin
Ls:=Copy(Read_Buffer,1,cbNum);
Memo1.Text:=Memo1.Text+Ls;
Timer1.Enabled:=False;
end;
end;
end;procedure Tform1.Comminitialize(ComName: string);//串口初始化
var
lpDCB: TDCB;
Begin
hcom:=CreateFile(Pchar(ComName),GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,0);
if hcom=INVALID_HANDLE_VALUE then
begin
MessageBox(Handle,'串口设置失败!','错误',MB_OK+MB_ICONERROR);
Exit;
end
else
begin
SetupComm(hcom,2048,2048); //设置缓冲区
GetCommState(hcom,lpDCB);
lpDCB.baudrate:=9600;
lpDCB.StopBits:=1;
lpDCB.ByteSize:=8;
lpDCB.Parity:=NOPARITY; //无校验
SetCommState(hcom,lpDCB);
setCommMask(hcom,EV_RXCHAR); //指定串口事件为接收到字符;
MessageBox(Handle,'串口设置成功!','提示',MB_OK+MB_ICONWARNING);
end;
end;function GetSysCom(var ComList: TStringList): Boolean;
var
i: integer;
Reg: TRegistry;
RegStr: string;
tmpList: TStringList;
begin
try
RegStr:='\HARDWARE\DEVICEMAP\SERIALCOMM';
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.OpenKey(RegStr,False) then
begin
try
tmpList:=TStringList.Create;
Reg.GetValueNames(tmpList);
for i := 0 to tmpList.Count - 1 do
ComList.Add(Reg.ReadString(tmpList.Strings[i]));
finally
FreeAndNil(tmpList);
end;
end;
finally
FreeAndNil(Reg);
end;
if ComList.Count>0 then
result:=True
else
result:=False;
end;procedure TForm1.FormCreate(Sender: TObject);
var
ComString: TStringList;
i: integer;
begin
try
ComString:=TStringList.Create;
if GetSysCom(ComString) then
begin
for i:=0 to ComString.Count-1 do
CkHComboBox.Items.Add(ComString.Strings[i]);
CkHComboBox.ItemIndex:=0;
end;
finally
FreeAndNil(ComString);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
SendText: string;
Nbw: LongWord;
begin
SendText:=SendEdit.Text;
WriteFile(hCom,Pointer(SendText)^,Length(SendText),Nbw,lpol);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
CommInitialize(CkHComboBox.Text);
CommThread:=TCommThread.Create(False); //创建串口监视线程;
end;end.
代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Registry, ExtCtrls;const
WM_COMMNOTIFY=WM_USER+1;type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
SendEdit: TEdit;
Memo1: TMemo;
Button1: TButton;
CkHComboBox: TComboBox;
Button2: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
Procedure Comminitialize(ComName: string);
public end; TCommThread=Class(TThread)
protected
procedure Execute;override;
end;var
Form1: TForm1;
hcom,Post_Event: Thandle;
lpol: Poverlapped;
CommThread: TCommThread;implementation{$R *.DFM}procedure TCommThread.Execute; //线程监听过程
var
dwEvtMask: Dword;
Begin
while True do
Begin
dwEvtMask:=EV_RXCHAR;
if WaitCommEvent(hcom,dwEvtMask,lpol) Then
form1.Timer1.Enabled:=True;
end;
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
Clear: Boolean;
Coms: TComStat;
cbNum,ReadNumber,lpErrors: Cardinal;
Read_Buffer: array[0..2047]of char;
Ls: string;
Begin
Clear:=ClearCommError(hcom,lpErrors,@Coms);
if Clear Then
Begin
//FillChar(Read_Buffer,2047,0);
cbNum:=Coms.cbInQue;
ReadFile(hCom,Read_Buffer,cbNum,ReadNumber,lpol);
if Read_Buffer[cbNum-1]=#6 then
begin
Ls:=Copy(Read_Buffer,1,cbNum);
Memo1.Text:=Memo1.Text+Ls;
Timer1.Enabled:=False;
end;
end;
end;procedure Tform1.Comminitialize(ComName: string);//串口初始化
var
lpDCB: TDCB;
Begin
hcom:=CreateFile(Pchar(ComName),GENERIC_READ or GENERIC_WRITE,0,nil,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,0);
if hcom=INVALID_HANDLE_VALUE then
begin
MessageBox(Handle,'串口设置失败!','错误',MB_OK+MB_ICONERROR);
Exit;
end
else
begin
SetupComm(hcom,2048,2048); //设置缓冲区
GetCommState(hcom,lpDCB);
lpDCB.baudrate:=9600;
lpDCB.StopBits:=1;
lpDCB.ByteSize:=8;
lpDCB.Parity:=NOPARITY; //无校验
SetCommState(hcom,lpDCB);
setCommMask(hcom,EV_RXCHAR); //指定串口事件为接收到字符;
MessageBox(Handle,'串口设置成功!','提示',MB_OK+MB_ICONWARNING);
end;
end;function GetSysCom(var ComList: TStringList): Boolean;
var
i: integer;
Reg: TRegistry;
RegStr: string;
tmpList: TStringList;
begin
try
RegStr:='\HARDWARE\DEVICEMAP\SERIALCOMM';
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.OpenKey(RegStr,False) then
begin
try
tmpList:=TStringList.Create;
Reg.GetValueNames(tmpList);
for i := 0 to tmpList.Count - 1 do
ComList.Add(Reg.ReadString(tmpList.Strings[i]));
finally
FreeAndNil(tmpList);
end;
end;
finally
FreeAndNil(Reg);
end;
if ComList.Count>0 then
result:=True
else
result:=False;
end;procedure TForm1.FormCreate(Sender: TObject);
var
ComString: TStringList;
i: integer;
begin
try
ComString:=TStringList.Create;
if GetSysCom(ComString) then
begin
for i:=0 to ComString.Count-1 do
CkHComboBox.Items.Add(ComString.Strings[i]);
CkHComboBox.ItemIndex:=0;
end;
finally
FreeAndNil(ComString);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
SendText: string;
Nbw: LongWord;
begin
SendText:=SendEdit.Text;
WriteFile(hCom,Pointer(SendText)^,Length(SendText),Nbw,lpol);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
CommInitialize(CkHComboBox.Text);
CommThread:=TCommThread.Create(False); //创建串口监视线程;
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货