D
duancy
Unregistered / Unconfirmed
GUEST, unregistred user!
盘点机光盘中只有一个VC的说明文件,我用delphi测试可以连接了,但是有一个奇怪的问题:
读取的第一条记录总是空的,而以后的记录正常!请大家帮忙,代码如下:
unit UnitCpt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFormCpt = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//动态库的函数声明
function OpenIrCom(nport:smallint; nBaud:longint): longint; stdcall;far;external 'IrLib.dll';
function vbOpenIrCom(nport:smallint; nBaud:longint): longint; stdcall;far;external 'IrLib.dll';
procedure CloseIrCom()stdcall;far;external 'IrLib.dll';
procedure WriteIrCom(Strchar)stdcall;far;external 'IrLib.dll';
function ReadIrCom()char;stdcall;far;external 'IrLib.dll';
function vbReadIrCom(arrchar):longint;stdcall;far;external 'IrLib.dll';
function ShowErrorMessage(nShow:smallint):smallint;stdcall;far;external 'IrLib.dll';
var
FormCpt: TFormCpt;
implementation
{$R *.dfm}
procedure TFormCpt.Button1Click(Sender: TObject);
var
szData : array[0..255] of char;
begin
if combobox1.ItemIndex < 0 then
begin
MessageBox (Handle, '端口没有选择!', '错误', MB_OK);
exit;
end;
memo1.Lines.Clear;
memo1.Lines.Add('开始');
OpenIrCom(Combobox1.ItemIndex+1, 115200);
WriteIrCom('READ' + #13);
while True do
begin
fillchar(szData[low(szData)],length(szData),#0);
StrPCopy(szData, ReadIrCom());
if szData = 'OVER' + #13 then
begin
MessageBox (Handle, '传输完成!', '提示', MB_OK);
break;
end;
if szData = 'NAK' + #13 then
begin
MessageBox (Handle, '命令错误!', '提示', MB_OK);
break;
end;
// if szData = '' then //注意:第一条记录总是出现为空,没办法,暂时不用
// begin
// MessageBox (Handle, '传输错误!', '提示', MB_OK);
// break;
// end;
WriteIrCom ('ACK' + #13); //如果NAK则表示接受错误,需要重发数据
memo1.Lines.Add(szData);
sleep(100)
end;
memo1.Lines.Add('结束');
CloseIrCom();
end;
procedure TFormCpt.Button2Click(Sender: TObject);
begin
close;
end;
end.
读取的第一条记录总是空的,而以后的记录正常!请大家帮忙,代码如下:
unit UnitCpt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFormCpt = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//动态库的函数声明
function OpenIrCom(nport:smallint; nBaud:longint): longint; stdcall;far;external 'IrLib.dll';
function vbOpenIrCom(nport:smallint; nBaud:longint): longint; stdcall;far;external 'IrLib.dll';
procedure CloseIrCom()stdcall;far;external 'IrLib.dll';
procedure WriteIrCom(Strchar)stdcall;far;external 'IrLib.dll';
function ReadIrCom()char;stdcall;far;external 'IrLib.dll';
function vbReadIrCom(arrchar):longint;stdcall;far;external 'IrLib.dll';
function ShowErrorMessage(nShow:smallint):smallint;stdcall;far;external 'IrLib.dll';
var
FormCpt: TFormCpt;
implementation
{$R *.dfm}
procedure TFormCpt.Button1Click(Sender: TObject);
var
szData : array[0..255] of char;
begin
if combobox1.ItemIndex < 0 then
begin
MessageBox (Handle, '端口没有选择!', '错误', MB_OK);
exit;
end;
memo1.Lines.Clear;
memo1.Lines.Add('开始');
OpenIrCom(Combobox1.ItemIndex+1, 115200);
WriteIrCom('READ' + #13);
while True do
begin
fillchar(szData[low(szData)],length(szData),#0);
StrPCopy(szData, ReadIrCom());
if szData = 'OVER' + #13 then
begin
MessageBox (Handle, '传输完成!', '提示', MB_OK);
break;
end;
if szData = 'NAK' + #13 then
begin
MessageBox (Handle, '命令错误!', '提示', MB_OK);
break;
end;
// if szData = '' then //注意:第一条记录总是出现为空,没办法,暂时不用
// begin
// MessageBox (Handle, '传输错误!', '提示', MB_OK);
// break;
// end;
WriteIrCom ('ACK' + #13); //如果NAK则表示接受错误,需要重发数据
memo1.Lines.Add(szData);
sleep(100)
end;
memo1.Lines.Add('结束');
CloseIrCom();
end;
procedure TFormCpt.Button2Click(Sender: TObject);
begin
close;
end;
end.