網(wǎng)站建設(shè)目錄結(jié)構(gòu)doc站長(zhǎng)工具官網(wǎng)查詢
-
安裝 MX Component 。
-
我的安裝地址在:
-
-
打開 utl 文件夾下的 Communication Settings Utility 執(zhí)行。
配置PLC 添加當(dāng)前需要配置的PLC 注意 logical station Namber 就是程序里需要對(duì)接的邏輯站點(diǎn)編號(hào)
5.配置選擇對(duì)應(yīng)的COM操作選擇對(duì)應(yīng)的cpu型型號(hào),然后測(cè)試程序是否可以聯(lián)通,如果聯(lián)通則可以繼續(xù)進(jìn)行。
5.程序編輯。
5.1 選擇 對(duì)應(yīng)的SDK
在此demo下有眾多可以適用的功能。
其中已經(jīng)包含了數(shù)據(jù)各種誰操作,連接關(guān)閉等操作。程序引用相關(guān)文件。 修改嵌入方式改成否,選擇保存本地。
以下就是我簡(jiǎn)單寫了一個(gè)操作類:
public static class MXCommonGet
{public static bool IsConnect;public static ActUtlType64Lib.ActUtlType64Class axActUtlType1 = new ActUtlType64Lib.ActUtlType64Class();//public MXCommonGet()//{// 解決因?yàn)榈谌娇丶?bào)錯(cuò),將實(shí)例化的對(duì)象添加到控件合集中// //((ISupportInitialize)(this.axActUtlType1)).BeginInit();// //this.Controls.Add(axActUtlType1);// //((ISupportInitialize)(this.axActUtlType1)).EndInit();// }public static bool ConnectM(){axActUtlType1.ActLogicalStationNumber = 2;//填設(shè)置的邏輯站號(hào),站號(hào)是在MXcomponent軟件里設(shè)置的axActUtlType1.ActPassword = "";//密碼int iReturnCode = axActUtlType1.Open();//嘗試連接PLC,如果連接成功則返回值為0if (iReturnCode == 0){OPCommon.LogWrite.WriteLog("PLC連接成功!");IsConnect = true;return true;}else{OPCommon.LogWrite.WriteLog("PLC連接失敗!");IsConnect = false;return false;}}/// <summary>/// 獲取前節(jié)點(diǎn)數(shù)據(jù)/// </summary>/// <param name="PointId"></param>/// <returns></returns>public static ResultMsg GetValueInPoint(string PointId){ResultMsg msg = new ResultMsg();try{if ( IsConnect){int relust;int iReturnCode = axActUtlType1.GetDevice(PointId, out relust);if (iReturnCode == 0){msg.ReturnInt = relust;msg.Success = true;}else{msg.Success = false;}}else{ConnectM();msg.Success = true;msg.ReturnInt = 0;}}catch (Exception ex){msg.Success = false;msg.ReturnInt = 0;msg.ErrMsg = "連接出錯(cuò)";} return msg; }/// <summary>/// 設(shè)置當(dāng)前節(jié)點(diǎn)數(shù)據(jù)/// </summary>/// <param name="PointId">節(jié)點(diǎn)編號(hào)</param>/// <param name="Result"></param>/// <returns></returns>public static ResultMsg SetValueInPoint(string PointId,int Result){ResultMsg msg = new ResultMsg();try{if ( IsConnect){ int iReturnCode = axActUtlType1.SetDevice(PointId, Result);if (iReturnCode == 0){msg.ReturnInt = Result;msg.Success = true;}else{msg.Success = false;}}else{ConnectM();msg.Success = true;msg.ReturnInt = 0;}}catch (Exception ex){msg.Success = false;msg.ReturnInt = 0;msg.ErrMsg = $"連接出錯(cuò) {ex.Message}";}return msg;}/// <summary>/// 關(guān)閉當(dāng)前連接/// </summary>/// <returns></returns>public static ResultMsg CloseThisMX(){ResultMsg msg = new ResultMsg();if (axActUtlType1 != null){int iReturnCode = axActUtlType1.Close();if (iReturnCode == 0){msg.Success = true;msg.ReturnInt = 0;}else{msg.Success = false ;msg.ReturnInt = iReturnCode;}}else{msg.Success = false;msg.ErrMsg = "數(shù)據(jù)不存在";}return msg;}
}