public static ARSResponseData SetFromByte(byte[] _arr) { ARSResponseData rst = new ARSResponseData(); try { string dataBady = Encoding.Default.GetString(_arr, 6, _arr.Length - 8); string[] datas = dataBady.Split('/'); int i = 0; rst.ARS_01 = datas[i++]; rst.ARS_02 = datas[i++]; rst.ARS_03 = datas[i++]; rst.ARS_04 = datas[i++]; rst.ARS_05 = datas[i++]; rst.ARS_06 = datas[i++]; rst.ARS_07 = datas[i++]; rst.ARS_08 = datas[i++]; rst.ARS_09 = datas[i++]; rst.ARS_10 = datas[i++]; } catch { Console.Write(string.Format("\n[{0}] ARSResponseData SetFromByte Exception", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); rst = null; } return(rst); }
public static byte[] GetToByte(ARSResponseData _data) { byte[] arr = null; try { byte[] soh = new byte[] { 0x01, 0x02 }; byte[] len = new byte[] { 0x30, 0x30, 0x30 }; byte cmd = (byte)'S'; byte[] b1 = Encoding.Default.GetBytes(string.Format("{0}", _data.ARS_01)); byte[] b2 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_02)); byte[] b3 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_03)); byte[] b4 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_04)); byte[] b5 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_05)); byte[] b6 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_06)); byte[] b7 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_07)); byte[] b8 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_08)); byte[] b9 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_09)); byte[] b10 = Encoding.Default.GetBytes(string.Format("/{0}", _data.ARS_10)); byte[] eoh = new byte[] { 0x03, 0x04 }; int dataLen = b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length; len = Encoding.Default.GetBytes(string.Format("{0:000}", dataLen)); arr = new byte[8 + dataLen]; Buffer.BlockCopy(soh, 0, arr, 0, soh.Length); Buffer.BlockCopy(len, 0, arr, soh.Length, len.Length); arr[soh.Length + len.Length] = cmd; Buffer.BlockCopy(b1, 0, arr, soh.Length + len.Length + 1, b1.Length); Buffer.BlockCopy(b2, 0, arr, soh.Length + len.Length + 1 + b1.Length, b2.Length); Buffer.BlockCopy(b3, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length, b3.Length); Buffer.BlockCopy(b4, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length, b4.Length); Buffer.BlockCopy(b5, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length, b5.Length); Buffer.BlockCopy(b6, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length, b6.Length); Buffer.BlockCopy(b7, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length, b7.Length); Buffer.BlockCopy(b8, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length, b8.Length); Buffer.BlockCopy(b9, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length, b9.Length); Buffer.BlockCopy(b10, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length, b10.Length); Buffer.BlockCopy(eoh, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length + b6.Length + b7.Length + b8.Length + b9.Length + b10.Length, eoh.Length); } catch (Exception ex) { Console.Write(string.Format("\n[{0}] ARSResponseData GetToByte Exception", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); arr = null; } return(arr); }