Exemplo n.º 1
0
        /// <summary>
        /// Locks access to the socket and does a read operation. If data has arrived, it is checked for Fanuc
        ///  control codes and saved to memory.
        /// </summary>
        /// <returns>A FanucChunk with the complete data and status</returns>
        private void ReadIn_Main(FanucSocketOptions options)
        {
            FanucChunk chunk;

            opCancelToken.ThrowIfCancellationRequested();

            chunk = new FanucChunk(0);

            ChangeState(FanucSocketState.Receiving);

            while (!opCancelToken.IsCancellationRequested)
            {
                if (ns.DataAvailable)
                {
                    ReadIn_GetData(ref chunk);

                    if (chunk.State == FanucChunkState.ReadyForProcessing)
                    {
                        ReadIn_ParseData(ref chunk, options);
                    }

                    if (chunk.State == FanucChunkState.Processed)
                    {
                        if (ChunkReceived != null)
                        {
                            ChunkReceived.Invoke(this, chunk.Data);
                        }
                    }
                    else if (chunk.State == FanucChunkState.Error)
                    {
                        if (BadChunkReceived != null)
                        {
                            BadChunkReceived.Invoke(this, chunk.Data);
                        }
                    }

                    if (!options.HasFlag(FanucSocketOptions.IgnoreControlCodes))
                    {
                        if ((int)chunk.PersistentExtra == 0x14)
                        {
                            return;
                        }
                    }

                    chunk = new FanucChunk(chunk);
                }
                else
                {
                    Thread.Sleep(500);
                }

                opCancelToken.ThrowIfCancellationRequested();
            }

            opCancelToken.ThrowIfCancellationRequested();
        }
 protected virtual void OnChunkReceived(Chunk c)
 {
     ChunkReceived?.Invoke(this, new FramerEventArgs {
         Chunk = c
     });
 }