大家好!动态连接库的工程文件:library DLLP;uses
ShareMem,
SysUtils,
Classes,
dllu in 'dllu.pas';{$R *.res}exports
openserialport,closeserialport,SendHex,readdata;begin
end.
函数实现单元如下:unit dllu;interfaceuses
ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;var
hserialport : thandle;
procedure openserialport(temp:string);stdcall;
procedure closeserialport;stdcall;
procedure SendHex(sss: String);stdcall;
function readdata:string;stdcall;implementation
procedure openserialport(temp:string);stdcall;
var
cc:tcommconfig;
begin
hserialport := createfile(pchar(temp),generic_read or generic_write,0,nil,
open_existing,0,0);
if (hserialport = invalid_handle_value) then
begin
messagebox(0,'打开串口时发生错误!','',mb_ok);
exit;
end;
getcommstate(hserialport,cc.dcb);
cc.dcb.BaudRate := cbr_9600;
cc.dcb.ByteSize := 8 ;
cc.dcb.Parity := noparity;
cc.dcb.StopBits := onestopbit;
if not setcommstate(hserialport,cc.dcb) then
begin
messagebox(0,'不能设置串口!','',mb_ok);
closehandle(hserialport);
exit;
end;
showmessage('串口已经打开!');
end;procedure closeserialport;stdcall;
begin
closehandle(hserialport);
showmessage('串口已经关闭!');
end;procedure SendHex(sss: String);stdcall;
var
s,s2:string;
buf1:array[0..50000] of char;
i:integer;
lw : longword;
begin
s2:='';
s:=sss;
for i:=1 to length(s) do
begin
if ((copy(s,i,1)>='0') and (copy(s,i,1)<='9'))or((copy(s,i,1)>='a') and (copy(s,i,1)<='f'))
or((copy(s,i,1)>='A') and (copy(s,i,1)<='F')) then
begin
s2:=s2+copy(s,i,1);
end;
end;
for i:=0 to (length(s2) div 2-1) do
buf1[i]:=char(strtoint('$'+copy(s2,i*2+1,2)));
if (hserialport = 0) then
exit;
writefile(hserialport,buf1,(length(s2) div 2),lw,nil);
end;function readdata:string;stdcall;
var
tempstring,tmpStr,wchb:string;
inbuff:array[0..2047] of char;
pstr:pchar;
bytesread,dwerror:longword;
cs:tcomstat;
tmpArray:array[0..4096] of Byte;
i: DWORD;
begin
clearcommerror(hserialport,dwerror,@cs);
if cs.cbInQue > sizeof(inbuff) then
begin
purgecomm(hserialport,purge_rxclear);
exit;
end;
readfile(hserialport,inbuff,cs.cbInQue,bytesread,nil);
pStr:=inbuff;
tmpStr:=string(pStr);
Dec(PStr);
for i:=0 to cs.cbInQue-1 do
begin
inc(PStr);
tmpArray[i]:=Byte(PSTR^);
wchb:=wchb+IntToHEX(Ord(tmpArray[i]),2);
end;
tempstring := wchb;
result:= tempstring;
end;end.
我的问题是:为什么调用readdata时总出错啊????????错误提示是:“Invalid Pointer Operation”。我是没办法了啊啊!!!!!!!!!!!!请高手前辈指导指导!!!分不够在加!!
ShareMem,
SysUtils,
Classes,
dllu in 'dllu.pas';{$R *.res}exports
openserialport,closeserialport,SendHex,readdata;begin
end.
函数实现单元如下:unit dllu;interfaceuses
ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;var
hserialport : thandle;
procedure openserialport(temp:string);stdcall;
procedure closeserialport;stdcall;
procedure SendHex(sss: String);stdcall;
function readdata:string;stdcall;implementation
procedure openserialport(temp:string);stdcall;
var
cc:tcommconfig;
begin
hserialport := createfile(pchar(temp),generic_read or generic_write,0,nil,
open_existing,0,0);
if (hserialport = invalid_handle_value) then
begin
messagebox(0,'打开串口时发生错误!','',mb_ok);
exit;
end;
getcommstate(hserialport,cc.dcb);
cc.dcb.BaudRate := cbr_9600;
cc.dcb.ByteSize := 8 ;
cc.dcb.Parity := noparity;
cc.dcb.StopBits := onestopbit;
if not setcommstate(hserialport,cc.dcb) then
begin
messagebox(0,'不能设置串口!','',mb_ok);
closehandle(hserialport);
exit;
end;
showmessage('串口已经打开!');
end;procedure closeserialport;stdcall;
begin
closehandle(hserialport);
showmessage('串口已经关闭!');
end;procedure SendHex(sss: String);stdcall;
var
s,s2:string;
buf1:array[0..50000] of char;
i:integer;
lw : longword;
begin
s2:='';
s:=sss;
for i:=1 to length(s) do
begin
if ((copy(s,i,1)>='0') and (copy(s,i,1)<='9'))or((copy(s,i,1)>='a') and (copy(s,i,1)<='f'))
or((copy(s,i,1)>='A') and (copy(s,i,1)<='F')) then
begin
s2:=s2+copy(s,i,1);
end;
end;
for i:=0 to (length(s2) div 2-1) do
buf1[i]:=char(strtoint('$'+copy(s2,i*2+1,2)));
if (hserialport = 0) then
exit;
writefile(hserialport,buf1,(length(s2) div 2),lw,nil);
end;function readdata:string;stdcall;
var
tempstring,tmpStr,wchb:string;
inbuff:array[0..2047] of char;
pstr:pchar;
bytesread,dwerror:longword;
cs:tcomstat;
tmpArray:array[0..4096] of Byte;
i: DWORD;
begin
clearcommerror(hserialport,dwerror,@cs);
if cs.cbInQue > sizeof(inbuff) then
begin
purgecomm(hserialport,purge_rxclear);
exit;
end;
readfile(hserialport,inbuff,cs.cbInQue,bytesread,nil);
pStr:=inbuff;
tmpStr:=string(pStr);
Dec(PStr);
for i:=0 to cs.cbInQue-1 do
begin
inc(PStr);
tmpArray[i]:=Byte(PSTR^);
wchb:=wchb+IntToHEX(Ord(tmpArray[i]),2);
end;
tempstring := wchb;
result:= tempstring;
end;end.
我的问题是:为什么调用readdata时总出错啊????????错误提示是:“Invalid Pointer Operation”。我是没办法了啊啊!!!!!!!!!!!!请高手前辈指导指导!!!分不够在加!!
解决方案 »
- 让我建立的窗体挡住任务栏
- ********有关TCP端口侦听的问题********
- 2000,xp下面怎么屏蔽ctrl+alt+del以及其他系统按键啊?
- 如何防止程序被多次执行。
- Delphi的工程文件扩展名是
- 高分请教:如何使用函数来设置窗体大小?
- 怎样把Delphi窗口最小化放入Windows开始状态的第四格中?
- SQL为ls_sql:='select * from X where X.y=''%s'''
- 请问如何让TImage移动的时候不闪烁?或者说减少闪烁?有何良策?
- 用ServerSocket(stThreadBlocking)和ClientSocket(ctBlocking)进行数据通讯
- 关于调用dll的问题……数据返回
- 转换
dll中不要用string,用pchar代替
tmpStr:=string(pStr);
改為:
tempStr := copy(inbuff, 1, cs.cbInQue);
改為:
tmpArray: Byte;Dec(PStr);
for i:=0 to cs.cbInQue-1 do
begin
inc(PStr);
tmpArray[i]:=Byte(PSTR^);
wchb:=wchb+IntToHEX(Ord(tmpArray[i]),2);
end;
改為:
for i:=1 to length(tmpstr) do
begin
tmpArray :=Byte(StrToInt('$' + tmpstr[i]));
wchb:=wchb+IntToHEX(Ord(tmpArray),2);
end;我只是看代碼改, 具體還要測試下