Ejemplo n.º 1
0
        public SerialFrame GetFrame(int timeout)
        {
            SerialFrame sf = null;

            lock (((ICollection)queueFrame).SyncRoot)
            {
                if (queueFrame.Count > 0)
                {
                    sf = queueFrame.Dequeue();
                }
            }

            if (timeout == 0 || sf != null)
            {
                return(sf);
            }

            if (timeout < 0)
            {
                eventFrame.WaitOne();
            }
            else
            {
                eventFrame.WaitOne(timeout, false);
            }

            return(GetFrame(0));
        }
Ejemplo n.º 2
0
        public void PutFrame(SerialFrame sf)
        {
            if (sf == null) return;
            if (this.Cmd > 0 && this.Cmd != sf.Cmd) return;

            lock (((ICollection)queueFrame).SyncRoot)
            {

                if (queueFrame.Count > 100)
                {
                    queueFrame.Dequeue();
                }
                queueFrame.Enqueue(sf);
                eventFrame.Set();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 处理设备的请求命令
        /// </summary>
        private void ProcessRequest()
        {
            try
            {
                FrameMonitor monitor = AddMonitor();
                while (true)
                {
                    DeviceRequest[] requests = null;
                    lock (((ICollection)listRequest).SyncRoot)
                    {
                        requests = listRequest.ToArray();

                        listRequest.Clear();
                    }


                    if (requests == null || requests.Length <= 0)
                    {
                        eventRequest.WaitOne();
                        continue;
                    }

                    for (int i = 0; i < requests.Length; i++)
                    {
                        monitor.Cmd = requests[i].Cmd - 0x80;

                        SendSerialData(requests[i].Cmd, requests[i].Request);

                        monitor.Reset();


                        SerialFrame sf = monitor.GetFrame(500);
                        if (sf != null)
                        {
                            requests[i].AddResponse(sf.RawData);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 4
0
        public void PutFrame(SerialFrame sf)
        {
            if (sf == null)
            {
                return;
            }
            if (this.Cmd > 0 && this.Cmd != sf.Cmd)
            {
                return;
            }

            lock (((ICollection)queueFrame).SyncRoot)
            {
                if (queueFrame.Count > 100)
                {
                    queueFrame.Dequeue();
                }
                queueFrame.Enqueue(sf);
                eventFrame.Set();
            }
        }
Ejemplo n.º 5
0
        private void ReceiveData()
        {
            byte[] buffer = new byte[128 * 1024 * 2];

            byte[] frameBuffer = new byte[128 * 1024];

            int framenum0 = 1;

            int Lastframesequence0 = 0;

            int framenum1 = 1;

            int Lastframesequence1 = 0;

            int framenum2 = 1;

            int Lastframesequence2 = 0;

            int frameLength = 0;

            int leftLength = 0;

            int dataLength = 0;

            int realLength = 0;

            bool rollback = false;

            try
            {
                sp.ReadTimeout = 500;

                while (true)
                {

                    dataLength=0;

                    if (rollback) //前一个数据帧回滚
                    {
                        for (int i = 1; i < frameLength; i++)
                        {
                            if (frameBuffer[i] == 0xaa && ((i==frameLength-1) || (frameBuffer[i+1]==0xaa)))
                            {
                                dataLength = frameLength - i;
                                Array.Copy(frameBuffer, i, buffer, 0, dataLength);
                                break;
                            }
                        }
                        frameLength = 0;
                        rollback = false;
                    }

                    if (leftLength > 0) //前一个BUffer剩下的Data
                    {
                        Array.Copy(buffer, buffer.Length / 2, buffer, dataLength, leftLength);
                        dataLength += leftLength;
                        leftLength = 0;
                    }

                    if(dataLength<=0) //串口数据
                    {
                        try
                        {
                            dataLength = sp.Read(buffer, 0, buffer.Length / 2);
                        }
                        catch (TimeoutException)
                        {
                            rollback = true;
                            continue;
                        }

                    }

                    for (int i = 0; i < dataLength; i++)
                    {
                        frameBuffer[frameLength++] = buffer[i];
                        leftLength = dataLength - i - 1;
                        switch (frameLength)
                        {
                            case 1:
                            case 2:
                                if (frameBuffer[frameLength - 1] != 0xaa) //帧头
                                {
                                    rollback = true;
                                }
                                break;
                            case 3:
                                if (frameBuffer[2] != 1) //版本协议
                                {
                                    rollback = true;
                                }
                                break;
                            case 4:
                                break;
                            case 5:
                                {
                                     realLength = frameBuffer[3] + (frameBuffer[4] << 8);//数据长度
                                     if (realLength+9 > frameBuffer.Length || (realLength <= 0))
                                     {
                                         rollback = true;
                                     }
                                }
                                break;
                            default:
                                if (frameLength == realLength + 9) //收到完整数据
                                {
                                    if (frameBuffer[5] == 0x15)
                                    {
                                        //0通道帧序号的判断
                                        if (framenum0 == 2 && frameBuffer[6] == 0)
                                        {
                                            int Presentframesequence = BitConverter.ToInt32(frameBuffer, 7);
                                            if (Presentframesequence != Lastframesequence0 + 1)
                                            {
                                                rollback = true;
                                            }
                                            Lastframesequence0 = Presentframesequence;
                                        }
                                        else if (framenum0 == 1 && frameBuffer[6] == 0)
                                        {
                                            framenum0 = 2;
                                            Lastframesequence0 = BitConverter.ToInt32(frameBuffer, 7);
                                        }

                                        //1通道帧序号的判断
                                        if (framenum1 == 2 && frameBuffer[6] == 1)
                                        {
                                            int Presentframesequence = BitConverter.ToInt32(frameBuffer, 7);
                                            if (Presentframesequence != Lastframesequence1 + 1)
                                            {
                                                rollback = true;
                                            }
                                            Lastframesequence1 = Presentframesequence;
                                        }
                                        else if (framenum1 == 1 && frameBuffer[6] == 1)
                                        {
                                            framenum1 = 2;
                                            Lastframesequence1 = BitConverter.ToInt32(frameBuffer, 7);
                                        }

                                        //2通道帧序号的判断
                                        if (framenum2 == 2 && frameBuffer[6] == 2)
                                        {
                                            int Presentframesequence = BitConverter.ToInt32(frameBuffer, 7);
                                            if (Presentframesequence != Lastframesequence2 + 1)
                                            {
                                                rollback = true;
                                            }
                                            Lastframesequence2 = Presentframesequence;
                                        }
                                        else if (framenum0 == 1 && frameBuffer[6] == 2)
                                        {
                                            framenum2 = 2;
                                            Lastframesequence2 = BitConverter.ToInt32(frameBuffer, 7);
                                        }
                                    }

                                   UInt16 calSum= CalSUMCheck(frameBuffer, 5, realLength);

                                   UInt16 realSum = (UInt16)(frameBuffer[realLength + 5] + (frameBuffer[realLength + 6] << 8));
                                   if (calSum != realSum)
                                   {
                                       rollback = true;
                                   }
                                   else
                                   {

                                       //获取到完整的数据帧
                                       if (frameBuffer[5] == 0x15)
                                       {
                                           int channel = frameBuffer[6];
                                           byte gearvalue = (byte)(frameBuffer[12]  >> 4);
                                           if(gearvalue > 3)
                                           {
                                               rollback = true;
                                           }

                                           gearList[channel] = gearvalue;
                                           if (channel < 3)
                                           {
                                               int adLen = frameBuffer[3] + (frameBuffer[4] << 8) - 6;//数据长度

                                               List<Int16> listAd = new List<Int16>();

                                               for (int j = 0; j < adLen / 2; j++)
                                               {
                                                   Int16 adVal = (Int16)(frameBuffer[j * 2 + 11] + ((frameBuffer[j * 2 + 12] & 0x0F) << 8));
                                                   Int16 fAD = (Int16)listFiltes[channel].DoFilter(adVal);
                                                   listAd.Add(fAD);
                                               }
                                               Int16[] adArray=listAd.ToArray();
                                               listBlock[channel].PutAdData(adArray, 0, adArray.Length);

                                               lock (listChannelBlock)
                                               {
                                                   List<ADBlock> listBB = listChannelBlock[channel];
                                                   foreach (ADBlock block in listBB)
                                                   {
                                                       block.PutAdData(adArray, 0, adArray.Length);
                                                   }

                                               }

                                           }

                                       }
                                       SerialFrame sf = new SerialFrame(frameBuffer, frameLength);
                                       lock (((ICollection)listMonitor).SyncRoot)
                                       {
                                           foreach (FrameMonitor monitor in listMonitor)
                                           {
                                               monitor.PutFrame(sf);
                                           }
                                       }

                                       frameLength = 0;

                                   }
                                }
                                break;
                        }

                        if (rollback || frameLength>=frameBuffer.Length)
                        {
                            Array.Copy(buffer, i + 1, buffer, buffer.Length / 2, leftLength);
                            break;
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }
Ejemplo n.º 6
0
        private void ReceiveData()
        {
            byte[] buffer = new byte[128 * 1024 * 2];

            byte[] frameBuffer = new byte[128 * 1024];

            int framenum0 = 1;

            int Lastframesequence0 = 0;

            int framenum1 = 1;

            int Lastframesequence1 = 0;

            int framenum2 = 1;

            int Lastframesequence2 = 0;

            int frameLength = 0;

            int leftLength = 0;

            int dataLength = 0;

            int realLength = 0;

            bool rollback = false;

            try
            {
                sp.ReadTimeout = 500;

                while (true)
                {
                    dataLength = 0;

                    if (rollback) //前一个数据帧回滚
                    {
                        for (int i = 1; i < frameLength; i++)
                        {
                            if (frameBuffer[i] == 0xaa && ((i == frameLength - 1) || (frameBuffer[i + 1] == 0xaa)))
                            {
                                dataLength = frameLength - i;
                                Array.Copy(frameBuffer, i, buffer, 0, dataLength);
                                break;
                            }
                        }
                        frameLength = 0;
                        rollback    = false;
                    }

                    if (leftLength > 0) //前一个BUffer剩下的Data
                    {
                        Array.Copy(buffer, buffer.Length / 2, buffer, dataLength, leftLength);
                        dataLength += leftLength;
                        leftLength  = 0;
                    }

                    if (dataLength <= 0) //串口数据
                    {
                        try
                        {
                            dataLength = sp.Read(buffer, 0, buffer.Length / 2);
                        }
                        catch (TimeoutException)
                        {
                            rollback = true;
                            continue;
                        }
                    }

                    for (int i = 0; i < dataLength; i++)
                    {
                        frameBuffer[frameLength++] = buffer[i];
                        leftLength = dataLength - i - 1;
                        switch (frameLength)
                        {
                        case 1:
                        case 2:
                            if (frameBuffer[frameLength - 1] != 0xaa)     //帧头
                            {
                                rollback = true;
                            }
                            break;

                        case 3:
                            if (frameBuffer[2] != 1)     //版本协议
                            {
                                rollback = true;
                            }
                            break;

                        case 4:
                            break;

                        case 5:
                        {
                            realLength = frameBuffer[3] + (frameBuffer[4] << 8);         //数据长度
                            if (realLength + 9 > frameBuffer.Length || (realLength <= 0))
                            {
                                rollback = true;
                            }
                        }
                        break;

                        default:
                            if (frameLength == realLength + 9)     //收到完整数据
                            {
                                if (frameBuffer[5] == 0x15)
                                {
                                    //0通道帧序号的判断
                                    if (framenum0 == 2 && frameBuffer[6] == 0)
                                    {
                                        int Presentframesequence = BitConverter.ToInt32(frameBuffer, 7);
                                        if (Presentframesequence != Lastframesequence0 + 1)
                                        {
                                            rollback = true;
                                        }
                                        Lastframesequence0 = Presentframesequence;
                                    }
                                    else if (framenum0 == 1 && frameBuffer[6] == 0)
                                    {
                                        framenum0          = 2;
                                        Lastframesequence0 = BitConverter.ToInt32(frameBuffer, 7);
                                    }

                                    //1通道帧序号的判断
                                    if (framenum1 == 2 && frameBuffer[6] == 1)
                                    {
                                        int Presentframesequence = BitConverter.ToInt32(frameBuffer, 7);
                                        if (Presentframesequence != Lastframesequence1 + 1)
                                        {
                                            rollback = true;
                                        }
                                        Lastframesequence1 = Presentframesequence;
                                    }
                                    else if (framenum1 == 1 && frameBuffer[6] == 1)
                                    {
                                        framenum1          = 2;
                                        Lastframesequence1 = BitConverter.ToInt32(frameBuffer, 7);
                                    }

                                    //2通道帧序号的判断
                                    if (framenum2 == 2 && frameBuffer[6] == 2)
                                    {
                                        int Presentframesequence = BitConverter.ToInt32(frameBuffer, 7);
                                        if (Presentframesequence != Lastframesequence2 + 1)
                                        {
                                            rollback = true;
                                        }
                                        Lastframesequence2 = Presentframesequence;
                                    }
                                    else if (framenum0 == 1 && frameBuffer[6] == 2)
                                    {
                                        framenum2          = 2;
                                        Lastframesequence2 = BitConverter.ToInt32(frameBuffer, 7);
                                    }
                                }

                                UInt16 calSum = CalSUMCheck(frameBuffer, 5, realLength);

                                UInt16 realSum = (UInt16)(frameBuffer[realLength + 5] + (frameBuffer[realLength + 6] << 8));
                                if (calSum != realSum)
                                {
                                    rollback = true;
                                }
                                else
                                {
                                    //获取到完整的数据帧
                                    if (frameBuffer[5] == 0x15)
                                    {
                                        int  channel   = frameBuffer[6];
                                        byte gearvalue = (byte)(frameBuffer[12] >> 4);
                                        if (gearvalue > 3)
                                        {
                                            rollback = true;
                                        }

                                        gearList[channel] = gearvalue;
                                        if (channel < 3)
                                        {
                                            int adLen = frameBuffer[3] + (frameBuffer[4] << 8) - 6;   //数据长度

                                            List <Int16> listAd = new List <Int16>();

                                            for (int j = 0; j < adLen / 2; j++)
                                            {
                                                Int16 adVal = (Int16)(frameBuffer[j * 2 + 11] + ((frameBuffer[j * 2 + 12] & 0x0F) << 8));
                                                Int16 fAD   = (Int16)listFiltes[channel].DoFilter(adVal);
                                                listAd.Add(fAD);
                                            }
                                            Int16[] adArray = listAd.ToArray();
                                            listBlock[channel].PutAdData(adArray, 0, adArray.Length);



                                            lock (listChannelBlock)
                                            {
                                                List <ADBlock> listBB = listChannelBlock[channel];
                                                foreach (ADBlock block in listBB)
                                                {
                                                    block.PutAdData(adArray, 0, adArray.Length);
                                                }
                                            }
                                        }
                                    }
                                    SerialFrame sf = new SerialFrame(frameBuffer, frameLength);
                                    lock (((ICollection)listMonitor).SyncRoot)
                                    {
                                        foreach (FrameMonitor monitor in listMonitor)
                                        {
                                            monitor.PutFrame(sf);
                                        }
                                    }

                                    frameLength = 0;
                                }
                            }
                            break;
                        }

                        if (rollback || frameLength >= frameBuffer.Length)
                        {
                            Array.Copy(buffer, i + 1, buffer, buffer.Length / 2, leftLength);
                            break;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }