Beispiel #1
0
        /// <summary>
        /// Crops the data in the DC2-DC4 bounds
        /// </summary>
        /// <param name="chunk">The chunk to process</param>
        static private void ReadIn_ClipBlock(ref FanucChunk chunk)
        {
            Regex           datacodes = new Regex(@"[\x12\x14]");
            MatchCollection matches   = datacodes.Matches(chunk.Data);

            if (matches.Count == 0)                       // No control codes found
            {
                if ((int)chunk.PersistentExtra != 0x12)   // The last persistant code was not a DC2
                {
                    chunk.State = FanucChunkState.Ignore; // Ignore this chunk, it isn't in a block
                }
            }
            else
            {
                int cutstart  = 0;
                int cutlength = chunk.Data.Length;

                foreach (Match m in matches)
                {
                    int code = (int)chunk.Data[(m.Index)];
                    if (code == 0x12)                           //DC2 - Enter Block
                    {
                        if ((int)chunk.PersistentExtra == 0x00) // We have no prev control code, this is correct
                        {
                            cutstart              = m.Index + 1;
                            cutlength            -= cutstart;
                            chunk.PersistentExtra = 0x12;
                        }
                        else // Something is broken with this chunk, error this chunk
                        {
                            chunk.State = FanucChunkState.Error;
                        }
                    }
                    else if (code == 0x14)
                    {
                        if ((int)chunk.PersistentExtra == 0x12) //We are in a block
                        {
                            cutlength             = m.Index - cutstart;
                            chunk.PersistentExtra = 0x14;
                        }
                        else // Either we missed something or something is broken, error this chunk
                        {
                            chunk.State = FanucChunkState.Error;
                        }
                    }
                    else
                    {
                        throw new FanucException("ReadIn_ClipBlock: An unknown code was parsed. It was ASCII code "
                                                 + Convert.ToString((int)chunk.Data[(m.Index)]) + ".");
                    }
                }

                // We have found our endpoints, clip the data as long as its clean
                if (chunk.State != FanucChunkState.Error)
                {
                    chunk.Data = chunk.Data.Substring(cutstart, cutlength);
                }
            }
        }
Beispiel #2
0
        private void WriteOut_SendChunk(FanucChunk chunk)
        {
            byte[] temp = ASCIIEncoding.ASCII.GetBytes(chunk.Data);

            nsLock.Wait();
            ns.Write(temp, 0, temp.Length);
            nsLock.Release();
        }
Beispiel #3
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();
        }
Beispiel #4
0
        /// <summary>
        /// Gets data from socket and puts it in the chunk
        /// </summary>
        /// <param name="chunk">The chunk to work with</param>
        private void ReadIn_GetData(ref FanucChunk chunk)
        {
            byte[] readBuffer = new byte[BufferSize];

            nsLock.Wait(opCancelToken);
            int iRX = ns.Read(readBuffer, 0, BufferSize);

            nsLock.Release();

            chunk.Data = Encoding.ASCII.GetString(readBuffer, 0, iRX);

            chunk.State = FanucChunkState.ReadyForProcessing;
        }
Beispiel #5
0
        /// <summary>
        /// Parses the data and keeps a state
        /// </summary>
        /// <param name="chunk">The chunk to process</param>
        /// <param name="options">Processing options</param>
        static private void ReadIn_ParseData(ref FanucChunk chunk, FanucSocketOptions options)
        {
            if (!options.HasFlag(FanucSocketOptions.RawMode))
            {
                if (!options.HasFlag(FanucSocketOptions.IgnoreControlCodes))
                {
                    ReadIn_ClipBlock(ref chunk);
                }

                if (options.HasFlag(FanucSocketOptions.FixNewline) && chunk.State != FanucChunkState.Error)
                {
                    ReadIn_ToCRLF(ref chunk);
                }
            }
            if (chunk.State == FanucChunkState.ReadyForProcessing)
            {
                chunk.State = FanucChunkState.Processed;
            }
        }
Beispiel #6
0
        private void WriteOut_ChunkData(out FanucChunk[] chunks, string data)
        {
            List <FanucChunk> chunkTemp = new List <FanucChunk>();

            while (data.Length > 0)
            {
                FanucChunk fc     = new FanucChunk(chunkTemp.Count);
                int        length = (BufferSize <= data.Length) ? BufferSize : data.Length;

                fc.Data  = data.Substring(0, length);
                fc.State = FanucChunkState.ReadyForProcessing;

                chunkTemp.Add(fc);

                data = data.Remove(0, length);
            }

            chunks = chunkTemp.ToArray();

            chunkTemp.Clear();
        }
Beispiel #7
0
 /// <summary>
 /// Converts LF to CR+LF
 /// </summary>
 /// <param name="chunk">The chunk to process</param>
 static private void ReadIn_ToCRLF(ref FanucChunk chunk)
 {
     chunk.Data = Regex.Replace(chunk.Data, "(?<!\r)\n\n", "\r\n");
     chunk.Data = Regex.Replace(chunk.Data, "(?<!\r)\n", "\r\n");
 }
Beispiel #8
0
 public FanucChunk(FanucChunk fc)
 {
     this.id = fc.id + 1;
     this.persist = fc.PersistentExtra;
     this.state = FanucChunkState.UnProcessed;
 }
Beispiel #9
0
        /// <summary>
        /// Parses the data and keeps a state
        /// </summary>
        /// <param name="chunk">The chunk to process</param>
        /// <param name="options">Processing options</param>
        private static void ReadIn_ParseData(ref FanucChunk chunk, FanucSocketOptions options)
        {
            if (!options.HasFlag(FanucSocketOptions.RawMode))
            {
                if (!options.HasFlag(FanucSocketOptions.IgnoreControlCodes))
                {
                    ReadIn_ClipBlock(ref chunk);
                }

                if (options.HasFlag(FanucSocketOptions.FixNewline) && chunk.State != FanucChunkState.Error)
                {
                    ReadIn_ToCRLF(ref chunk);
                }
            }
            if (chunk.State == FanucChunkState.ReadyForProcessing)
            {
                chunk.State = FanucChunkState.Processed;
            }
        }
Beispiel #10
0
        private void WriteOut_SendChunk(FanucChunk chunk)
        {
            byte[] temp = ASCIIEncoding.ASCII.GetBytes(chunk.Data);

            nsLock.Wait();
            ns.Write(temp, 0, temp.Length);
            nsLock.Release();
        }
Beispiel #11
0
        private void WriteOut_Prepare(out FanucChunk[] chunks, ref string data, FanucSocketOptions options)
        {
            if (!options.HasFlag(FanucSocketOptions.RawMode))
            {
                WriteOut_ToLF(ref data); // Correct newlines
                WriteOut_Sanitize(ref data, options); // Sanitize data
            }

            WriteOut_ChunkData(out chunks, data);
        }
Beispiel #12
0
        private void WriteOut_ChunkData(out FanucChunk[] chunks, string data)
        {
            List<FanucChunk> chunkTemp = new List<FanucChunk>();

            while (data.Length > 0)
            {
                FanucChunk fc = new FanucChunk(chunkTemp.Count);
                int length = (BufferSize <= data.Length) ? BufferSize : data.Length;

                fc.Data = data.Substring(0, length);
                fc.State = FanucChunkState.ReadyForProcessing;

                chunkTemp.Add(fc);

                data = data.Remove(0, length);
            }

            chunks = chunkTemp.ToArray();

            chunkTemp.Clear();
        }
Beispiel #13
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();
        }
Beispiel #14
0
        /// <summary>
        /// Gets data from socket and puts it in the chunk
        /// </summary>
        /// <param name="chunk">The chunk to work with</param>
        private void ReadIn_GetData(ref FanucChunk chunk)
        {
            byte[] readBuffer = new byte[BufferSize];

            nsLock.Wait(opCancelToken);
            int iRX = ns.Read(readBuffer, 0, BufferSize);
            nsLock.Release();

            chunk.Data = Encoding.ASCII.GetString(readBuffer, 0, iRX);

            chunk.State = FanucChunkState.ReadyForProcessing;
        }
Beispiel #15
0
 /// <summary>
 /// Converts LF to CR+LF
 /// </summary>
 /// <param name="chunk">The chunk to process</param>
 private static void ReadIn_ToCRLF(ref FanucChunk chunk)
 {
     chunk.Data = Regex.Replace(chunk.Data, "(?<!\r)\n\n", "\r\n");
     chunk.Data = Regex.Replace(chunk.Data, "(?<!\r)\n", "\r\n");
 }
Beispiel #16
0
 public FanucChunk(FanucChunk fc)
 {
     this.id      = fc.id + 1;
     this.persist = fc.PersistentExtra;
     this.state   = FanucChunkState.UnProcessed;
 }
Beispiel #17
0
        /// <summary>
        /// Crops the data in the DC2-DC4 bounds
        /// </summary>
        /// <param name="chunk">The chunk to process</param>
        private static void ReadIn_ClipBlock(ref FanucChunk chunk)
        {
            Regex datacodes = new Regex(@"[\x12\x14]");
            MatchCollection matches = datacodes.Matches(chunk.Data);

            if (matches.Count == 0) // No control codes found
            {
                if ((int)chunk.PersistentExtra != 0x12) // The last persistant code was not a DC2
                {
                    chunk.State = FanucChunkState.Ignore; // Ignore this chunk, it isn't in a block
                }
            }
            else
            {
                int cutstart = 0;
                int cutlength = chunk.Data.Length;

                foreach (Match m in matches)
                {
                    int code = (int)chunk.Data[(m.Index)];
                    if (code == 0x12)  //DC2 - Enter Block
                    {
                        if ((int)chunk.PersistentExtra == 0x00) // We have no prev control code, this is correct
                        {
                            cutstart = m.Index + 1;
                            cutlength -= cutstart;
                            chunk.PersistentExtra = 0x12;
                        }
                        else // Something is broken with this chunk, error this chunk
                        {
                            chunk.State = FanucChunkState.Error;
                        }
                    }
                    else if (code == 0x14)
                    {
                        if ((int)chunk.PersistentExtra == 0x12) //We are in a block
                        {
                            cutlength = m.Index - cutstart;
                            chunk.PersistentExtra = 0x14;
                        }
                        else // Either we missed something or something is broken, error this chunk
                        {
                            chunk.State = FanucChunkState.Error;
                        }
                    }
                    else
                    {
                        throw new FanucException("ReadIn_ClipBlock: An unknown code was parsed. It was ASCII code "
                            + Convert.ToString((int)chunk.Data[(m.Index)]) + ".");
                    }
                }

                // We have found our endpoints, clip the data as long as its clean
                if (chunk.State != FanucChunkState.Error) chunk.Data = chunk.Data.Substring(cutstart, cutlength);
            }
        }