【正文】
進(jìn)制代碼function strToHexStr(str:string):string。ss:string。beginwhile str39。 do begin c:=str[1]。%0x39。 delete(str,1,1)。strtohexStr:= ss。//16進(jìn)制字符串轉(zhuǎn)換成字符串function HexStrToStr(const S:string):string。ts:string。begint:=1。39。039。939。A39。F39。a39。f39。 if (t+1Length(S))or(not (S[t+1] in [39。..39。,39。..39。,39。..39。])) then ts:=39。+S[t] else ts:=39。+S[t]+S[t+1]。 if Code=0 then Result:=Result+Chr(M)。end。//替換字符串函數(shù)function replacing(s,source,target:string):string。 begin {source在S中出現(xiàn)的位置} site:=pos(source,s)。 {刪除source字符串} delete(s,site,StrLen)。 {返回新串} replacing:=s。//// CalCRC16用于計算Modbus RTU的CRC16// 多項式公式為X16+X15+X2+1//function CalCRC16(AData:array of Byte。const GENP=$A001。 i:Integer。procedure CalOneByte(AByte:Byte)。begin crc:=crc xor AByte。 //取出最低位 crc:=crc shr 1。 //將最高位置0 if tmp=1 then //檢測移出的位,如果為1,那么與多項式異或 crc:=crc xor GENP。 end。begin crc:=$FFFF。 Result:=crc。function returnCRC16(vStr1:String):String。 i,j,Count:Integer。 ResStr:string。begin szData:=Trimplace(vStr1)。 //讀入需要計算的字符串長度 i:=1。 for j:=0 to Count1 do begin if (i mod 2)=0 then //每2個字符放入一個字節(jié)中 i:=i+1。 Data[j]:=StrToInt(39。+copy(szData,i,2))。 end。 Res:=CalCRC16(Data,Low(Data),Count1)。 Result:=RightStr(ResStr,2)+39。+LeftStr(ResStr,2)。//=================================設(shè)置串口=====================================procedure ()。 if 0 then begin try :=0。 :=。 //設(shè)置串口的當(dāng)前波特率 。 // :=39。++39。 except := 39。++39。 end。串口狀態(tài):39。串口已關(guān)閉39。 //showmessage(inttostr())。//==============================打開串口==================================procedure (Sender: TObject)。 //設(shè)置串口的當(dāng)前波特率 :=。Even(偶)39。 if =39。 then :=Mark。None(無)39。 if =39。 then :=Odd。Space(空格)39。 if =39。 then //設(shè)置數(shù)據(jù)位 :=_5。639。 //設(shè)置數(shù)據(jù)位 if =39。 then :=_7。839。 //設(shè)置數(shù)據(jù)位 if =39。 then //設(shè)置停止位 :=_1。39。 if =39。 then :=_2。 。狀態(tài):39。串口已開始捕獲39。狀態(tài):39。串口打開失敗39。 //:=inttoStr()。end。begin 。狀態(tài):39。串口已停止捕獲39。//==============================設(shè)置串口號======================================procedure (Sender: TObject)。end。vari:integer。begin viewstring:=39。 mflg:=true。 for i:=1 to high(sbuf) do begin if not (sbuf[i],1) then begin mflg:=false。 end。 viewstring:=viewstring+inttohex(sbuf[i],2)+39。 if not mflg then messagedlg(39。,mterror,[mbyes],0)。 viewstring:=39。+viewstring。 //(39。)。// ==========================串口發(fā)送數(shù)據(jù)=======================================procedure sentcustom(SqrStr:string)。 i,j,k:integer。begin if SqrStr39。 then begin str11:=Trimplace(SqrStr)。 //獲得字符串長度,除2取整后加1 //showmessage(inttostr(i))。 //重新設(shè)定發(fā)送數(shù)組范圍 //showmessage(inttostr(high(sbuf)))。 //showmessage(tmpstr)。39。0039。$39。 //將變量轉(zhuǎn)換為byte數(shù)組 end。 sbuf[1]:=byte($01)。 sbuf[3]:=byte($00)。 sbuf[5]:=byte($00)。 sbuf[7]:=byte($85)。 end。end。 Buffer: Pointer。 type ss=array[1..8192]of char 。 i:integer。 n:integer。begin str1:=Buffer。039。139。239。339。E39。 for i:=10 to 15 do s[i]:=chr(55+i)。39。 string1:=string1+s[n div 16]+s[n mod 16]+ 39。 // 除取整 除取余