예제 #1
0
파일: TCDLE30.cs 프로젝트: ufjl0683/sshmc
        private TextPackage ReadText()
        {
            int Seq=0,Address=0,Len=0,LRC=0;//,HeadLRC=0;
            byte[] text=null;
            TextPackage textPackage = new TextPackage();
            Seq = stream.ReadByte();
            Address = stream.ReadByte() * 256;
            Address+= stream.ReadByte();
            Len = stream.ReadByte() * 256;
            Len += stream.ReadByte();
             //   HeadLRC = stream.ReadByte();
            Len -= 10;
            textPackage.Seq = Seq;
            textPackage.Address = Address;

            //if (HeadLRC != (((Address >> 8) & 0x00ff) ^ (Address & 0x00ff) ^ ((Len >> 8) & 0x00ff) ^ (Len & 0x00ff)))
            //{
            //  // textPackage.HasErrors = true;
            //    textPackage.SetErrBit(V2DLE.DLE_ERR_FRAME, true);
            //    textPackage.eErrorDescription += "Hearder LRC Error!\r\n";
            //    //Console.WriteLine("Hearder LRC Error!");
            //    return textPackage;
            //}
            //else
            //{

                text = new byte[Len];
                int rlen = 0;

                do
                {
                    rlen+=stream.Read(text,rlen, Len-rlen);

                } while (rlen != Len);
                //for (int i = 0; i < Len; i++)
                //    text[i] = (byte)stream.ReadByte();

                stream.ReadByte();   //READ DLE
                stream.ReadByte();    // READ ETX

                LRC= STX ^ Seq ^ETX ^ (Len+10) /256 ^ (Len+10)%256 ;
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bool isDLE=false;
                for (int i = 0; i < text.Length; i++)
                {
                    LRC ^= text[i];
                    if (text[i] != DLE)
                    {
                        ms.WriteByte(text[i]);
                        isDLE = false;
                    }
                    else  //double dle 處理
                    {
                        if (isDLE) //2nd dle
                            isDLE = false;
                        else  // 1st dle
                        {
                            isDLE = true;
                            ms.WriteByte(text[i]);
                        }
                    }

                }

                int tmp = stream.ReadByte();
                if (LRC !=tmp)// stream.ReadByte())
                {
                    textPackage.SetErrBit(TCDLE30.DLE_ERR_LCR, true);
                    textPackage.eErrorDescription += "LRC Error!\r\n";
                    Console.WriteLine("LRC Error!");
                    return textPackage;
                }
                else
                {
                   textPackage.Text=text;
                   textPackage.LRC = LRC;
                   return textPackage;

                }

              //  }
        }
예제 #2
0
파일: SirfDLE.cs 프로젝트: ufjl0683/sshmc
        private TextPackage ReadText()
        {
            int len,devid;
            TextPackage txt = new TextPackage();
               // devid = stream.ReadByte();
            len =  stream.ReadByte()*256+stream.ReadByte();
             txt.Text = new byte[len];
            int rlen = 0;
            txt.Address = 0; ; // devid;
            do
            {
                rlen += stream.Read(txt.Text, rlen, len - rlen);

            } while (rlen != len);
            int cks = 0;
            foreach (byte d in txt.Text)
                cks += d;

            cks &=0x7fff;
            int tempcks = stream.ReadByte() * 256 + stream.ReadByte();

            if (cks != tempcks/*stream.ReadByte()*256+stream.ReadByte()*/)
            {
                txt.SetErrBit(SirfDLE.DLE_ERR_LCR, true);
                txt.eErrorDescription += getDeviceName() + "LRC Error!\r\n";

            }

            return txt;

             //   throw new NotImplementedException();
        }
예제 #3
0
파일: V2DLE.cs 프로젝트: ufjl0683/sshmc
        private TextPackage ReadText()
        {
            int Seq=0,Address=0,Len=0,LRC=0,HeadLRC=0;
            byte[] text=null;
            TextPackage textPackage = new TextPackage();
            Seq = ReadByte(stream);
            Address = ReadByte(stream) * 256;
            Address+= ReadByte(stream);
            Len = ReadByte(stream) * 256;
            Len += ReadByte(stream);
            HeadLRC = ReadByte(stream);
            textPackage.Seq = Seq;
            textPackage.Address = Address;
              //  textPackage.LRC = HeadLRC;

            if (HeadLRC != (((Address >> 8) & 0x00ff) ^ (Address & 0x00ff) ^ ((Len >> 8) & 0x00ff) ^ (Len & 0x00ff)))
            {
              // textPackage.HasErrors = true;
                textPackage.SetErrBit(V2DLE.DLE_ERR_FRAME, true);
                textPackage.eErrorDescription += "Hearder LRC Error!\r\n";
                //Console.WriteLine("Hearder LRC Error!");
                return textPackage;
            }
            else
            {

                text = new byte[Len];
                int rlen = 0;

                do
                {
                    rlen+=stream.Read(text,rlen, Len-rlen);

                } while (rlen != Len);
                //for (int i = 0; i < Len; i++)
                //    text[i] = (byte)stream.ReadByte();

                //if (rlen != Len)
                //   Console.WriteLine("rLen={0}!=len={1}",rlen,Len);

                LRC=DLE ^ SOH ^ Seq ;
                for (int i = 0; i < text.Length; i++)
                    LRC ^= text[i];

                int tmp = ReadByte(stream);
                if (LRC !=tmp)// stream.ReadByte())
                {
                    textPackage.SetErrBit(V2DLE.DLE_ERR_LCR, true);
                    textPackage.eErrorDescription += "LRC Error!\r\n";
                    textPackage.Text = text;
                    Console.WriteLine("LRC Error!"+"==>"+ textPackage.ToString());
                    return textPackage;
                }
                else
                {
                   textPackage.Text=text;
                   textPackage.LRC = LRC;
                   return textPackage;

                }

            }
        }
예제 #4
0
파일: ETTUDLE.cs 프로젝트: ufjl0683/sshmc
        //----------------------Read CDU
        private TextPackage ReadText()
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            byte data;
            TextPackage ret = new TextPackage();

            while (true)
            {
                data =(byte) stream.ReadByte();
                ms.WriteByte(data);
                if (data == 0xF8 || data == 0xC7)
                    break;
            }

            byte[] databuff = ms.ToArray();

            uint LCR = 0;
            byte lcr1, lcr2;
            for (int i = 0; i < databuff.Length - 3; i++)  //cal lcr
                LCR += databuff[i];
            LCR =(uint)( (~LCR) + 1);  //2's complement
            lcr1 =(byte)( LCR & 0x0f);
            lcr2 =(byte)( (LCR >> 4) & 0x0f);
            ret.Text = new byte[databuff.Length - 3];

            ret.CCU_EndCode = databuff[databuff.Length - 1];

            System.Array.Copy(databuff, ret.Text, ret.Text.Length);
            if (!(lcr1 == databuff[databuff.Length - 2] && lcr2 == databuff[databuff.Length - 3]))
            {

                ret.SetErrBit((int)V2DLE.DLE_ERR_LCR,true);
                ret.eErrorDescription="LCR Error!";
                ret.LRC =(byte)( (lcr2 << 4) | lcr1);

            }
            else
            {
                ret.LRC = (byte)LCR;
            }

            if(this.OnReceiveText!=null)
            this.OnReceiveText(this, ret);
            return ret;
        }