控件安装完后在delphi里调用此控件,打开源代码可直接修改,支持delphi5、6、7
初始化属性
constructor tmodbusm.create( aowner: tcomponent );
begin
inherited create( aowner );
// initialize to default values
fcomporthandle := 0; // not connected
fcomport := pncom2; // com 2
fcomportbaudrate := br9600; // 9600 bauds
fcomportdatabits := db8bits; // 8 data bits
fcomportstopbits := sb1bits; // 1 stop bit
fcomportparity := ptnone; // no parity
fcomporthwhandshaking := hhnone; // no hardware handshaking
fcomportswhandshaking := shnone; // no software handshaking
fcomportinbufsize := 1028; // input buffer of 1024 bytes
fcomportoutbufsize := 1028; // output buffer of 1024 bytes
fcomportpollingdelay := 80; // poll com port every 80ms
foutputtimeout := 5000; // output timeout - 5000ms
fenabledtronopen := false; // dtr high on connect
// temporary buffer for received data
......
......
end;
//
modbus发送
procedure tmodbusm.query;
var
cadenacrc: word;
cadena: tdatabyte;
pcadena: pbyte;
lcadena,i: integer;
bytetemp: byte;
begin
if not connected then begin
ferror:=11;
if assigned(fonerror) then fonerror(self, serrorcodes[11] );
exit;
end;
if fbusy then begin
ferror:=10;
if assigned(fonerror) then fonerror(self, serrorcodes[11]);
exit;
end;
fbusy:=true;
ferror:=0;
setlength(readbuffer, 0);
//功能码1数据打包,依次累推可增加自己所需的其它功能码
case ffunction of
1: begin
setlength(cadena,8);
cadena[0]:=fslaveid;
cadena[1]:=ffunction;
cadena[2]:=hi(foffset);
cadena[3]:=lo(foffset);
cadena[4]:=hi(fquantity);
cadena[5]:=lo(fquantity);
cadenacrc:=crc(cadena);
cadena[high(cadena)-1]:=lo(cadenacrc);
cadena[high(cadena)]:=hi(cadenacrc);
pcadena:=@cadena[0];
lcadena:=high(cadena)+1;
if senddata(pcadena,lcadena)=lcadena
then ftimer.enabled:=true
else begin
ferror:=9;
if assigned(fonerror) then fonerror(self, serrorc----------------------------------------------
此篇文章从博客转发
原文地址: Http://blog.gkong.com/more.asp?id=65049&Name=jiayoua