用Delphi调用C写的DLL
代码如下:
DLL的声明函数前面已经贴出,我也不知道我在Delphi里面声明错了没有,请指点.
function StOpenComm(com:string;i:Integer;o:Integer):LongInt;stdcall;
external 'ComDll.dll';
function StCloseComm(Port:LongInt):Boolean;stdcall;
external 'ComDll.dll';
function StSetComm(comPort:LongInt;LPSTR:string):Integer;stdcall;
external 'ComDll.dll';
function StWriteComm(hPort:LongInt;pBuf:widestring;uWrite:Integer):Integer;stdcall;
external 'ComDll.dll';
function StReadComm(hPort:LongInt;pBuf
char;uRead:Integer):Integer;stdcall;
external 'ComDll.dll';
implementation
{$R *.dfm}
procedure TForm1.btnOpenComClick(Sender: TObject);
begin
Port1 := StOpenComm('COM1',1024,1024);
end;
procedure TForm1.btnCloseComClick(Sender: TObject);
var
result :Boolean;
begin
result:=False;
result:=StCloseComm (Port1);
if result=True then
ShowMessage('已经关闭了COM1口.')
else
ShowMessage('关闭COM口1失败.');
end;
procedure TForm1.btnSetComPortClick(Sender: TObject);
begin
// StSetComm(Port,'baud=9600, parity=N ,data=8, stop=1');
end;
procedure TForm1.btnSendDataClick(Sender: TObject);
var
i:integer;
pBuf : array[0..1024] of byte;
begin
//StWriteComm(Port1,HexStrToStr('$02'),1);
//StWriteComm(Port1,HexStrToStr('$00'),1);
//StWriteComm(Port1,HexStrToStr('$02'),1);
//StWriteComm(Port1,HexStrToStr('$13'),1);
//StWriteComm(Port1,HexStrToStr('$03'),1);
//StWriteComm(Port1,HexStrToStr('$03'),1);
StWriteComm(Port1,'a',1);
StWriteComm(Port1,'b',1);
//pBuf[0] := ord('a');
//pBuf[1] := ord('b');
//StWriteComm(Port1,pBuf,2);
end;
procedure TForm1.btnReceiveDataClick(Sender: TObject);
var
//pBuf
char;
//pBuf:widestring;
pBuf : array[0..1024] of char;
sTemp : String;
i : Integer;
begin
pBuf :='';
i := StReadComm(Port2,pBuf,1024);
sTemp := pBuf;
RichEdit1.Lines.Add(sTemp);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
Port2 := StOpenComm('COM2',1024,1024);
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
var
result :Boolean;
begin
result:=StCloseComm (Port2);
if result=True then
ShowMessage('已经关闭了COM2口.')
else
ShowMessage('关闭COM2口失败.');
end;