using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; // add this for save function and Stream Reader function using System.IO.Ports; // add this for use serial port function namespace WindowsFormsApplication14 { public partial class Form1 : Form { private SerialPort myport; //Delegating function serial port delegate void SetTextCallback(string text);// i forget for this one :D private string in_data; // Delegating data from arduino // string line; // delegating for some funtion to read txt file public Form1() { InitializeComponent(); inisial(); // For delegating enable stop button label3.Visible = false; // this for delegating speed text result not visible in the first time } // This one for detected Serial port available in your pc private void Form1_Load(object sender, EventArgs e) { string[] portNames = System.IO.Ports.SerialPort.GetPortNames(); for (int i = 0; i <= portNames.Length - 1; i++) { portName_cbox.Items.Add(portNames[i]); } } // This one is for connecting software interface with the arduino private void start_btn_Click(object sender, EventArgs e) { myport = new SerialPort(); myport.BaudRate = 115200; //Depend on baud rate have been set up on Arduino sketch myport.PortName = portName_cbox.Text; myport.Parity = Parity.None; myport.DataBits = 8; myport.StopBits = StopBits.One; myport.DataReceived += myport_DataReceived; // this one is to received arduino serial print try { myport.Open(); data_tb.Text = "\n"; // Showing serial print from arduino with line by line } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } } // This function is for delegating read incoming data from arduino void myport_DataReceived(object sender, SerialDataReceivedEventArgs e) { in_data = myport.ReadLine(); this.Invoke(new EventHandler(displaydata_event)); } // This for showing data to Display textbox private void displaydata_event(object sender, EventArgs e) { data_tb.AppendText(in_data + "\n"); // Showing all data line by line string[] newData = in_data.Split('|'); // Function for read separate data counter_lbl.Text = newData[0]; // Showing data line 0 is time counter } private void inisial () { stop_btn.Enabled = true; } // This function is the core private void stop_btn_Click(object sender, EventArgs e) { try { label3.Visible = true; myport.WriteLine("1"); // Sending command Stop count to arduino stop_btn.Enabled = true; string pathfile = @"C:\Users\Dell\Desktop\golap\"; // Where is folder want to keep the txt save file string filename = "textgo.txt"; // This is the name of txt file string line; System.IO.File.WriteAllText(pathfile + filename, data_tb.Text); // Take all data into txt file MessageBox.Show("Let Show Result"); // This is to give function read and separate data System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Dell\Desktop\golap\textgo.txt"); while ((line = file.ReadLine()) != null) { if (line.Contains("Lap : 1")) { { textBox1.Text = line; } if (line.Contains("Lap : 2")) { textBox2.Text = line; } if (line.Contains("Lap : 3")) { textBox3.Text = line; } if (line.Contains("km/h :")) { textBox5.Text = line; string[] newLine = line.Split('|'); textBox5.Text = newLine[1]; } } } } catch (Exception ex3) { MessageBox.Show(ex3.Message, "Error"); } } private void reset_btn_Click(object sender, EventArgs e) { try { myport.WriteLine("0"); // Send command to reset arduino label3.Visible = false; } catch (Exception ex4) { MessageBox.Show(ex4.Message, "Error"); } } private void data_tb_TextChanged(object sender, EventArgs e) { } // This Function is to Save by printscreen private Image GetScreenCapture (bool FullScreen) { if (FullScreen) SendKeys.SendWait("{PRTSC 2}"); else SendKeys.SendWait("%{PRTSC}"); IDataObject objData = Clipboard.GetDataObject(); return (Image)objData.GetData (DataFormats.Bitmap); } // This function is to create save as private void save_btn_Click(object sender, EventArgs e) { GetScreenCapture(true); SaveFileDialog sfd1; sfd1 = new SaveFileDialog(); sfd1.Filter = "Image|*.bmp"; sfd1.FilterIndex = 1; sfd1.RestoreDirectory = true; if (sfd1.ShowDialog()==DialogResult.OK) { GetScreenCapture(true).Save(sfd1.FileName); } } } }