/// <summary> /// Convert Dec string to UInt16Array. /// </summary> /// <param name="str">dec string. For example: "12333 12334" or "23333,12331"</param> /// <returns></returns> public static UInt16Array FromString(string str) { str = str.Trim(); UInt16Array Arr = new UInt16Array(); if (str != string.Empty) { string[] s = str.Split(new char[] { ',', ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < s.Length; i++) { try { if (s[i] != string.Empty) { Arr.Add(Convert.ToUInt16(s[i], 10)); } } catch { } } } return(Arr); }
/// <summary> /// Convert Hex string to UInt16Array. /// </summary> /// <param name="str">Hex string. For example: "EEFFEEFF" or "EEFF EEFF" or "EEFF,EEFF"</param> /// <returns></returns> public static UInt16Array FromHexString(string str) { str = str.Trim(); UInt16Array Arr = new UInt16Array(); if (str != string.Empty) { string[] s = Util.SplitString(str, new char[] { ',', ' ', '\t', '\n', '\r' }, 4); for (int i = 0; i < s.Length; i++) { try { if (s[i] != string.Empty) { Arr.Add(Convert.ToUInt16(s[i], 16)); } } catch { } } } return(Arr); }
public static object ParseArrayTypeFromString(string rawval, string type, string format) { string str = rawval.Trim(); try { switch (type) { case "u1v": case "u96": switch (format) { case "Hex": return((object)LLRPBitArray.FromHexString(str)); default: return((object)LLRPBitArray.FromString(str)); } case "bytesToEnd": case "u8v": switch (format) { case "Hex": return((object)ByteArray.FromHexString(str)); default: return((object)ByteArray.FromString(str)); } case "s8v": switch (format) { case "Hex": return((object)SignedByteArray.FromHexString(str)); default: return((object)SignedByteArray.FromString(str)); } case "u16v": switch (format) { case "Hex": return((object)UInt16Array.FromHexString(str)); default: return((object)UInt16Array.FromString(str)); } case "s16v": switch (format) { case "Hex": return((object)Int16Array.FromHexString(str)); default: return((object)Int16Array.FromString(str)); } case "u32v": switch (format) { case "Hex": return((object)UInt32Array.FromHexString(str)); default: return((object)UInt32Array.FromString(str)); } case "s32v": switch (format) { case "Hex": return((object)Int32Array.FromHexString(str)); default: return((object)Int32Array.FromString(str)); } case "utf8v": return((object)str); default: throw new Exception(string.Format("{0} is unsupported type.", (object)type)); } } catch { throw new Exception(string.Format("Can't parse {0} to {1} as format {2}", (object)str, (object)type, (object)format)); } }
/// <summary> /// Convert bit array to object based on position, type of object and length of the bits /// </summary> /// <param name="bit_array">the input bit array</param> /// <param name="cursor">the start position</param> /// <param name="obj">output value</param> /// <param name="type">conversion type</param> /// <param name="field_len">field length, if it's 0, the field length will be determined by type</param> public static void ConvertBitArrayToObj(ref BitArray bit_array, ref int cursor, out Object obj, Type type, int field_len) { if (type.Equals(typeof(bool))) { obj = bit_array[cursor]; cursor++; } else if (type.Equals(typeof(byte))) { obj = (byte)(UInt64)CalculateVal(ref bit_array, ref cursor, 8); } else if (type.Equals(typeof(sbyte))) { obj = (sbyte)(UInt64)CalculateVal(ref bit_array, ref cursor, 8); } else if (type.Equals(typeof(UInt16))) { obj = (UInt16)(UInt64)CalculateVal(ref bit_array, ref cursor, 16); } else if (type.Equals(typeof(Int16))) { obj = (Int16)(UInt64)CalculateVal(ref bit_array, ref cursor, 16); } else if (type.Equals(typeof(UInt32))) { obj = (UInt32)(UInt64)CalculateVal(ref bit_array, ref cursor, field_len); } else if (type.Equals(typeof(Int32))) { obj = (Int32)(UInt64)CalculateVal(ref bit_array, ref cursor, 32); } else if (type.Equals(typeof(UInt64))) { obj = (UInt64)(UInt64)CalculateVal(ref bit_array, ref cursor, 64); } else if (type.Equals(typeof(Int64))) { obj = (Int64)(UInt64)CalculateVal(ref bit_array, ref cursor, 64); } else if (type.Equals(typeof(string))) { string s = string.Empty; for (int i = 0; i < field_len; i++) { try { byte bd = (byte)(UInt64)CalculateVal(ref bit_array, ref cursor, 8); System.Text.UTF8Encoding encoding = new UTF8Encoding(); byte[] bdarr = new byte[1] { bd }; s += encoding.GetString(bdarr); } catch { } } if (field_len > 1 && s[field_len - 1] == 0) { s = s.Substring(0, field_len - 1); } obj = s; } else if (type.Equals(typeof(ByteArray))) { obj = new ByteArray(); for (int i = 0; i < field_len; i++) { ((ByteArray)obj).Add((byte)(UInt64)CalculateVal(ref bit_array, ref cursor, 8)); } } else if (type.Equals(typeof(UInt16Array))) { obj = new UInt16Array(); for (int i = 0; i < field_len; i++) { ((UInt16Array)obj).Add((UInt16)(UInt64)CalculateVal(ref bit_array, ref cursor, 16)); } } else if (type.Equals(typeof(UInt32Array))) { obj = new UInt32Array(); for (int i = 0; i < field_len; i++) { ((UInt32Array)obj).Add((UInt32)(UInt64)CalculateVal(ref bit_array, ref cursor, 32)); } } else if (type.Equals(typeof(TwoBits))) { obj = new TwoBits(bit_array[cursor], bit_array[cursor + 1]); cursor += 2; } else if (type.Equals(typeof(BitArray))) { obj = new BitArray(field_len); for (int i = 0; i < field_len; i++) { ((BitArray)obj)[i] = bit_array[cursor]; cursor++; } } else if (type.Equals(typeof(LLRPBitArray))) { obj = new LLRPBitArray(); int mod = field_len % 8; int total_len = (mod > 0) ? (field_len + 8 - mod) : field_len; for (int i = 0; i < total_len; i++) { if (i < field_len) { ((LLRPBitArray)obj).Add(bit_array[cursor]); } cursor++; } } else { obj = (UInt32)(UInt64)CalculateVal(ref bit_array, ref cursor, field_len); } }
/// <summary> /// Parse array type supported by LLRP protocol /// </summary> /// <param name="val">input string value</param> /// <param name="type">array type (string), defined in LLRP protocol</param> /// <param name="format">format (string), defined in LLRP protocol</param> /// <returns></returns> public static object ParseArrayTypeFromString(string val, string type, string format) { try { switch (type) { case "u1v": case "u96": switch (format) { case "Hex": return(LLRPBitArray.FromHexString(val)); case "Dec": default: return(LLRPBitArray.FromString(val)); } break; case "bytesToEnd": case "u8v": switch (format) { case "Hex": return(ByteArray.FromHexString(val)); case "Dec": default: return(ByteArray.FromString(val)); } break; case "u16v": switch (format) { case "Hex": return(UInt16Array.FromHexString(val)); case "Dec": default: return(UInt16Array.FromString(val)); } break; case "u32v": switch (format) { case "Hex": return(UInt32Array.FromHexString(val)); case "Dec": default: return(UInt32Array.FromString(val)); } break; case "utf8v": return(val); break; default: throw new Exception(string.Format("{0} is unsupported type.", type)); } } catch { throw new Exception(string.Format("Can't parse {0} to {1} as format {2}", val, type, format)); } }
/// <summary> /// Create a OpSpec that Write a block of data into Gen2 memory /// </summary> /// <param name="tagOperation"> Tag operation</param> /// <returns>PARAM_C1G2BlockWrite</returns> private PARAM_C1G2BlockWrite BuildBlockWriteTagOpSpec(Gen2.BlockWrite tagOperation) { PARAM_C1G2BlockWrite c1g2BlockWrite = new PARAM_C1G2BlockWrite(); int dataLength = 0; //AccessPassword c1g2BlockWrite.AccessPassword = ((Gen2.Password)(ParamGet("/reader/gen2/accessPassword"))).Value; //Memory Bank c1g2BlockWrite.MB = new LTKD.TwoBits((ushort)(tagOperation.Bank)); //Data to be written dataLength = tagOperation.Data.Length; LTKD.UInt16Array data = new LTKD.UInt16Array(); List<ushort> dataWrite = new List<ushort>(); for (int i = 0; i < dataLength; i++) { dataWrite.Add(tagOperation.Data[i]); } data.data = dataWrite; c1g2BlockWrite.WriteData = LTKD.UInt16Array.FromString(data.ToString()); //WordPointer c1g2BlockWrite.WordPointer = (ushort)tagOperation.WordPtr; // Set the OpSpecID to a unique number. c1g2BlockWrite.OpSpecID = ++OpSpecID; return c1g2BlockWrite; }