如题,
解决方案 »
- Tlistview简单问题大家麻烦看下。
- 紧急求救!!!安装数据库的问题,高手请进!!!!!!!!!
- 如何把一个数据库里的多个表转到另外一个数据库里,其中支持修改表名和字段名?(在线等)
- 小弟有将Access转换成SQLServer时(数据库结构相同),SQL语句代码异同几点总结,愿交换.
- 请问如何在delphi中调用access中的模块和宏?
- 请问MS SQL查询分析器上的写代码的时候各个颜色代表什么类型
- 这是一个WORD文档文件头被破坏后,如何修复的问题?
- 修改数据库问题,急,在线等待!
- api
- 我写了个ASP组件,用regsvr32.exe注册后,又反注册了。我的dll文件删除不掉了?
- 如何读XML文件?
- 多层数据库连接的种类有几种??
unit MainUnit;interfaceuses
Windows, SysUtils, Forms, SPComm, Messages;type
TDllComm = class(TComm)
private
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
end;const
ERR_SC_SUCCESS = 0;
ERR_SC_NOTACTIVE = 1;
ERR_SC_BUSY = 2;
ERR_SC_TIMEOUT = 3;var
IsActive: Boolean = False;
IsBusy: Boolean = False;
Received: Boolean; RcvBuf: PChar;
BufLen: PInteger;
Comm: TDllComm;
ReadTimeout: DWord;type
TReceiveProc = function (RcvBuffer: PChar; Len: Integer): Integer;function OpenComm(Com: DWord; Bandrate: DWord): Boolean; stdcall;
procedure CloseComm(); stdcall;
function SendCommCmd(Command: PChar; RcvBuffer: PChar; var Len: Integer): Integer; stdcall;
procedure SetCommTimeout(Timeout: DWord); stdcall;function SendData(Data: PChar; Len: Integer): Integer;
procedure SetReceiveProc(OnReceive: TReceiveProc); stdcall;implementationvar
ReceiveProc: TReceiveProc;procedure TDllComm.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
begin
if IsBusy then
begin
CopyMemory(RcvBuf, Buffer, BufferLength);
BufLen^ := BufferLength;
Received := True;
end; if Assigned(ReceiveProc) then
ReceiveProc(Buffer, BufferLength);
end;function OpenComm(Com: DWord; Bandrate: DWord): Boolean; stdcall;
begin
if IsActive then CloseComm(); ReadTimeout := 30000;
Comm := TDllComm.Create(nil);
Comm.CommName := 'COM' + IntToStr(Com);
Comm.OnReceiveData := Comm.Comm1ReceiveData;
Comm.BaudRate := Bandrate;
Comm.Inx_XonXoffFlow := False;
Comm.Outx_XonXoffFlow := False;
try
Comm.StartComm;
Sleep(200);
IsActive := True;
except
IsActive := False;
end;
Result := IsActive;
end;procedure CloseComm(); stdcall;
begin
if not IsActive then Exit; Comm.StopComm;
IsActive := False;
Comm.Free;
end;function SendCommCmd(Command: PChar; RcvBuffer: PChar; var Len: Integer): Integer; stdcall;
var
i: DWord;
begin
if not IsActive then
begin
Result := ERR_SC_NOTACTIVE;
Exit;
end;
if IsBusy then
begin
Result := ERR_SC_BUSY;
Exit;
end; IsBusy := True;
try
Received := False;
RcvBuf := RcvBuffer;
BufLen := @Len; Comm.WriteCommData(Command, Len);
i := GetTickCount();
while (not Received) and IsActive do
begin
if GetTickCount() - i >= ReadTimeout then
begin
Result := ERR_SC_TIMEOUT;
Exit;
end;
Application.ProcessMessages;
end;
if not IsActive then
begin
Result := ERR_SC_NOTACTIVE;
Exit;
end; Result := ERR_SC_SUCCESS; finally
IsBusy := False;
end;
end;procedure SetCommTimeout(Timeout: DWord); stdcall;
begin
ReadTimeout := Timeout;
end;function SendData(Data: PChar; Len: Integer): Integer;
begin
if not IsActive then
begin
Result := ERR_SC_NOTACTIVE;
Exit;
end;
if IsBusy then
begin
Result := ERR_SC_BUSY;
Exit;
end; IsBusy := True;
try
Comm.WriteCommData(Data, Len);
Result := ERR_SC_SUCCESS;
finally
IsBusy := False;
end;
end;procedure SetReceiveProc(OnReceive: TReceiveProc);
begin
ReceiveProc := OnReceive;
end;end.