private void DataReceived(IAsyncResult ar) { UdpClient c = (UdpClient)ar.AsyncState; IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Byte[] receivedBytes = c.EndReceive(ar, ref receivedIpEndPoint); // Convert data to ASCII and print in console string receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); //SetText(logForm.txtResponse.Text + receivedText); //logForm.txtResponse.SelectionStart = logForm.txtResponse.Text.Length; if (gcodeCmd == "M27") //progress report { string[] lines = receivedText.Split('\n'); lines = lines.Where(x => !string.IsNullOrEmpty(x)).ToArray(); double[] progress = new double[2]; if ((lines[0].Substring(0, 5).ToUpper() != "ERROR") && (lines[0].Substring(0, 2).ToUpper() != "OK")) { isPrinting = true; lines = lines[0].Split(new[] { "byte" }, StringSplitOptions.None); lines = lines[1].Split('/'); progress[0] = double.Parse(lines[0].Trim()); progress[1] = double.Parse(lines[1].Trim()); //SetText(logForm.txtResponse.Text + progressCount.ToString()+"\r\n"); if (progress[0] > progressBefore && progressCount >= 1) { progressCount = 0; int PrintProgress = (int)Math.Round((progress[0] * 100) / progress[1]); SetProgress(PrintProgress); SetLbl("Percent done : " + PrintProgress.ToString()); progressBefore = progress[0]; SetText(logForm.txtResponse.Text + "Frame = " + framenumber.ToString() + ", "); if (timelapse) { FrameSnapped = false; if (framenumber >= skipFirst) { if ((framenumber - skipFirst) % takeEvery == 0) { gcodeCmd = "M25"; receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length); receivedBytes = receiver.Receive(ref ipEndPoint); receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); gcodeCmd = "M114"; //receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length); //receivedBytes = receiver.Receive(ref ipEndPoint); //receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); FrameSnapped = true; } //else //{ // if(PrintProgress>=100) //It's not the nth frame but the print finished. take the last frame // { // gcodeCmd = "M25"; // receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length); // receivedBytes = receiver.Receive(ref ipEndPoint); // receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); // gcodeCmd = "M114"; // } //} } } framenumber++; } else if (progress[0] > progressBefore && progressCount < 1) { progressCount++; } //txtResponse.SelectionStart = txtResponse.Text.Length; //txtResponse.ScrollToCaret(); //txtResponse.Refresh(); } else if (lines[0].Substring(0, 5).ToUpper() == "ERROR") { if (isPrinting) //means it was printing and now it finsihed { SetProgress(100); SetLbl("Percent done : 100"); if (timelapse) { if (!FrameSnapped) //The lasr frame was not snapped coz it was not the nth frame { gcodeCmd = "M114"; //receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length); //receivedBytes = receiver.Receive(ref ipEndPoint); //receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); FrameSnapped = true; } timelapse = false; //stop time lapse } } isPrinting = false; } } else if (gcodeCmd == "M114") //Get current Z value untill it settles { if (receivedText.IndexOf("X:0.000000") != -1) { string[] lines = receivedText.Split(new[] { "Z:" }, StringSplitOptions.None); lines = lines[1].Split(new[] { "E:" }, StringSplitOptions.None); double zpos = double.Parse(lines[0]); SetZheight("Z Height = " + Math.Round(zpos, 3).ToString()); if (zposbefore != zpos) { zposbefore = zpos; } else { double Zoffset = MaxLiftHeight - zpos; gcodeCmd = "G91, G0 Z" + Zoffset.ToString() + " F300"; receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length); receivedBytes = receiver.Receive(ref ipEndPoint); receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); prevzoffset = Zoffset; zposbefore = 0; snapped = false; snapComplete = false; gcodeCmd = "M4000"; } } } else if (gcodeCmd == "M4000") //Get current Z value untill it settles { if (receivedText.IndexOf("X:0.000") != -1) { string[] lines = receivedText.Split(new[] { "Z:" }, StringSplitOptions.None); lines = lines[1].Split(new[] { "F:" }, StringSplitOptions.None); double zpos = double.Parse(lines[0]); SetZheight("Z Height = " + Math.Round(zpos, 3).ToString()); if (zposbefore != zpos) { zposbefore = zpos; } else { if (!snapped) { if (!FirstSnap) { SetText(logForm.txtResponse.Text + "Time Lapse Starts\r\n"); MessageBox.Show("Setup the Camera for time Lapse and click OK."); FirstSnap = true; } snapComplete = false; _actionSource.FireEvent(ActionEvent.Command.TAKE_PICTURE, IntPtr.Zero); SetText(logForm.txtResponse.Text + "Take Picture\r\n"); snapped = true; } if (snapComplete) { gcodeCmd = "G91, G0 Z" + (-1 * prevzoffset).ToString() + " F300";; receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length); receivedBytes = receiver.Receive(ref ipEndPoint); receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); if (isPrinting) //if the printing it going on then resume print { gcodeCmd = "M24"; receiver.Send(Encoding.ASCII.GetBytes(gcodeCmd), gcodeCmd.Length); receivedBytes = receiver.Receive(ref ipEndPoint); receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes); } zposbefore = 0; snapComplete = false; snapped = false; gcodeCmd = "M27"; } } } } // Restart listening for udp data packages c.BeginReceive(DataReceived, ar.AsyncState); }