/// <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); } } }
private void WriteOut_SendChunk(FanucChunk chunk) { byte[] temp = ASCIIEncoding.ASCII.GetBytes(chunk.Data); nsLock.Wait(); ns.Write(temp, 0, temp.Length); nsLock.Release(); }
/// <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(); }
/// <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; }
/// <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; } }
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(); }
/// <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"); }
public FanucChunk(FanucChunk fc) { this.id = fc.id + 1; this.persist = fc.PersistentExtra; this.state = FanucChunkState.UnProcessed; }
/// <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; } }
private void WriteOut_SendChunk(FanucChunk chunk) { byte[] temp = ASCIIEncoding.ASCII.GetBytes(chunk.Data); nsLock.Wait(); ns.Write(temp, 0, temp.Length); nsLock.Release(); }
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); }
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(); }
/// <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(); }
/// <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; }
/// <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"); }
public FanucChunk(FanucChunk fc) { this.id = fc.id + 1; this.persist = fc.PersistentExtra; this.state = FanucChunkState.UnProcessed; }
/// <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); } }