using HslCommunication.BasicFramework; using HslCommunication.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HslCommunication.Profinet.Keyence { /// /// 基恩士Nano串口Bytes数据转换规则 /// ///以数据格式“位”读取R100~R103时, []表示空格,发送指令如下: ///ACSII码: R D S [] R 1 0 0 [] 4 /r ///16进制码:0x52,0x44,0x53,0x20,0x52,0x31,0x30,0x30,0x20,0x34,0x0d ///响应如下 ///ACSII码: 1 [] 0 [] 1 [] 0 /r /n ///16进制码:0x31,0x20,0x30,0x20,0x31,0x20,0x30,0x0d,0x0a public class KeyenceNanoByteTransform : IByteTransform { #region Constructor /// /// 实例化一个默认的对象 /// public KeyenceNanoByteTransform() { } /// /// 数据格式 /// public DataFormat DataFormat { get; set; } #endregion #region Get Value From Bytes /// /// Nano响应的Bytes转换为string数组 /// /// 缓存数据 /// 字符串数组 private string[] BytesToStringArray(byte[] buffer) { string strBuffer = Encoding.Default.GetString(buffer); return strBuffer.Split(' '); } /// /// 从缓存中提取出bool结果 /// /// 缓存数据 /// 位的索引 /// bool对象 public virtual bool TransBool(byte[] buffer, int index) { return BytesToStringArray(buffer)[index] == "1" ? true : false; } /// /// 从缓存中提取出bool数组结果 /// /// 缓存数据 /// 位的索引 /// bool长度 /// bool数组 public bool[] TransBool(byte[] buffer, int index, int length) { bool[] result = new bool[length]; for (int i = 0; i < length; i++) { result[i] = BytesToStringArray(buffer)[index + i] == "1" ? true : false; } return result; } /// /// 从缓存中提取byte结果 /// /// 缓存数据 /// 索引位置 /// byte对象 public virtual byte TransByte(byte[] buffer, int index) { return Convert.ToByte(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取byte数组结果 /// /// 缓存数据 /// 索引位置 /// 读取的数组长度 /// byte数组对象 public virtual byte[] TransByte(byte[] buffer, int index, int length) { byte[] result = new byte[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToByte(BytesToStringArray(buffer)[index + i]); } return result; } /// /// 从缓存中提取short结果 /// /// 缓存数据 /// 索引位置 /// short对象 public virtual short TransInt16(byte[] buffer, int index) { return Convert.ToInt16(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取short数组结果 /// /// 缓存数据 /// 索引位置 /// 读取的数组长度 /// short数组对象 public virtual short[] TransInt16(byte[] buffer, int index, int length) { short[] result = new short[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToInt16(BytesToStringArray(buffer)[i + index]); } return result; } /// /// 从缓存中提取ushort结果 /// /// 缓存数据 /// 索引位置 /// ushort对象 public virtual ushort TransUInt16(byte[] buffer, int index) { return Convert.ToUInt16(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取ushort数组结果 /// /// 缓存数据 /// 索引位置 /// 读取的数组长度 /// ushort数组对象 public virtual ushort[] TransUInt16(byte[] buffer, int index, int length) { ushort[] result = new ushort[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToUInt16(BytesToStringArray(buffer)[i + index]); } return result; } /// /// 从缓存中提取int结果 /// /// 缓存数据 /// 索引位置 /// int对象 public virtual int TransInt32(byte[] buffer, int index) { return Convert.ToInt32(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取int数组结果 /// /// 缓存数据 /// 索引位置 /// 读取的数组长度 /// int数组对象 public virtual int[] TransInt32(byte[] buffer, int index, int length) { int[] result = new int[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToInt32(BytesToStringArray(buffer)[i + index]); } return result; } /// /// 从缓存中提取uint结果 /// /// 缓存数据 /// 索引位置 /// uint对象 public virtual uint TransUInt32(byte[] buffer, int index) { return Convert.ToUInt32(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取uint数组结果 /// /// 缓存数据 /// 索引位置 /// 读取的数组长度 /// uint数组对象 public virtual uint[] TransUInt32(byte[] buffer, int index, int length) { uint[] result = new uint[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToUInt32(BytesToStringArray(buffer)[i + index]); } return result; } /// /// 从缓存中提取long结果 /// /// 缓存数据 /// 索引位置 /// long对象 public virtual long TransInt64(byte[] buffer, int index) { return Convert.ToInt64(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取long数组结果 /// /// 缓存数据 /// 索引位置 /// 读取的数组长度 /// long数组对象 public virtual long[] TransInt64(byte[] buffer, int index, int length) { long[] result = new long[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToInt64(BytesToStringArray(buffer)[i + index]); } return result; } /// /// 从缓存中提取ulong结果 /// /// 缓存数据 /// 索引位置 /// ulong对象 public virtual ulong TransUInt64(byte[] buffer, int index) { return Convert.ToUInt64(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取ulong数组结果 /// /// 缓存数据 /// 索引位置 /// 读取的数组长度 /// ulong数组对象 public virtual ulong[] TransUInt64(byte[] buffer, int index, int length) { ulong[] result = new ulong[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToUInt64(BytesToStringArray(buffer)[i + index]); } return result; } /// /// 从缓存中提取float结果 /// /// 缓存对象 /// 索引位置 /// float对象 public virtual float TransSingle(byte[] buffer, int index) { return Convert.ToSingle(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取float数组结果 /// /// 缓存数据 /// 索引位置 /// 读取的数组长度 /// float数组对象 public virtual float[] TransSingle(byte[] buffer, int index, int length) { float[] result = new float[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToSingle(BytesToStringArray(buffer)[i + index]); } return result; } /// /// 从缓存中提取double结果 /// /// 缓存对象 /// 索引位置 /// double对象 public virtual double TransDouble(byte[] buffer, int index) { return Convert.ToDouble(BytesToStringArray(buffer)[index]); } /// /// 从缓存中提取double数组结果 /// /// 缓存对象 /// 索引位置 /// 读取的数组长度 /// double数组对象 public virtual double[] TransDouble(byte[] buffer, int index, int length) { double[] result = new double[length]; for (int i = 0; i < length; i++) { result[i] = Convert.ToDouble(BytesToStringArray(buffer)[i + index]); } return result; } /// /// 从缓存中提取string结果,使用指定的编码 /// /// 缓存对象 /// 索引位置 /// byte数组长度 /// 字符串的编码 /// string对象 public virtual string TransString(byte[] buffer, int index, int length, Encoding encoding) { byte[] tmp = TransByte(buffer, index, length); return encoding.GetString(tmp); } #endregion #region Get Bytes From Value /// /// bool变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(bool value) { return TransByte(new bool[] { value }); } /// /// bool数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(bool[] values) { StringBuilder stringBuilder = new StringBuilder(); string strLength = values.Length.ToString(); for (int i = 0; i < values.Length; i++) { stringBuilder.Append(" "); stringBuilder.Append(values[i] ? "1" : "0"); } return Encoding.ASCII.GetBytes(stringBuilder.ToString()); } /// /// byte变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(byte value) { return new byte[] { value }; } /// /// short变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(short value) { return TransByte(new short[] { value }); } /// /// short数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(short[] values) { return Trans.ToBytes(values, ".S"); } /// /// ushort变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(ushort value) { return TransByte(new ushort[] { value }); } /// /// ushort数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(ushort[] values) { return Trans.ToBytes(values, ".U"); } /// /// int变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(int value) { return TransByte(new int[] { value }); } /// /// int数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(int[] values) { return Trans.ToBytes(values, ".L"); } /// /// uint变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(uint value) { return TransByte(new uint[] { value }); } /// /// uint数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(uint[] values) { return Trans.ToBytes(values, ".D"); } /// /// long变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(long value) { return TransByte(new long[] { value }); } /// /// long数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(long[] values) { return Trans.ToBytes(values, ".L"); } /// /// ulong变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(ulong value) { return TransByte(new ulong[] { value }); } /// /// ulong数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(ulong[] values) { return Trans.ToBytes(values, ".L"); } /// /// float变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(float value) { return TransByte(new float[] { value }); } /// /// float数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(float[] values) { return Trans.ToBytes(values, ".F"); } /// /// double变量转化缓存数据 /// /// 等待转化的数据 /// buffer数据 public virtual byte[] TransByte(double value) { return TransByte(new double[] { value }); } /// /// double数组变量转化缓存数据 /// /// 等待转化的数组 /// buffer数据 public virtual byte[] TransByte(double[] values) { return Trans.ToBytes(values,".DF"); } /// /// 使用指定的编码字符串转化缓存数据 /// /// 等待转化的数据 /// 字符串的编码方式 /// buffer数据 public virtual byte[] TransByte(string value, Encoding encoding) { if (value == null) return null; return encoding.GetBytes(value); } /// /// 字节转换类 /// /// private class Trans where T : struct { /// /// 泛型对象转换为字节数组 /// /// /// /// public static byte[] ToBytes(T[] values,string dataFormat) { if (values == null) return null; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(dataFormat); stringBuilder.Append(" "); stringBuilder.Append(values.Length.ToString()); for (int i = 0; i < values.Length; i++) { stringBuilder.Append(" "); if (typeof(T)==typeof(int) ) { var value = Convert.ToInt32(values[i]); stringBuilder.Append(value.ToString()); } else if (typeof(T)== typeof(uint)) { var value = Convert.ToUInt32(values[i]); stringBuilder.Append(value.ToString()); } else if (typeof(T) == typeof(short)) { var value = Convert.ToInt16(values[i]); stringBuilder.Append(value.ToString()); } else if (typeof(T) == typeof(ushort)) { var value = Convert.ToUInt16(values[i]); stringBuilder.Append(value.ToString()); } else if (typeof(T) == typeof(long)) { var value = Convert.ToInt64(values[i]); stringBuilder.Append(value.ToString()); } else if (typeof(T) == typeof(ulong)) { var value = Convert.ToUInt64(values[i]); stringBuilder.Append(value.ToString()); } else if (typeof(T) == typeof(float)) { var value = Convert.ToSingle(values[i]); stringBuilder.Append(value.ToString()); } else if (typeof(T) == typeof(double)) { var value = Convert.ToDouble(values[i]); stringBuilder.Append(value.ToString()); } else { return null; } } return Encoding.ASCII.GetBytes(stringBuilder.ToString()); } } #endregion } }