// ==================================================================================== public int SetDevice(PlcDeviceType iType, int iAddress, int iData) { var data = new StringBuilder(iType.ToAsciiName()); data.AppendFormat("{0:X6}", iAddress); data.AppendFormat("{0:X4}", 1); data.AppendFormat("{0:X4}", iData); byte[] sdCommand = Command.SetCommand(0x1401, 0x0000, data.ToString()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse, CommandFrame); return(rtCode); }
// ==================================================================================== public int WriteDeviceBlock(PlcDeviceType iType, int iAddress, int iSize, int[] iData) { var data = new StringBuilder(iType.ToAsciiName()); data.AppendFormat("{0:X6}", iAddress); data.AppendFormat("{0:X4}", iSize); foreach (int t in iData) { data.AppendFormat("{0:X4}", t); } byte[] sdCommand = Command.SetCommand(0x1401, 0x0000, data.ToString()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse, CommandFrame); return(rtCode); }
// ==================================================================================== public int ReadDeviceBlock(PlcDeviceType iType, int iAddress, int iSize, int[] oData) { var data = new StringBuilder(iType.ToAsciiName()); data.AppendFormat("{0:X6}", iAddress); data.AppendFormat("{0:X4}", iSize); byte[] sdCommand = Command.SetCommand(0x0401, 0x0000, data.ToString()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse, CommandFrame); byte[] rtData = Command.Response; for (int i = 0; i < iSize; ++i) { oData[i] = (rtCode == 0) ? BitConverter.ToInt16(rtData, i * 2) : 0; } return(rtCode); }
// ==================================================================================== // 位单元的随机写入 public int SetBitDevice(PlcDeviceType iType, Dictionary <int, byte> iAddressAndOnOffMap) { StringBuilder data = new StringBuilder(); data.AppendFormat("{0:00}", iAddressAndOnOffMap.Count); foreach (var kv in iAddressAndOnOffMap) { data.AppendFormat(iType.ToAsciiName()); data.AppendFormat("{0:000000}", kv.Key); // 地址 data.AppendFormat("{0:X2}", kv.Value); // On/Off } byte[] sdCommand = Command.SetCommand(0x1402, 0x0001, data.ToString()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse, CommandFrame); return(rtCode); }
// ==================================================================================== // 位单元的连续写入 public int SetBitDevice(PlcDeviceType iType, int iAddress, int iSize, byte[] onOffBits) { StringBuilder data = new StringBuilder(); data.AppendFormat("{0}", iType.ToAsciiName()); data.AppendFormat("{0:000000}", iAddress); data.AppendFormat("{0:0000}", iSize); for (int i = 0; i < onOffBits.Length; i++) { data.AppendFormat("{0}", onOffBits[i] == 1 ? '1' : '0'); } byte[] sdCommand = Command.SetCommand(0x1401, 0x0001, data.ToString()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse, CommandFrame); return(rtCode); }
// ==================================================================================== public int GetDevice(PlcDeviceType iType, int iAddress, out int oData) { int addr = iAddress; var data = new StringBuilder(iType.ToAsciiName()); data.AppendFormat("{0:X6}", addr); data.AppendFormat("{0:X4}", 1); byte[] sdCommand = Command.SetCommand(0x0401, 0x0000, data.ToString()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse, CommandFrame); if (0 < rtCode) { oData = 0; } else { byte[] rtData = Command.Response; oData = BitConverter.ToInt16(rtData, 0); } return(rtCode); }
// ==================================================================================== public int GetBitDevice(PlcDeviceType iType, int iAddress, int iSize, byte[] outOnOffBits) { var data = new StringBuilder(iType.ToAsciiName()); data.AppendFormat("{0:X6}", iAddress); data.AppendFormat("{0:X4}", iSize); byte[] sdCommand = Command.SetCommand(0x0401, 0x0001, data.ToString()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse, CommandFrame); byte[] rtData = Command.Response; for (int i = 0; i < iSize; ++i) { if (i % 2 == 0) { outOnOffBits[i] = (byte)((rtCode == 0) ? ((rtData[i / 2] >> 4) & 0x01) : 0); } else { outOnOffBits[i] = (byte)((rtCode == 0) ? (rtData[i / 2] & 0x01) : 0); } } return(rtCode); }