// ==================================================================================== public int WriteDeviceBlock(PlcDeviceType iType, int iAddress, int iSize, int[] iData) { PlcDeviceType type = iType; int addr = iAddress; var data = new List <byte>(6) { (byte)addr , (byte)(addr >> 8) , (byte)(addr >> 16) , (byte)type , (byte)iSize , (byte)(iSize >> 8) }; foreach (int t in iData) { data.Add((byte)t); data.Add((byte)(t >> 8)); } byte[] sdCommand = Command.SetCommand(0x1401, 0x0000, data.ToArray()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse); return(rtCode); }
// ==================================================================================== public int GetBitDevice(PlcDeviceType iType, int iAddress, int iSize, int[] oData) { PlcDeviceType type = iType; int addr = iAddress; var data = new List <byte>(6) { (byte)addr , (byte)(addr >> 8) , (byte)(addr >> 16) , (byte)type , (byte)iSize , (byte)(iSize >> 8) }; byte[] sdCommand = Command.SetCommand(0x0401, 0x0001, data.ToArray()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse); byte[] rtData = Command.Response; for (int i = 0; i < iSize; ++i) { if (i % 2 == 0) { oData[i] = (rtCode == 0) ? ((rtData[i / 2] >> 4) & 0x01) : 0; } else { oData[i] = (rtCode == 0) ? (rtData[i / 2] & 0x01) : 0; } } return(rtCode); }
// ==================================================================================== public int ReadDeviceBlock(PlcDeviceType iType, int iAddress, int iSize, int[] oData) { PlcDeviceType type = iType; int addr = iAddress; var data = new List <byte>(6) { (byte)addr , (byte)(addr >> 8) , (byte)(addr >> 16) , (byte)type , (byte)iSize , (byte)(iSize >> 8) }; byte[] sdCommand = Command.SetCommand(0x0401, 0x0000, data.ToArray()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse); 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 GetDevice(PlcDeviceType iType, int iAddress, out int oData) { PlcDeviceType type = iType; int addr = iAddress; var data = new List <byte>(6) { (byte)addr , (byte)(addr >> 8) , (byte)(addr >> 16) , (byte)type , 0x01 , 0x00 }; byte[] sdCommand = Command.SetCommand(0x0401, 0x0000, data.ToArray()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse); if (0 < rtCode) { oData = 0; } else { byte[] rtData = Command.Response; oData = BitConverter.ToInt16(rtData, 0); } return(rtCode); }
// ==================================================================================== public static bool IsHexDevice(PlcDeviceType type) { return((type == PlcDeviceType.X) || (type == PlcDeviceType.Y) || (type == PlcDeviceType.B) || (type == PlcDeviceType.W)); }
// ==================================================================================== public static bool IsBitDevice(PlcDeviceType type) { return(!((type == PlcDeviceType.D) || (type == PlcDeviceType.SD) || (type == PlcDeviceType.Z) || (type == PlcDeviceType.ZR) || (type == PlcDeviceType.R) || (type == PlcDeviceType.W))); }
// ==================================================================================== 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 SetBitDevice(PlcDeviceType iType, int iAddress, int iSize, int[] iData) { var type = iType; var addr = iAddress; var data = new List <byte>(6) { (byte)addr , (byte)(addr >> 8) , (byte)(addr >> 16) , (byte)type , (byte)iSize , (byte)(iSize >> 8) }; var d = (byte)iData[0]; var i = 0; while (i < iData.Length) { if (i % 2 == 0) { d = (byte)iData[i]; d <<= 4; } else { d |= (byte)(iData[i] & 0x01); data.Add(d); } ++i; } if (i % 2 != 0) { data.Add(d); } byte[] sdCommand = Command.SetCommand(0x1401, 0x0001, data.ToArray()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse); return(rtCode); }
// ==================================================================================== public int SetDevice(PlcDeviceType iType, int iAddress, int iData) { PlcDeviceType type = iType; int addr = iAddress; var data = new List <byte>(6) { (byte)addr , (byte)(addr >> 8) , (byte)(addr >> 16) , (byte)type , 0x01 , 0x00 , (byte)iData , (byte)(iData >> 8) }; byte[] sdCommand = Command.SetCommand(0x1401, 0x0000, data.ToArray()); byte[] rtResponse = TryExecution(sdCommand); int rtCode = Command.SetResponse(rtResponse); 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); }
// ==================================================================================== public static void GetDeviceCode(string iDeviceName, out PlcDeviceType oType, out int oAddress) { string s = iDeviceName.ToUpper(); string strAddress; // 取出一个字 string strType = s.Substring(0, 1); switch (strType) { case "A": case "B": case "D": case "F": case "L": case "M": case "R": case "V": case "W": case "X": case "Y": // 第二个字母以后应该是数字,所以要转换 strAddress = s.Substring(1); break; case "Z": // 再拿出一个字 strType = s.Substring(0, 2); // 文件寄存器的情况 : 2 // 索引寄存器时 : 1 strAddress = s.Substring(strType.Equals("ZR") ? 2 : 1); break; case "C": // 再拿出一个字 strType = s.Substring(0, 2); switch (strType) { case "CC": case "CM": case "CN": case "CS": case "CT": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; case "S": // 再拿出一个字 strType = s.Substring(0, 2); switch (strType) { case "SD": case "SM": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; case "T": // 再拿出一个字 strType = s.Substring(0, 2); switch (strType) { case "TC": case "TM": case "TN": case "TS": case "TT": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; default: throw new Exception("Invalid format."); } oType = GetDeviceType(strType); oAddress = IsHexDevice(oType) ? Convert.ToInt32(strAddress, BlockSize) : Convert.ToInt32(strAddress); }
// ==================================================================================== public static void GetDeviceCode(string iDeviceName, out PlcDeviceType oType, out int oAddress) { string s = iDeviceName.ToUpper(); string strAddress; // 获取软元件的首字母(大写) string strType = s.Substring(0, 1); switch (strType) { case "A": case "B": case "D": case "F": case "L": case "M": case "R": case "V": case "W": case "X": case "Y": // 获取这个软元件号,比如D1F4,则1F4被提取 strAddress = s.Substring(1); break; case "Z": // Z开头可能是变址寄存器Z,也可能是文件寄存器ZR strType = s.Substring(0, 2); // 如果是文件寄存器ZR : 2 // 变址寄存器Z : 1 strAddress = s.Substring(strType.Equals("ZR") ? 2 : 1); if (strType != "ZR") { strType = "Z"; } break; case "C": strType = s.Substring(0, 2); switch (strType) { case "CC": case "CM": case "CN": case "CS": case "CT": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; case "S": strType = s.Substring(0, 2); switch (strType) { case "SD": case "SM": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; case "T": strType = s.Substring(0, 2); switch (strType) { case "TC": case "TM": case "TN": case "TS": case "TT": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; default: throw new Exception("Invalid format."); } oType = GetDeviceType(strType); oAddress = IsHexDevice(oType) ? Convert.ToInt32(strAddress, BlockSize) : Convert.ToInt32(strAddress); }
public static byte[] ToAsciiNameBytes(this PlcDeviceType deviceType) { return(ASCIIEncoding.ASCII.GetBytes(typeMapping[deviceType])); }
// ==================================================================================== public static void GetDeviceCode(string iDeviceName, out PlcDeviceType oType, out int oAddress) { string s = iDeviceName.ToUpper(); string strAddress; // 1文字取り出す string strType = s.Substring(0, 1); switch (strType) { case "A": case "B": case "D": case "F": case "L": case "M": case "R": case "V": case "W": case "X": case "Y": // 2文字目以降は数値のはずなので変換する strAddress = s.Substring(1); break; case "Z": // もう1文字取り出す strType = s.Substring(0, 2); // ファイルレジスタの場合 : 2 // インデックスレジスタの場合 : 1 strAddress = s.Substring(strType.Equals("ZR") ? 2 : 1); break; case "C": // もう1文字取り出す strType = s.Substring(0, 2); switch (strType) { case "CC": case "CM": case "CN": case "CS": case "CT": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; case "S": // もう1文字取り出す strType = s.Substring(0, 2); switch (strType) { case "SD": case "SM": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; case "T": // もう1文字取り出す strType = s.Substring(0, 2); switch (strType) { case "TC": case "TM": case "TN": case "TS": case "TT": strAddress = s.Substring(2); break; default: throw new Exception("Invalid format."); } break; default: throw new Exception("Invalid format."); } oType = GetDeviceType(strType); oAddress = IsHexDevice(oType) ? Convert.ToInt32(strAddress, BlockSize) : Convert.ToInt32(strAddress); }
public static string ToAsciiName(this PlcDeviceType deviceType) { return(typeMapping[deviceType]); }