기술자료/델파이자료
Modbus RTU sample DOC [PDF]
theF
2012. 1. 5. 16:00
구글링으로 얻은 PDF
원 저작자님 감사 합니다. ^^;
응용한 프로그램 소스 중 일부
- Cport 264 사용..
- 온도 모니터링 하는데, 가끔 뻑난단다.. 콤포 문젠지 뭔지 감이 안잡힘....젠장.
procedure TfrmMain.ComTmpRxChar(Sender: TObject; Count: Integer); var i:integer; RX_Char:char;//수신된 값 RX_Array:array[1..200]of string;//수신된 데이타 rx_s : string; begin //ComPort1.Readstr(rx_s,count); //Edit_RX.Text := Edit_RX.Text + rx_s; //010304021600F81BCD for i:=1 to count do begin ComTmp.Read(RX_Char,1); RX_Array[i]:=inttohex(byte(RX_Char),2); RxBuf2:=RxBuf2+RX_Array[i]; end; if (18 = length(RxBuf2)) and (RxBuf2[3] = '0') and (RxBuf2[4] = '4') then begin FlterTmp(RxBuf2); Edit_Rx.Text := RxBuf2; ComTmp.ClearBuffer(True,True); RxBuf2 := ''; exit; end; if (10 = length(RxBuf2)) and ((RxBuf2[3] = '8') and (RxBuf2[4] = '1')) or ((RxBuf2[3] = '8') and (RxBuf2[4] = '4')) then begin FlterTmp(RxBuf2); Edit_Rx.Text := RxBuf2; ComTmp.ClearBuffer(True,True); RxBuf2 := ''; exit; end; end; procedure TfrmMain.FlterTmp(tmpstr:string); var i, j, iTag,k, tmparryMax, tmparryMin : integer; oStr, iCut1, iCut2, iCut3, iCut4 : string; Dcut1, Dcut2, Dcut3, Dcut4 : real; begin tmrERRch.Enabled := False; oStr := tmpstr; if tmrMoni.Enabled = False then begin iTag := StrToInt(edtchNo.text); end else begin iTag := BtnTmpRead.Tag; end; if Length(oStr) < 18 then begin workD[iTag].iValue := 0; workD[iTag].iValue2 := 0; TEdit(FindComponent('EdtTemp_'+FormatFloat('00',iTag))).Text := 'ERR';//FormatFloat('0.0',Dcut1); TEdit(FindComponent('EdtHy_'+FormatFloat('00',iTag))).Text := 'ERR';//FormatFloat('0.0',Dcut2); LogADD('CH'+FormatFloat('00',iTag)+' ERROR '+ oStr); if tmrMoni.Enabled = False then begin exit; end; end else begin iCut1 := Copy(oStr,1,2); //ID iCut2 := Copy(oStr,7,4); //온도 iCut3 := Copy(oStr,11,4); //습도 Dcut2 := (StrToIntDef('$'+iCut2,0))/100; Dcut3 := (StrToIntDef('$'+iCut3,0))/100; workD[iTag].iValue := Dcut2; workD[iTag].iValue2 := Dcut3; TEdit(FindComponent('EdtTemp_'+FormatFloat('00',iTag))).Text := FormatFloat('0.0',Dcut2); TEdit(FindComponent('EdtHy_'+FormatFloat('00',iTag))).Text := FormatFloat('0.0',Dcut3); //LogADD('flterRTN'); end; for i:=8 downto 1 do begin if workD[i].ichkuse then begin tmparryMax := i; break; end; end; for i:=1 to 8 do begin if workD[i].ichkuse then begin tmparryMin := i; break; end; end; BtnTmpRead.Tag := BtnTmpRead.Tag +1; if BtnTmpRead.Tag > tmparryMax then begin BtnTmpRead.Tag := tmparryMin; Measuring; end; end; procedure TfrmMain.BtnTmpReadClick(Sender: TObject); var i,j, k:integer; CRC_Data_Len:integer;//CRC 계산에 사용될 데이타 길이 CRC_Data_Array:array[1..30]of byte;//CRC 계산에 사용될 데이타 Result_CRC:Word;//계산된 CRC값 TX_Data_Size:integer;//송신할 데이타 길이 TX_Data_Array:array[1..32]of byte;//송신할 데이타 tmpstr : string; tmpchV, tmparryMax : integer; begin tmrERRch.Enabled := True; for i:=8 downto 1 do begin if workD[i].ichkuse then begin tmparryMax := i; break; end; end; if tmrMoni.Enabled then begin for k := BtnTmpRead.Tag to tmparryMax do begin if workD[k].ichkuse then begin BtnTmpRead.Tag := k; Break; end; end; tmpchV := BtnTmpRead.Tag; edtchNo.Text := FormatFloat('00',tmpchV); end; edtchNo.Text := FormatFloat('00',strtoint(edtchNo.Text)); Edit_Tx.Text := edtchNo.Text + '04' + '0000' + '0002'; CRC_Data_Len:=round(length(Edit_TX.Text)/2); For i:=1 to CRC_Data_Len do begin j:=i*2-1; CRC_Data_Array[i]:=byte(strtoint('$'+Edit_TX.Text[j]+Edit_TX.Text[j+1])); end; Result_CRC:=Make_CRCValue(CRC_Data_Array,CRC_Data_Len); EdtCRC16.Text:= inttohex(Lo(Result_CRC),2)+inttohex(Hi(Result_CRC),2); if (ConfD.iSimulate = True) or (ComTmp.Connected = False) then exit; Edit_RX.Text:=''; tmpstr := Edit_TX.Text + EdtCRC16.Text; TX_Data_Size:=round(length(tmpstr)/2); For i:=1 to TX_Data_Size do begin j:=i*2-1; TX_Data_Array[i]:=byte(strtoint('$'+tmpstr[j]+tmpstr[j+1])); end; ComTmp.Write(TX_Data_Array,TX_Data_Size); end; //Table에 의한 CRC값 생성 함수 function TfrmMain.Make_CRCvalue(Data: array of byte; nByte: word): Word; var CRCValue:Word; i:integer; begin CRCvalue:=$FFFF; For i := 1 to nByte do CRCvalue := Hi(CRCvalue) XOR CRC_Table[ Data[i-1] XOR Lo(CRCvalue) ]; Result:=CRCvalue; end; procedure TfrmMain.btntmrstopClick(Sender: TObject); begin tmrMoni.Enabled := not tmrMoni.Enabled; if tmrMoni.Enabled then begin btntmrstop.Caption := 'Auto Run.'; end else begin btntmrstop.Caption := 'Manual Run.'; end; end;