串口通信中接收和发送转义的算法

原文链接:https://my.oschina.net/u/724135/blog/283139

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];
            }

+

+

+

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;
        }

 

 

 

 

 

转载于:https://my.oschina.net/u/724135/blog/283139

展开阅读全文
博主设置当前文章不允许评论。

没有更多推荐了,返回首页