Пример #1
0
        public static byte[] Two(int iStationAddress, int iMark, int iCard, string strMessage)
        {
            try
            {
                byte[] bMessage = Encoding.GetEncoding("gb2312").GetBytes(strMessage);

                byte[] eBit;
                byte[] Fs = new byte[15 + bMessage.Length];
                Fs[0] = 10;
                for (int i = 1; i <= 5; i++)
                {
                    Fs[i] = 255;
                }
                Fs[6]  = (byte)iStationAddress;       // 基站地址号
                Fs[7]  = 19;                          // 命令号
                Fs[8]  = (byte)iMark;                 // 主备标志
                Fs[9]  = (byte)(3 + bMessage.Length); // 长度
                Fs[10] = (byte)0;                     // 控制
                if (iCard >= 0)
                {
                    if (iCard > 256)
                    {
                        Fs[11] = (byte)(iCard / 256);
                        Fs[12] = (byte)(iCard - 256);
                    }
                    else
                    {
                        Fs[11] = (byte)0;
                        Fs[12] = (byte)iCard;
                    }
                }
                else
                {
                    Fs[11] = (byte)0; // 卡号
                    Fs[12] = (byte)0; // 卡号
                }

                for (int i = 0; i < bMessage.Length; i++)
                {
                    Fs[13 + i] = bMessage[i];
                }
                eBit = CRCVerify.Crc(Fs, Fs.Length - 2, 1);
                Fs[Fs.Length - 2] = eBit[1]; //低位
                Fs[Fs.Length - 1] = eBit[0]; //高位
                return(Fs);
            }
            catch
            {
                return(null);
            }
        }
Пример #2
0
        public override void baseSocketClient_DataReceivedByAddress(byte[] bytes, string strAddress)
        {
            base.baseSocketClient_DataReceivedByAddress(bytes, strAddress);
            try
            {
                #region [ 缓冲区溢出 ]
                // 如果不存在数据
                if (iCurLoc == 0)
                {
                    iStartLoc    = -1;
                    iEndLoc      = -1;
                    iEndStartLoc = -1;
                    cmdNewBytes  = null;
                    if (timer.Enabled == false)
                    {
                        timer.Enabled = true;
                    }
                    return;
                }

                #endregion

                #region [ 寻找开始标记 ]

                // 寻找开始标记
                int i238;
                if (iStartLoc == -1)
                {
                    i238 = 0;
                    for (int i = 0; i < iCurLoc; i++)
                    {
                        if (byteBuffer[i] == 238)
                        {
                            i238++;
                        }
                        else
                        {
                            i238 = 0;
                        }

                        if (i238 >= 5)
                        {
                            iStartLoc    = i - 5;
                            iEndStartLoc = i + 1;
                            break;
                        }
                    }
                }

                if (iEndStartLoc == -1)
                {
                    return;
                }

                #endregion

                #region [获取分站信息]
                //分站地址号
                int        address  = (int)byteBuffer[iStartLoc + 6];
                MemStation mStation = new MemStation();
                if (address != 0)
                {
                    mStation = GetStation(address);
                }
                if (mStation.Address == 0)
                {
                    iCurLoc = 0;
                    for (int i = 0; i < byteBuffer.Length; i++)
                    {
                        byteBuffer[i] = (byte)0;
                    }
                    cmdNewBytes = null;
                    if (timer.Enabled == false)
                    {
                        timer.Enabled = true;
                    }
                    return;
                }
                #endregion


                switch (mStation.StationModel)
                {
                case 1:    //KJ128A
                    #region [ 寻找结束标记 ]

                    // 寻找结束标记
                    int i254;
                    if (iEndLoc == -1)
                    {
                        i254 = 0;
                        int iTmp = 0;
                        for (iTmp = iEndStartLoc; iTmp < iCurLoc; iTmp++)
                        {
                            if (byteBuffer[iTmp] == 254)
                            {
                                i254++;
                            }
                            else
                            {
                                i254 = 0;
                            }

                            if (i254 >= 3)
                            {
                                iEndLoc = iTmp;
                                break;
                            }
                        }

                        // 记录下一次开始寻找结束标志的位置
                        if (iEndLoc == -1)
                        {
                            iEndStartLoc = iTmp - 2;
                            return;
                        }
                    }

                    // Error: 写日志
                    if (iEndStartLoc == -1)
                    {
                        return;
                    }

                    #endregion

                    #region [ 检索重复开始标志 ]

                    // 检索开始标志后,结束标志前,是否还有第二个开始标志
                    i238 = 0;
                    for (int i = iEndLoc - 2; i > iStartLoc + 4; i--)
                    {
                        if (byteBuffer[i] == 238)
                        {
                            i238++;
                        }
                        else
                        {
                            i238 = 0;
                        }

                        if (i238 >= 5)
                        {
                            // 提取重复标志位的数据
                            int    iErrCmd = iEndLoc - iStartLoc + 1;
                            byte[] cmdErr  = new byte[iErrCmd];
                            for (int k = 0; k < iErrCmd; k++)
                            {
                                cmdErr[k] = byteBuffer[iStartLoc + k];
                            }

                            // 显示重复标志位的数据
                            //if (RTxtMsge != null)
                            //{
                            //    RTxtMsge.WriteTxt(cmdErr, " [标志重复]", true, 0);
                            //}

                            iStartLoc = i - 1;
                            break;
                        }
                    }

                    #endregion

                    #region [ 提取完整命令 ]

                    // 根据命令长度,分配命令存放的空间
                    int iCmdNewLength = iEndLoc - iStartLoc - 9;
                    if (iCmdNewLength < 1)
                    {
                        // Error:
                        string str = string.Empty;
                        return;
                    }
                    cmdNewBytes = new byte[iCmdNewLength];

                    // 提取完整命令
                    for (int i = 0; i < iCmdNewLength; i++)
                    {
                        cmdNewBytes[i] = byteBuffer[iStartLoc + i + 6];
                    }

                    iCurLoc = 0;
                    for (int i = 0; i < byteBuffer.Length; i++)
                    {
                        byteBuffer[i] = (byte)0;
                    }

                    //// 将后续命令前移
                    //if (iEndLoc + 1 < iCurLoc)
                    //{
                    //    iEndLoc++;

                    //    int iCmdTmpLength = iCurLoc - iEndLoc;
                    //    for (int i = 0; i <= iCmdTmpLength; i++)
                    //    {
                    //        byteBuffer[i] = byteBuffer[iEndLoc + i];
                    //    }

                    //    iCurLoc = iCmdTmpLength;
                    //}
                    //else
                    //{
                    //    iCurLoc = 0;
                    //}

                    #endregion
                    break;

                default:
                    #region [ 提取完整命令 ]
                    try
                    {
                        iCmdNewLength = 0;
                        if (byteBuffer.Length > 9)
                        {
                            switch (byteBuffer[iStartLoc + 7])
                            {
                            case 20:        //数据回送
                                iCmdNewLength = 6 + int.Parse(byteBuffer[iStartLoc + 8].ToString()) + int.Parse(byteBuffer[iStartLoc + 9].ToString()) * 256;
                                if (iCmdNewLength <= 6)
                                {
                                    cmdNewBytes = null;
                                    if (timer.Enabled == false)
                                    {
                                        timer.Enabled = true;
                                    }
                                    return;
                                }
                                if (byteBuffer.Length < iCmdNewLength + 6)
                                {
                                    iCmdNewLength = 0;
                                    for (int i = 0; i < byteBuffer.Length; i++)
                                    {
                                        byteBuffer[i] = (byte)0;
                                    }
                                }
                                break;

                            case 21:        //数据回送成功确认
                            case 23:        //对时成功确认
                            case 24:        //请求对时
                                iCmdNewLength = 4;
                                break;

                            default:
                                for (int i = 0; i < byteBuffer.Length; i++)
                                {
                                    byteBuffer[i] = (byte)0;
                                }
                                break;
                            }
                        }


                        // 根据命令长度,分配命令存放的空间
                        if (iCmdNewLength < 1)
                        {
                            // Error:
                            string str = string.Empty;
                            cmdNewBytes = null;
                            if (timer.Enabled == false)
                            {
                                timer.Enabled = true;
                            }
                            return;
                        }
                        cmdNewBytes = new byte[iCmdNewLength];
                        byte[] cmdBytesAll = new byte[iCmdNewLength + 5];
                        for (int i = 0; i < iCmdNewLength + 5; i++)
                        {
                            cmdBytesAll[i] = byteBuffer[iStartLoc + i + 1];
                        }
                        // 提取完整命令
                        for (int i = 0; i < iCmdNewLength; i++)
                        {
                            cmdNewBytes[i] = byteBuffer[iStartLoc + i + 6];
                        }

                        #region [分析效验位]
                        _crcByte = CRCVerify.Crc(cmdBytesAll, iCmdNewLength + 3, 0);
                        #endregion

                        if (_crcByte == null)
                        {
                            cmdNewBytes = null;
                            if (timer.Enabled == false)
                            {
                                timer.Enabled = true;
                            }
                            return;
                        }
                        if (cmdNewBytes[1] == 0)
                        {
                            cmdNewBytes = null;
                            if (timer.Enabled == false)
                            {
                                timer.Enabled = true;
                            }
                            return;
                        }
                        //if (cmdNewBytes[cmdNewBytes.Length - 1] == 0 && cmdNewBytes[cmdNewBytes.Length - 2] == 0)
                        //{
                        //    cmdNewBytes = null;
                        //    if (timer.Enabled == false)
                        //    {
                        //        timer.Enabled = true;
                        //    }
                        //    return;
                        //}
                        // 验证命令号是否正确cmdNewBytes[1] != CmdNO
                        if (cmdNewBytes[1] != mStation.SCmd && cmdNewBytes[1] != 24)
                        {
                            for (int i = 0; i < byteBuffer.Length; i++)
                            {
                                byteBuffer[i] = (byte)0;
                            }
                            cmdNewBytes = null;
                            if (timer.Enabled == false)
                            {
                                timer.Enabled = true;
                            }
                            return;
                        }
                        // Crc 校验
                        if (!_crcByte[0].Equals(cmdNewBytes[cmdNewBytes.Length - 1]) || !_crcByte[1].Equals(cmdNewBytes[cmdNewBytes.Length - 2]))
                        {
                            cmdNewBytes = null;
                            if (timer.Enabled == false)
                            {
                                timer.Enabled = true;
                            }
                            return;
                        }
                        iCurLoc = 0;
                        for (int i = 0; i < byteBuffer.Length; i++)
                        {
                            byteBuffer[i] = (byte)0;
                        }
                    }
                    catch
                    {
                        for (int i = 0; i < byteBuffer.Length; i++)
                        {
                            byteBuffer[i] = (byte)0;
                        }
                        cmdNewBytes = null;
                        if (timer.Enabled == false)
                        {
                            timer.Enabled = true;
                        }
                    }

                    #endregion
                    break;
                }
            }
            catch
            {
                cmdNewBytes = null;
                if (timer.Enabled == false)
                {
                    timer.Enabled = true;
                }
            }
        }