A
avant
Unregistered / Unconfirmed
GUEST, unregistred user!
测试程序如下:
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Label1: TLabel;
Button1: TButton;
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
hCom:Thandle;
Connected:boolean;
NotifyWnd : HWND; // This is used for the timer
procedure TimerWndProc( var msg: TMessage );
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{******************打开、初始化串口*************************}
procedure TForm3.Button1Click(Sender: TObject);
var
dcb:TDCB;
tms: TCOMMTIMEOUTS;
strOut:string;
WriteNum:Cardinal;
begin
hCom := CreateFile(
'com1',
GENERIC_READ or GENERIC_WRITE,
0, // Not shared
nil, // No security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0 // No template
) ;
if HCom > 0 then
begin
BuildCommDCB('Com1:9600,n,8,1',dcb);
SetCommState(hcom,dcb);
tms.ReadIntervalTimeout := MAXDWORD;
tms.ReadTotalTimeoutMultiplier := 0;
tms.ReadTotalTimeoutConstant := 0;
tms.WriteTotalTimeoutMultiplier := 0;
tms.WriteTotalTimeoutConstant := 1000;
SetCommTimeOuts( hcom, tms );
PurgeComm( hcom, PURGE_TXCLEAR );
PurgeComm( hcom, PURGE_RXCLEAR );
{初始化}
strOut:='ATZ'+#13#10;
WriteFile(hcom,strOut,Length(strOut),WriteNum,nil);
{接收定时器}
NotifyWnd := AllocateHWnd( TimerWndProc );
SetTimer( NotifyWnd, 1, 50, nil );
Connected:=true;
Label1.Caption:='Connect Ok';
end;
end;
{****************************发数据**************************}
procedure TForm3.Memo1KeyPress(Sender: TObject; var Key: Char);
var
nsent: DWord;
begin
if Connected then
WriteFile(hcom,Key,1,nsent,nil);
end;
{****************************读数据**************************}
procedure TForm3.TimerWndProc(var msg: TMessage);
var
nRead: dword;
ReadBuffer : array[0..100] of char;
begin
if (msg.Msg = WM_TIMER) and Connected then
begin
nRead := 0;
if ReadFile( hCom, ReadBuffer, Length(ReadBuffer), nRead, nil ) then
if (nRead <> 0) then
Memo2.Lines.Add(ReadBuffer);
end;
end;
procedure TForm3.FormDestroy(Sender: TObject);
begin
KillTimer(NotifyWnd,1);
CloseHandle(Hcom);
end;
end.
问题:
1)以上程序在Delphi的IDE环境运行一切正常,每在Memo1中写一字母,就在Memo2
中收到,并能正确响应.如:Memo1中输入ATL0,Memo2中立即显示ATL0,Ok.但是一脱离IDE
环境就出异常(access violation...in module 'DERNEL32.DLL')!!! Why???
2)我对Memo1KeyPress改进了一下,当收到回车时才发送,代码如下:
procedure TForm3.Memo1KeyPress(Sender: TObject; var Key: Char);
var
nsent: DWord;
DataSentchar; //用String也不行!!!
SizeSent:integer;
begin
if (Key=#13)and(Connected) then
begin
DataSent:=PChar(Memo1.Lines[Memo1.Lines.Count-1]);
SizeSent:=Length(DataSent);
WriteFile(hcom,DataSent,SizeSent,nsent,nil);
WriteFile(hcom,Key,1,nsent,nil);
end;
end;
Memo2显示的全是乱码!
已做如下测试(对DataSent):
用PChar,用String,用String+#13#10,用String+#0(该法在直接发时有效:'atdt163');
请高手指教!谢谢!
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Label1: TLabel;
Button1: TButton;
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
hCom:Thandle;
Connected:boolean;
NotifyWnd : HWND; // This is used for the timer
procedure TimerWndProc( var msg: TMessage );
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{******************打开、初始化串口*************************}
procedure TForm3.Button1Click(Sender: TObject);
var
dcb:TDCB;
tms: TCOMMTIMEOUTS;
strOut:string;
WriteNum:Cardinal;
begin
hCom := CreateFile(
'com1',
GENERIC_READ or GENERIC_WRITE,
0, // Not shared
nil, // No security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0 // No template
) ;
if HCom > 0 then
begin
BuildCommDCB('Com1:9600,n,8,1',dcb);
SetCommState(hcom,dcb);
tms.ReadIntervalTimeout := MAXDWORD;
tms.ReadTotalTimeoutMultiplier := 0;
tms.ReadTotalTimeoutConstant := 0;
tms.WriteTotalTimeoutMultiplier := 0;
tms.WriteTotalTimeoutConstant := 1000;
SetCommTimeOuts( hcom, tms );
PurgeComm( hcom, PURGE_TXCLEAR );
PurgeComm( hcom, PURGE_RXCLEAR );
{初始化}
strOut:='ATZ'+#13#10;
WriteFile(hcom,strOut,Length(strOut),WriteNum,nil);
{接收定时器}
NotifyWnd := AllocateHWnd( TimerWndProc );
SetTimer( NotifyWnd, 1, 50, nil );
Connected:=true;
Label1.Caption:='Connect Ok';
end;
end;
{****************************发数据**************************}
procedure TForm3.Memo1KeyPress(Sender: TObject; var Key: Char);
var
nsent: DWord;
begin
if Connected then
WriteFile(hcom,Key,1,nsent,nil);
end;
{****************************读数据**************************}
procedure TForm3.TimerWndProc(var msg: TMessage);
var
nRead: dword;
ReadBuffer : array[0..100] of char;
begin
if (msg.Msg = WM_TIMER) and Connected then
begin
nRead := 0;
if ReadFile( hCom, ReadBuffer, Length(ReadBuffer), nRead, nil ) then
if (nRead <> 0) then
Memo2.Lines.Add(ReadBuffer);
end;
end;
procedure TForm3.FormDestroy(Sender: TObject);
begin
KillTimer(NotifyWnd,1);
CloseHandle(Hcom);
end;
end.
问题:
1)以上程序在Delphi的IDE环境运行一切正常,每在Memo1中写一字母,就在Memo2
中收到,并能正确响应.如:Memo1中输入ATL0,Memo2中立即显示ATL0,Ok.但是一脱离IDE
环境就出异常(access violation...in module 'DERNEL32.DLL')!!! Why???
2)我对Memo1KeyPress改进了一下,当收到回车时才发送,代码如下:
procedure TForm3.Memo1KeyPress(Sender: TObject; var Key: Char);
var
nsent: DWord;
DataSentchar; //用String也不行!!!
SizeSent:integer;
begin
if (Key=#13)and(Connected) then
begin
DataSent:=PChar(Memo1.Lines[Memo1.Lines.Count-1]);
SizeSent:=Length(DataSent);
WriteFile(hcom,DataSent,SizeSent,nsent,nil);
WriteFile(hcom,Key,1,nsent,nil);
end;
end;
Memo2显示的全是乱码!
已做如下测试(对DataSent):
用PChar,用String,用String+#13#10,用String+#0(该法在直接发时有效:'atdt163');
请高手指教!谢谢!