Exemple #1
0
        public static BattResponseData SetFromByte(byte[] _arr)
        {
            BattResponseData rst = new BattResponseData();

            try
            {
                string   dataBady = Encoding.Default.GetString(_arr, 6, _arr.Length - 8);
                string[] datas    = dataBady.Split('/');

                int i = 0;

                rst.State      = byte.Parse(datas[i++]);
                rst.PSU_1_Volt = float.Parse(datas[i++]);
                rst.PSU_2_Volt = float.Parse(datas[i++]);
                rst.PSU_1_Per  = int.Parse(datas[i++]);
                rst.PSU_2_Per  = int.Parse(datas[i++]);
            }
            catch
            {
                Console.Write(string.Format("\n[{0}] BattResponseData SetFromByte Exception", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                rst = null;
            }

            return(rst);
        }
Exemple #2
0
        public static byte[] GetToByte(BattResponseData _data)
        {
            byte[] arr = null;

            try
            {
                byte[] soh = new byte[] { 0x01, 0x02 };
                byte[] len = new byte[] { 0x30, 0x30, 0x30 };
                byte   cmd = (byte)'B';
                byte[] b1  = Encoding.Default.GetBytes(string.Format("{0}", _data.State));
                byte[] b2  = Encoding.Default.GetBytes(string.Format("/{0}", _data.PSU_1_Volt));
                byte[] b3  = Encoding.Default.GetBytes(string.Format("/{0}", _data.PSU_2_Volt));
                byte[] b4  = Encoding.Default.GetBytes(string.Format("/{0}", _data.PSU_1_Per));
                byte[] b5  = Encoding.Default.GetBytes(string.Format("/{0}", _data.PSU_2_Per));
                byte[] eoh = new byte[] { 0x03, 0x04 };

                int dataLen = b1.Length + b2.Length + b3.Length + b4.Length + b5.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(eoh, 0, arr, soh.Length + len.Length + 1 + b1.Length + b2.Length + b3.Length + b4.Length + b5.Length, eoh.Length);
            }
            catch (Exception ex)
            {
                Console.Write(string.Format("\n[{0}] BattResponseData GetToByte Exception", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                arr = null;
            }

            return(arr);
        }