using Serilog; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace fwHZUpload { /* * 需要安装MELSOFT官方提供的MX Component组件,并确保PLC设备已经使用Communication Setup Utility工具设置好站点号 */ public class MELSECHelper { private ActUtlTypeLib.ActUtlType melsecUtl = new ActUtlTypeLib.ActUtlType(); private int flag = 0; private int FLAG_OPEND = 0x01;
public string plc_addr_allow_switch = "M3700"; public string plc_addr_start_switch = "M3701"; public string plc_addr_finish_switch = "M3702"; public string plc_addr_heartbeat = "M3703"; public string plc_addr_height = "D3700"; public string plc_addr_innerRace = "D3702"; public string plc_addr_outerRace = "D3704"; // 内圈外径 public string plc_addr_innerRace_outDiameter = "D3725"; // 产品编号 public string plc_addr_product_no = "D3706"; // 产品型号 18*2 个字符 public string plc_addr_product_type = "D3707";
public int Open(int stationNo) { flag = 0; melsecUtl.ActLogicalStationNumber = stationNo; melsecUtl.ActPassword = ""; int iRet = 0; try { iRet = melsecUtl.Open(); } //Exception processing catch (Exception ex) { Log.Error($"open melsec failed. msg={ex.Message}"); return -2; } flag |= FLAG_OPEND; return iRet; }
public int ReadBit(string addr, out int value) { value = 0; if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } return melsecUtl.GetDevice(addr, out value); }
public int WriteBit(string addr, int value) { if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } return melsecUtl.SetDevice(addr, value); }
// 三菱PLC 存储是 小端模式 public int ReadBlockShort(string addr,int count, ref short[] value) { if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } return melsecUtl.ReadDeviceBlock2(addr, count, out value[0]); ; }
public int WriteBlockShort(string addr, int count, ref short[] value) { if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } return melsecUtl.WriteDeviceBlock2(addr,count, ref value[0]); }
/* * Int 相关操作尚未通过测试,暂时没用到。 */ public int ReadBlockInt(string addr, int count, ref int[] value) { if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } return melsecUtl.ReadDeviceBlock(addr, count, out value[0]); }
public int WriteBlockInt(string addr, int count, ref int[] value) { if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } return melsecUtl.WriteDeviceBlock(addr, count, ref value[0]); }
public int Close() { if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } return melsecUtl.Close(); }
public int SetSwitch() { if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } int bVal = 1; int iRet = WriteBit(plc_addr_allow_switch, bVal); if (iRet != 0) { Log.Error($"write plc heatbeat failed. code={iRet}"); return -2; } return bVal; }
public int AllowSwitch() { if (FLAG_OPEND != (flag & FLAG_OPEND)) { return -1; } int bVal = 1; int iRet = ReadBit(plc_addr_allow_switch, out bVal); if (iRet != 0) { Log.Error($"read plc heatbeat failed. code={iRet}"); return -2; } return bVal; }
}
/* * * MELSECHelper melsec = new MELSECHelper(); int iRet = melsec.Open(1); if (iRet != 0) { MessageBox.Show("open plc failed."); }
short[] sData = new short[20]; sData[0] = 0x1111; sData[1] = 0x2222; sData[2] = 0x3333; string devs = "d0"; iRet = melsec.WriteBlockShort(devs, 3, ref sData); sData[0] = 0; sData[1] = 0; sData[2] = 0;
iRet = melsec.ReadBlockShort(devs, 3,ref sData); if (iRet != 0) { MessageBox.Show("open plc failed."); }
int bVal = 1; iRet = melsec.WriteBit("M3700", bVal); if (iRet != 0) { MessageBox.Show("open plc failed."); } bVal = 0; iRet = melsec.ReadBit("M3700", out bVal); if (iRet != 0) { MessageBox.Show("open plc failed."); } if (bVal != 1) { MessageBox.Show("read bit failed."); }
// 保存的数是 0x12345678 short[] iValArr = new short[10]; iValArr[0] = 0x5678; iValArr[1] = 0x1234; iRet = melsec.WriteBlockShort("D3700", 2, ref iValArr); if(iRet != 0) { Console.WriteLine("failed."); } iValArr[0] = 0; iValArr[1] = 0; iRet = melsec.ReadBlockShort("D3700", 2, ref iValArr); if (iRet != 0) { Console.WriteLine("failed."); }
*/
}
|