使用三菱PLC源码进行PLC读取写入操作
-
安装 MX Component 。
-
我的安装地址在:
-
-
打开 utl 文件夹下的 Communication Settings Utility 执行。
配置PLC 添加当前需要配置的PLC 注意 logical station Namber 就是程序里需要对接的逻辑站点编号
5.配置选择对应的COM操作选择对应的cpu型型号,然后测试程序是否可以联通,如果联通则可以继续进行。
5.程序编辑。
5.1 选择 对应的SDK
在此demo下有众多可以适用的功能。
其中已经包含了数据各种谁操作,连接关闭等操作。程序引用相关文件。 修改嵌入方式改成否,选择保存本地。
以下就是我简单写了一个操作类:
public static class MXCommonGet
{public static bool IsConnect;public static ActUtlType64Lib.ActUtlType64Class axActUtlType1 = new ActUtlType64Lib.ActUtlType64Class();//public MXCommonGet()//{// 解决因为第三方控件报错,将实例化的对象添加到控件合集中// //((ISupportInitialize)(this.axActUtlType1)).BeginInit();// //this.Controls.Add(axActUtlType1);// //((ISupportInitialize)(this.axActUtlType1)).EndInit();// }public static bool ConnectM(){axActUtlType1.ActLogicalStationNumber = 2;//填设置的逻辑站号,站号是在MXcomponent软件里设置的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>/// 获取前节点数据/// </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 = "连接出错";} return msg; }/// <summary>/// 设置当前节点数据/// </summary>/// <param name="PointId">节点编号</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 = $"连接出错 {ex.Message}";}return msg;}/// <summary>/// 关闭当前连接/// </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 = "数据不存在";}return msg;}
}