Ejemplo n.º 1
0
        /// <summary>
        /// 切割資料
        /// </summary>
        /// <param name="data">待切割資料</param>
        /// <param name="output">切割後的資料</param>
        /// <returns>資料狀態</returns>
        public static List<List<byte>> ParseData(List<byte> data)
        {
            if (data == null) return null;
            List<List<byte>> output = new List<List<byte>>();

            int pStx = 0, len, pEtx, totalLen;
            while ((pStx = data.IndexOf(RSTX, pStx)) != -1)
            {
                if (data.Count <= pStx + 1) { WriteLog.Console("Parse1", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return null; }
                len = data[pStx + 1];
                pEtx = pStx + len + 3;
                totalLen = len + 4;
                if (data.Count <= pEtx) { WriteLog.Console("Parse2", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return null; }
                if (data[pEtx] != RETX) { WriteLog.Console("Parse3", DataStatue.SETXFail.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); /*pStx += 1*/data.RemoveAt(0); continue; }
                if (data[pEtx - 1] != MakeCrc(data, pStx, totalLen - 2)) { WriteLog.Console("Parse4", DataStatue.CrcFail.ToString() + " " + ByteConverter.ToHexString(data)); pStx += 1; continue; }
                output.Add(data.GetRange(pStx, totalLen));
                if (pStx != 0)
                {
                    data.RemoveRange(0, pStx);
                    WriteLog.Console("Parse5", DataStatue.DataFail.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " "));
                    pStx = 0;
                }

                data.RemoveRange(pStx, totalLen);
            }
            return output;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 將byte清單送出
        /// </summary>
        /// <param name="data">要送出的資料</param>
        /// <returns>發送狀態</returns>
        public Communicate SendData(List <byte> data)
        {
            if (data.Count == 0)
            {
                return(Communicate.NoData);
            }
            if (m_serialPort == null)
            {
                return(Communicate.UartNull);
            }
            if (!m_serialPort.IsOpen)
            {
                return(Communicate.UartNotOpen);
            }

            ClearBuffer();

            WriteLog.Console("    Send    ", BitConverter.ToString(data.ToArray()).Replace("-", " "));
            try
            {
                m_serialPort.Write(data.ToArray(), 0, data.Count);
            }
            catch { }

            return(Communicate.Success);
        }
Ejemplo n.º 3
0
        public static byte[] AES128Encrypt(byte[] data, byte[] key, byte[] iv)
        {
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = key;
                aesAlg.IV  = iv;

                // Create a encrytor to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(data, 0, data.Length);

                        byte[] b = new byte[data.Length];
                        Array.Copy(msEncrypt.ToArray(), b, data.Length);
                        WriteLog.Console("    Data    ", data);
                        return(b);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 開啟指定的連接埠
        /// </summary>
        /// <param name="com_port">指定的連接埠</param>
        /// <param name="baud_rate">鮑率</param>
        /// <returns>UART狀態</returns>
        public Statue Open()
        {
            try
            {
                if (null == m_serialPort)
                {
                    return(Statue.Null);
                }

                if (m_serialPort.IsOpen)
                {
                    m_serialPort.Close();
                }

                if (m_serialPort.IsOpen)
                {
                    return(Statue.Denied);
                }

                m_serialPort.Open();

                if (!m_serialPort.IsOpen)
                {
                    return(Statue.CantOpen);
                }

                ClearBuffer();
                return(Statue.Success);
            }
            catch (Exception ex)
            {
                WriteLog.Console("Open Com Exp", ex.Message);
            }
            finally
            {
            }

            return(Statue.Exception);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 關閉目前開啟的連接埠
        /// </summary>
        /// <returns>UART狀態</returns>
        public Statue Close()
        {
            try
            {
                if (m_serialPort.IsOpen)
                {
                    m_serialPort.Close();
                }

                if (m_serialPort.IsOpen)
                {
                    return(Statue.Denied);
                }

                return(Statue.Success);
            }
            catch (Exception ex)
            {
                WriteLog.Console("Close Com Exp", ex.Message);
            }
            return(Statue.Exception);
        }
        /// <summary>
        /// 切割資料
        /// </summary>
        /// <param name="data">待切割資料</param>
        /// <param name="output">切割後的資料</param>
        /// <returns>資料狀態</returns>
        public static List <List <byte> > ParseData(List <byte> data)
        {
            if (data == null)
            {
                return(null);
            }
            List <List <byte> > output = new List <List <byte> >();

            int pStx, len, pEtx, totalLen;

            while ((pStx = data.IndexOf(RSTX)) != -1)
            {
                if (data.Count <= pStx + 1)
                {
                    WriteLog.Console("Parse1", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return(null);
                }
                len      = data[pStx + 1];
                pEtx     = pStx + len + 2;
                totalLen = len + 3;
                if (data.Count <= pEtx)
                {
                    WriteLog.Console("Parse2", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return(null);
                }
                if (data[pEtx] != RETX)
                {
                    WriteLog.Console("Parse3", DataStatue.SETXFail.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); data.RemoveRange(0, pStx + 1); continue;
                }

                //scWriteLog.Console("Parse4", data.GetRange(pStx, totalLen));
                output.Add(data.GetRange(pStx, totalLen));
                if (pStx != 0)
                {
                    WriteLog.Console("Parse5", DataStatue.DataFail.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " "));
                }

                data.RemoveRange(pStx, totalLen);
            }
            return(output);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 關閉目前開啟的連接埠
        /// </summary>
        /// <returns>UART狀態</returns>
        public Statue Close()
        {
            try
            {
                if (m_serialPort.IsOpen)
                {
                    KeepReceiveFlag = false;
                    System.Threading.Thread.Sleep(50);
                    m_serialPort.Close();
                }

                if (m_serialPort.IsOpen)
                {
                    return(Statue.Denied);
                }

                return(Statue.Success);
            }
            catch (Exception ex)
            {
                WriteLog.Console("Close Com Exp", ex.Message);
            }
            return(Statue.Exception);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 切割資料
        /// </summary>
        /// <param name="data">待切割資料</param>
        /// <param name="output">切割後的資料</param>
        /// <returns>資料狀態</returns>
        public static List <List <byte> > ParseData(List <byte> data)
        {
            if (data == null)
            {
                return(null);
            }
            List <List <byte> > output = new List <List <byte> >();

            int pStx, pEtx, totalLen;

            while ((pStx = data.IndexOf(RSTX)) != -1)
            {
                totalLen = 0;
                if (data.Count <= pStx + 3)
                {
                    WriteLog.Console("Parse1", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return(null);
                }
                pEtx = pStx + 2;
                while ((pEtx = data.IndexOf(RETX, pEtx + 1)) != -1)
                {
                    totalLen = pEtx - pStx + 1;
                    if (data[pEtx - 1] != MakeCrcInRange(data, pStx, totalLen - 2))
                    {
                        WriteLog.Console("CRC Fail", data.GetRange(pStx, totalLen));
                        data.RemoveRange(0, pStx + 1);
                    }
                    else
                    {
                        break;
                    }
                }

                if (totalLen == 0 || totalLen > 30 || pEtx == -1)
                {
                    continue;
                }
                output.Add(data.GetRange(pStx, totalLen));

                data.RemoveRange(pStx, totalLen);
            }

            while ((pStx = data.IndexOf(KRSTX)) != -1)
            {
                totalLen = 0;
                if (data.Count <= pStx + 3)
                {
                    WriteLog.Console("Parse1", DataStatue.LengthNotEnough.ToString() + " " + BitConverter.ToString(data.ToArray()).Replace("-", " ")); return(null);
                }
                pEtx = pStx + 2;
                while ((pEtx = data.IndexOf(RETX, pEtx + 1)) != -1)
                {
                    totalLen = pEtx - pStx + 1;
                    if (data[pEtx - 1] != MakeCrcInRange(data, pStx, totalLen - 2))
                    {
                        WriteLog.Console("CRC Fail", data.GetRange(pStx, totalLen));
                        data.RemoveRange(0, pStx + 1);
                    }
                    else
                    {
                        break;
                    }
                }

                if (totalLen == 0 || totalLen > 30 || pEtx == -1)
                {
                    continue;
                }
                output.Add(data.GetRange(pStx, totalLen));

                data.RemoveRange(pStx, totalLen);
            }
            return(output);
        }