程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Mitsubish FX 3U PLC 串口 連接單元

Mitsubish FX 3U PLC 串口 連接單元

編輯:Delphi

 前段時間遇到一個Mitsubish FX 3U PLC ,現將PLC連接單元分享一下,希望對其他人有所啟示。  

unit PLC_MitsubishiFX;  
  
interface  
  
uses  
  Windows, Messages, SysUtils, Classes, syncobjs,UnitCom, ACTPCCOMLib_TLB,  
  PLC_Base, PLCCommonFunc;  
  
  
  
type  
  TPLC_MitsubishiFX=class(TPLC)  
  private  
    FMyCom:TActFXCPU;{定義串口通信對象}  
  public  
    ConStructor Create; override; {構造函數}  
    destructor Destroy; override; {析構函數}  
    function Open(ComName,IpAddress: string):Integer;override;{打開PLC}  
    function Close:Integer;override;  {關閉PLC}  
    //讀PLC函數  
    function DoRead(Station:Integer; StartAddress:Integer; Count:Integer; Buffer:Pointer; DataType:array of TPLCDataType): Integer;override;  
    //寫PLC函數  
    function DoWrite(Station:Integer; StartAddress:Integer; Count:Integer; Buffer:Pointer; DataType: TPLCDataType):Integer;override;{返回值為寫入成功與否}  
  
  
end;  
  
implementation  
  
{ TPLC_Mitsubishi }  
  
  
  
constructor TPLC_MitsubishiFX.Create;  
begin  
  Inherited;  
  FMyCom:=TActFXCPU.Create(nil); {創建串口通信對象}  
  FMyCom.ActTimeOut:=10000;  
end;  
  
  
destructor TPLC_MitsubishiFX.Destroy;  
begin  
  FMyCom.Free ;{釋放串口通信對象}  
  inherited;  
end;  
  
  
  
function TPLC_MitsubishiFX.Open(ComName,IpAddress: string): Integer;  
begin  
  FMyCom.ActPortNumber :=strtoint(copy(comname,4,length(comname)-3));     //com1  
  Result:=FMyCom.Open; //該函數返回0為成功  
  if Result = 0 then  
    Result := SUCCESS;  
end;  
  
function TPLC_MitsubishiFX.Close: Integer;  
begin  
  Result := FMyCom.Close;{關閉串口通信對象}  
  if Result = 0 then  
    Result := SUCCESS;  
end;  
  
  
function TPLC_MitsubishiFX.DoRead(Station:Integer; StartAddress:Integer; Count:Integer; Buffer:Pointer; DataType:array of TPLCDataType): Integer;  
var  
  DataInfo:TPLCStruct; //接收從Buffer傳來的參數  
  lpdata: array[0..99] of integer;  
  i:integer;  
  LState:integer;  
begin  
  DataInfo := PTPLCStruct(Buffer)^;  
  try  
    LState:=FMyCom.ReadDeviceBlock('D'+ConvertStartAddr(StartAddress),Count,lpdata[0]) ;  
  except  
    LState:=-1;  
  end;  
  
  FLinkState := LState =0;  
  
  if LState<>0 then  //讀取失敗的情況  
  begin  
    Result:=UNSUCCESS;  
    exit;  
  end;  
  
  for i:=0 to Count-1 do  
  begin  
      DataInfo.PLCInteger[i]:=lpdata[i];  
  end;  
  
  PTPLCStruct(Buffer)^:=DataInfo; //傳出讀取的PLC數據  
  Result:=SUCCESS;  
end;  
  
function TPLC_MitsubishiFX.DoWrite(Station:Integer; StartAddress:Integer;  
  Count:Integer; Buffer:Pointer; DataType: TPLCDataType): Integer;  
var  
  DataInfo:TPLCStruct; //接收從Buffer傳來的參數  
  LDataInfo :array[0..100] of integer;  
  i:integer;  
  LState:integer;  
begin  
  DataInfo := PTPLCStruct(Buffer)^;  
//  if DataType = dtHexInt then  
//    for i:=0 to Count - 1 do  
//      LDataInfo[i]:=StrToint('$'+DataInfo.PLCChar[i]) //十六進制  
//  else  
    for i:=0 to Count - 1 do  
      LDataInfo[i]:=DataInfo.PLCInteger[i];  //十進制  
  try  
    LState:=FMyCom.WriteDeviceBlock('D'+ConvertStartAddr(StartAddress),Count,LDataInfo[0]) ;  
  except  
    LState:=-1;  
  end;  
  
  FLinkState := LState = 0;  
  
  if LState = 0 then  
    result:= SUCCESS  
  else  
    result:=UNSUCCESS;  
end;  
  

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved