串口通訊中接收和發送轉義的算法

byte[] temp_Data = new byte[] { 0x7f, 0x01, 0x00, 0xFF, 0x02, 0x00, 0x0F, 0x5F, 0x7D, 0x5F, 0x5D,  0x00, 0x2A, 0x01, 0xEA, 0x01, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xEE, 0x7f };
            int by_RxLen;
            int by_RxLen_data = 0;                      數組

            int i = 0;
            by_RxLen = temp_Data[6];
            //=========================================移除數組的頭尾數據
            ArrayList list = new ArrayList(temp_Data);
            list.RemoveRange(temp_Data.Length - 1, 1);
            list.RemoveRange(0, 1);
            //======================================================================
            foreach (byte InByte in list)
            {
                by_RxLen_data++;
                if ((InByte == 0x5f && temp_Data[by_RxLen_data+1] == 0x5d))
                {
                    num_remove_flag[i] = by_RxLen_data;
                    i++;
                }
                if (InByte == 0x5f && temp_Data[by_RxLen_data + 1] == 0x7d)
                {
                    num_remove_flag[i] = by_RxLen_data;
                    list.Remove(num_remove_flag[i]);
                    temp_Data[by_RxLen_data] = 0x7f;
                    i++;                  
                }
            }
            //=================================================arraylist unit (0x7f) instead of (0x5d 0x7d) ==
            ArrayList list1 = new ArrayList(temp_Data);
            list1.RemoveRange(temp_Data.Length - 1, 1);
            list1.RemoveRange(0, 1);
           
            //================================================================================================
            for (int j = 0; j < i; j++)
            {
                list1.RemoveRange(num_remove_flag[j]  - j, 1);
            }
            //================================================================================================
            byte head_end_byte = 0x7f;
            list1.Insert(0, head_end_byte);
            list1.Insert(temp_Data.Length - i -1, head_end_byte);
            byte[] new_array = new byte[temp_Data.Length - i];
            for (int k = 0; k < temp_Data.Length - i; k++)
            {
                new_array[k] = (byte)list1[k];
            }rem

+it

+foreach

+List

private byte[] transferred_meaning_array_send(byte[] temp_Data)
        {
            //  byte[] temp_Data = new byte[] { 0x7f, 0x01, 0x00, 0xFF, 0x02, 0x00, 0x0F, 0x7f, 0x7f, 0x7f, 0x00, 0x7f, 0x2A, 0x01, 0xEA, 0x01, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xEE, 0x7f };
            int by_TxLen;
            int by_TxLen_data = 0;                //yonglai jishu tigong jisuanchu yao yichu shu danyuan de weizhi.
            int[] num_add_flag = new int[10];  //yao yichu de shuju zai shuzu zhong de zuobiao
            int i = 0;
            by_TxLen = temp_Data[6];
            //=========================================移除數組的頭尾數據
            ArrayList list= new ArrayList(temp_Data);
            list.RemoveRange(temp_Data.Length-1,1);
            list.RemoveRange(0, 1);
            //======================================================================
            foreach (byte InByte in list)     //遍歷數組中須要轉義的數 將index存到數組中
            {
                if ((InByte != 0x5f)&&(InByte!=0x7f))
                {
                    by_TxLen_data++;
                   
                }
                if ((by_TxLen_data<temp_Data.Length-2)&&(byte)list[by_TxLen_data] == 0x7f)
                {遍歷

                    num_add_flag[i] = by_TxLen_data;                
                    by_TxLen_data++;
                    i++;
                }
                if ((by_TxLen_data < temp_Data.Length - 2) && (byte)list[by_TxLen_data] == 0x5f)
                {
                    num_add_flag[i] = by_TxLen_data;
                    by_TxLen_data++;
                    i++;
                }
            }
            //===================================================向數組中插數據
            int num_add = 0;
            for (int j=0; j<i; j++)
            {
                ArrayList list2 = new ArrayList();
                if ((byte)list[num_add_flag[j]] == 0x5f)
                {
                    //(byte)list[num_add_flag[j]] = 0x5f;
                    byte addbyte    =   0x5d;
                    list.Insert(num_add_flag[j]+1, addbyte);
                    for (int shift = 0; shift < i; shift++)   //插入數據後將未插入的數作相應位移
                    {
                        num_add_flag[j + shift]++;
                    }
                    num_add++;
                  
                }
                if ((byte)list[num_add_flag[j]] == 0x7f)
                {
                    byte addbyte = 0x7d;
                    list.Insert(num_add_flag[j] + 1, addbyte);
                    for (int shift = 0; shift < i; shift++)     //插入數據後將未插入的數作相應位移
                    {
                        num_add_flag[j + shift]++;
                    }
                    
                }     
            }
            //======================================================================
            byte head_end_byte = 0x7f;
            list.Insert(0, head_end_byte);
            list.Insert(temp_Data.Length + i-1, head_end_byte);
            byte[] new_array = new byte[temp_Data.Length + i];
            for (int k = 0; k < temp_Data.Length + i; k++)
            {
                new_array[k] = (byte)list[k];
            }
            return new_array;
        }數據

相關文章
相關標籤/搜索