简体   繁体   English

从NamedPipeClientStream移位

[英]Shifting Bits from a NamedPipeClientStream

I am currently trying to write a bit synchronous serial protocol that reads data through a NamedPipeClientStream. 我目前正在尝试编写有点同步的串行协议,该协议通过NamedPipeClientStream读取数据。 This data then needs to be continuously monitored for sync characters (Hex 22) that may or may not be on the byte boundaries and shift the data accordingly to ensure that the other data being received is in Frame. 然后需要连续监视此数据的同步字符(十六进制22),同步字符可能在字节边界上,也可能不在字节边界上,并相应地移动数据以确保所接收的其他数据在帧中。

I have prototyped several scenarios including a bit array and using a BitStream object designed by a person who posted to this site all of which seem cumbersome and most likely very inefficient. 我已经原型化了几种方案,包括位数组和使用由发布到此站点的人设计的BitStream对象,所有这些似乎都很麻烦并且很可能效率很低。

The data being sent will continue to come in after the sync characters have been found so i must continue to add the incoming data to the end of the bit stream that was shifted. 找到同步字符后,将继续发送要发送的数据,因此我必须继续将传入的数据添加到已移位的位流的末尾。

Maybe something like this 也许像这样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader("filename");
            UInt16 chr = (UInt16)reader.Read();
            UInt16 word = 0;
            int shift = -1;

            while (shift == -1)
            {
                word = (UInt16)((UInt16)(word << 8) | chr);
                shift = TestSync(chr, word);
            }
            while ((chr = (UInt16)reader.Read()) >= 0)
            {
                word = (UInt16)((UInt16)(word << 8) | chr);
                byte newChar = (byte)((word >> shift) & 0xff);

            }


        }
        static int TestSync(UInt16 chr, UInt16 word)
        {
            int results = -1;


            if((UInt16)(word & 0xff) == 0x11) return 0;
            if((UInt16)(word & 0xff) == 0x22) return 1; 
            if((UInt16)(word & 0xff) == 0x44) return 2;
            if((UInt16)(word & 0xff) == 0x88) return 3;
            if((UInt16)(word & 0x1fe) == 0x110) return 4;
            if((UInt16)(word & 0x3fc) == 0x220) return 5;
            if((UInt16)(word & 0x7f8) == 0x440) return 6;
            if ((UInt16)(word & 0xff0) == 0x880) return 7;

            return results;
        }
    }
}
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM