/// <summary> /// Begin a task reading data from the Fanuc Device /// </summary> public void WriteOut(string data, FanucSocketOptions options) { if (SafeToProceed()) { ns.Flush(); // Removes extra data that is stuck in the socket cancelSource = new CancellationTokenSource(); opCancelToken = cancelSource.Token; // Main read task operation = new Task( cancellationToken: cancelSource.Token, creationOptions: TaskCreationOptions.LongRunning, action: () => { this.WriteOut_Main(data, options); } ); // If sending completes fully, fire event and dispose operation.ContinueWith( continuationOptions: TaskContinuationOptions.ExecuteSynchronously, continuationAction: (t) => { if (t.IsCompleted && !t.IsCanceled) { if (SendOperationCompleted != null) { SendOperationCompleted.Invoke(this, null); } } else if (t.IsFaulted) { if (ExceptionOccurred != null) { ExceptionOccurred.Invoke(this, t.Exception); } } cancelSource.Dispose(); cancelSource = null; operation.Dispose(); operation = null; ChangeState(FanucSocketState.Idle); } ); operation.Start(); } else { throw new FanucException("Operation in progress!"); } }
/// <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(); }
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); }
/// <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; } }
/// <summary> /// Santizes data before sending it out. Needs some work /// </summary> /// <param name="data">The data string</param> static private void WriteOut_Sanitize(ref string data, FanucSocketOptions options) { //Remove control codes left over in old files data = Regex.Replace(data, @"[\x11-\x14]", ""); if (!options.HasFlag(FanucSocketOptions.NoStartBlock)) { // Trim junk to the first program block Match m = Regex.Match(data, @"[:oO][\d]{4}"); if (m != null) { data = data.Substring(m.Index); data = "%\n" + data; } else { throw new FanucException("No sub-programs found!"); } } if (!options.HasFlag(FanucSocketOptions.NoEndBlock)) { Match m = Regex.Match(data, "%", RegexOptions.RightToLeft); //If it found a % (it better i JUST put one there) if (m.Index != 0) { data = data.Substring(0, m.Index + 1); } else { //Fix this, its a 2AM hack. data += "%"; // Append one to the end } } }
private void WriteOut_Main(string data, FanucSocketOptions options) { FanucChunk[] chunks; opCancelToken.ThrowIfCancellationRequested(); WriteOut_Prepare(out chunks, ref data, options); bool OkayToSend = false; int lastCode = 0; int cId = 0; ChangeState(FanucSocketState.Waiting); while (cId < chunks.Length) { lastCode = WriteOut_GetCode(); if ((lastCode == 0x11) && !OkayToSend) { OkayToSend = true; //Got DC1, Cleared to send ChangeState(FanucSocketState.Sending); } else if ((lastCode == 0x13) && OkayToSend) { OkayToSend = false; //Got DC3, Pause until its cleared ChangeState(FanucSocketState.Waiting); } if (OkayToSend) { WriteOut_SendChunk(chunks[cId]); cId++; if (ChunkSent != null) { ChunkSent.Invoke(this, Math.Round((cId / (double)chunks.Length) * 100, 1)); } } //opCancelToken.ThrowIfCancellationRequested(); Thread.Sleep(SendDelay); opCancelToken.ThrowIfCancellationRequested(); } bool isWaiting = true; int tempTimer = 0; while (isWaiting) { lastCode = WriteOut_GetCode(); if ((lastCode == 0x13) || tempTimer >= 30) { isWaiting = false; } tempTimer++; Thread.Sleep(1000); opCancelToken.ThrowIfCancellationRequested(); } opCancelToken.ThrowIfCancellationRequested(); }
private void WriteOut_Main(string data, FanucSocketOptions options) { FanucChunk[] chunks; opCancelToken.ThrowIfCancellationRequested(); WriteOut_Prepare(out chunks, ref data, options); bool OkayToSend = false; int lastCode = 0; int cId = 0; ChangeState(FanucSocketState.Waiting); while(cId < chunks.Length) { lastCode = WriteOut_GetCode(); if ((lastCode == 0x11) && !OkayToSend) { OkayToSend = true; //Got DC1, Cleared to send ChangeState(FanucSocketState.Sending); } else if ((lastCode == 0x13) && OkayToSend) { OkayToSend = false; //Got DC3, Pause until its cleared ChangeState(FanucSocketState.Waiting); } if (OkayToSend) { WriteOut_SendChunk(chunks[cId]); cId++; if (ChunkSent != null) ChunkSent.Invoke(this, Math.Round((cId / (double)chunks.Length) * 100, 1)); } //opCancelToken.ThrowIfCancellationRequested(); Thread.Sleep(SendDelay); opCancelToken.ThrowIfCancellationRequested(); } bool isWaiting = true; int tempTimer = 0; while (isWaiting) { lastCode = WriteOut_GetCode(); if ((lastCode == 0x13) || tempTimer >= 30) { isWaiting = false; } tempTimer++; Thread.Sleep(1000); opCancelToken.ThrowIfCancellationRequested(); } opCancelToken.ThrowIfCancellationRequested(); }
/// <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> /// Santizes data before sending it out. Needs some work /// </summary> /// <param name="data">The data string</param> private static void WriteOut_Sanitize(ref string data, FanucSocketOptions options) { //Remove control codes left over in old files data = Regex.Replace(data, @"[\x11-\x14]", ""); if (!options.HasFlag(FanucSocketOptions.NoStartBlock)) { // Trim junk to the first program block Match m = Regex.Match(data, @"[:oO][\d]{4}"); if (m != null) { data = data.Substring(m.Index); data = "%\n" + data; } else { throw new FanucException("No sub-programs found!"); } } if (!options.HasFlag(FanucSocketOptions.NoEndBlock)) { Match m = Regex.Match(data, "%", RegexOptions.RightToLeft); //If it found a % (it better i JUST put one there) if (m.Index != 0) { data = data.Substring(0, m.Index + 1); } else { //Fix this, its a 2AM hack. data += "%"; // Append one to the end } } }
/// <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; } }
/// <summary> /// Begin a task reading data from the Fanuc Device /// </summary> public void WriteOut(string data, FanucSocketOptions options) { if (SafeToProceed()) { ns.Flush(); // Removes extra data that is stuck in the socket cancelSource = new CancellationTokenSource(); opCancelToken = cancelSource.Token; // Main read task operation = new Task( cancellationToken: cancelSource.Token, creationOptions: TaskCreationOptions.LongRunning, action: () => { this.WriteOut_Main(data, options); } ); // If sending completes fully, fire event and dispose operation.ContinueWith( continuationOptions: TaskContinuationOptions.ExecuteSynchronously, continuationAction: (t) => { if (t.IsCompleted && !t.IsCanceled) { if (SendOperationCompleted != null) SendOperationCompleted.Invoke(this, null); } else if (t.IsFaulted) { if (ExceptionOccurred != null) ExceptionOccurred.Invoke(this, t.Exception); } cancelSource.Dispose(); cancelSource = null; operation.Dispose(); operation = null; ChangeState(FanucSocketState.Idle); } ); operation.Start(); } else throw new FanucException("Operation in progress!"); }