using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.Ports; namespace BlenderGUI { public partial class Form1 : Form { private string[] Recipes; private int [,] ingredient; private int[,] amount; private int selection; private int [] bottle; private int next_recipe; private int next_ingre = 0; private int flag; //private char[] Pre_Sent_Data; private StringBuilder Send_Data; private string function; SerialPort port; private string temp, CorrectString = ""; private int dispensed = 0, blending = 0, loaded = 0, calibrated = 0; public Form1() { InitializeComponent(); //Initial Recipes Recipes = new string[10]; ingredient = new int[10,4]; amount = new int[10,4]; bottle = new int[4]; Recipes[0] = "SweetWater"; Recipes[1] = "HoneyWater"; next_recipe = 2; ingredient[0, 0] = 0; ingredient[0, 1] = 7; ingredient[0, 2] = -1; ingredient[0, 3] = -1; ingredient[1, 0] = 0; ingredient[1, 1] = 8; ingredient[1, 2] = -1; ingredient[1, 3] = -1; amount[0, 0] = 100; amount[0, 1] = 10; amount[0, 2] = 0; amount[0, 3] = 0; amount[1, 0] = 100; amount[1, 1] = 10; amount[1, 2] = 0; amount[1, 3] = 0; //Initiaize Serial Port at 9600 baud for communication port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One); port.Open(); } private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Add(Recipes[0]); listBox1.Items.Add(Recipes[1]); Send_Data = new StringBuilder(""); } private void button3_Click(object sender, EventArgs e) { //close the port before exiting the program port.Close(); Application.Exit(); } private void button4_Click(object sender, EventArgs e) //Load Recipe { if (listBox1.SelectedIndex >= 0) //will load up a recipe only when we select a recipe from the list box { selection = listBox1.SelectedIndex; textBox1.Text = Recipes[selection]; //put everything back to zero first numericUpDown1.Value = 0; numericUpDown2.Value = 0; numericUpDown3.Value = 0; numericUpDown4.Value = 0; numericUpDown5.Value = 0; numericUpDown6.Value = 0; numericUpDown7.Value = 0; numericUpDown8.Value = 0; numericUpDown9.Value = 0; numericUpDown10.Value = 0; //Display the amount of each ingredient in appropriate numericUpDown box for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 0 && amount[selection,i] > 0) numericUpDown1.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 1) numericUpDown2.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 2) numericUpDown3.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 3) numericUpDown4.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 4) numericUpDown5.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 5) numericUpDown6.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 6) numericUpDown7.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 7) numericUpDown8.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 8) numericUpDown9.Value = amount[selection, i]; } for (int i = 0; i < 4; i++) { if (ingredient[selection, i] == 9) numericUpDown10.Value = amount[selection, i]; } loaded = 1; //loaded flag is on } else MessageBox.Show("Please select one of the recipes or create a new one"); } private void button2_Click(object sender, EventArgs e) //Dispense { if (loaded == 1 && calibrated == 1 && blending == 0) //make sure recipe is loaded and bottles are calibrated and we are not blending { Send_Data.Remove(0, Send_Data.Length); //Building the Data Array function = "d"; Send_Data.Append(function); //determine what to append for the dispense string for (int i = 0; i < 4; i++) { flag = 0; for (int j = 0; j < 4; j++) { if (ingredient[selection, j] == bottle[i]) { if (amount[selection, j] == 0) { flag = 1; } if (amount[selection, j] < 10) { Send_Data.Append("00"); Send_Data.Append(amount[selection, j]); flag = 1; } else if (amount[selection, j] < 100) { Send_Data.Append("0"); Send_Data.Append(amount[selection, j]); flag = 1; } else { Send_Data.Append(amount[selection, j]); flag = 1; } } } //less than 4 ingredient if (flag == 0) Send_Data.Append("000"); } dispensed = 1; Send_Data.Append("\r"); MessageBox.Show(Send_Data.ToString()); port.Write(Send_Data.ToString()); } else MessageBox.Show("Please calibrate and load a recipe before you dispense"); } private void button1_Click(object sender, EventArgs e) //Create new recipe { if (next_recipe < 10) { next_ingre = 0; if (textBox1.Text.Length < 11) Recipes[next_recipe] = textBox1.Text.PadLeft(10); else { Recipes[next_recipe] = (textBox1.Text).Substring(0, 10); MessageBox.Show("10 character maximum for recipe names"); } if (numericUpDown1.Value > 0) { ingredient[next_recipe, next_ingre] = 0; amount[next_recipe, next_ingre] = (int)numericUpDown1.Value; next_ingre++; } if (numericUpDown2.Value > 0) { ingredient[next_recipe, next_ingre] = 1; amount[next_recipe, next_ingre] = (int)numericUpDown2.Value; next_ingre++; } if (numericUpDown3.Value > 0) { ingredient[next_recipe, next_ingre] = 2; amount[next_recipe, next_ingre] = (int)numericUpDown3.Value; next_ingre++; } if (numericUpDown4.Value > 0) { ingredient[next_recipe, next_ingre] = 3; amount[next_recipe, next_ingre] = (int)numericUpDown4.Value; next_ingre++; } if (numericUpDown5.Value > 0) { ingredient[next_recipe, next_ingre] = 4; amount[next_recipe, next_ingre] = (int)numericUpDown5.Value; next_ingre++; } if (numericUpDown6.Value > 0) { ingredient[next_recipe, next_ingre] = 5; amount[next_recipe, next_ingre] = (int)numericUpDown6.Value; next_ingre++; } if (numericUpDown7.Value > 0) { ingredient[next_recipe, next_ingre] = 6; amount[next_recipe, next_ingre] = (int)numericUpDown7.Value; next_ingre++; } if (numericUpDown8.Value > 0) { ingredient[next_recipe, next_ingre] = 7; amount[next_recipe, next_ingre] = (int)numericUpDown8.Value; next_ingre++; } if (numericUpDown9.Value > 0) { ingredient[next_recipe, next_ingre] = 8; amount[next_recipe, next_ingre] = (int)numericUpDown9.Value; next_ingre++; } if (numericUpDown10.Value > 0) { ingredient[next_recipe, next_ingre] = 9; amount[next_recipe, next_ingre] = (int)numericUpDown10.Value; next_ingre++; } //MessageBox.Show(next_ingre.ToString()); listBox1.Items.Add(Recipes[next_recipe]); next_recipe++; } else MessageBox.Show("Only 10 recipes allowed"); } private void button5_Click(object sender, EventArgs e) //Upload Recipes to MCU { Send_Data.Remove(0,Send_Data.Length); //Building the Data Array function = "v"; //first append save function command Send_Data.Append(function); //Append number of recipes (2 digits) if (next_recipe >= 10) Send_Data.Append(next_recipe.ToString()); else if (next_recipe < 10) { Send_Data.Append("0"); Send_Data.Append(next_recipe.ToString()); } //Start appending recipes for (int i = 0; i < next_recipe; i++) { //Append recipe name Send_Data.Append(Recipes[i].ToString()); for (int j = 0; j < 4; j++) { //Append ingredient and amounts if (amount[i, j] > 0) { Send_Data.Append(ingredient[i, j].ToString()); Send_Data.Append(amount[i, j].ToString().PadLeft(3,'0')); } else //Empty ingredient { Send_Data.Append("0000"); } } } Send_Data.Append("\r"); port.Write(Send_Data.ToString()); //MessageBox.Show(Send_Data.ToString()); } private void button7_Click(object sender, EventArgs e) //Calibration { if (comboBox1.SelectedIndex >= 0 && comboBox2.SelectedIndex >= 0 && comboBox3.SelectedIndex >= 0 && comboBox4.SelectedIndex >= 0) { bottle[0] = comboBox1.SelectedIndex; bottle[1] = comboBox2.SelectedIndex; bottle[2] = comboBox3.SelectedIndex; bottle[3] = comboBox4.SelectedIndex; calibrated = 1; //set calibrated flag to 1 } else MessageBox.Show("Please Select Contents in the Bottles"); } private void button8_Click(object sender, EventArgs e) //Blend { Send_Data.Remove(0, Send_Data.Length); if (dispensed == 1) //blend only when there is stuff in the cup { if (button8.Text == "Start Blend") //change the text on the button { function = "b\r"; //send the blending command Send_Data.Append(function); button8.Text = "Stop Blend"; blending = 1; } else if (button8.Text == "Stop Blend") //change the text back to stop blend { function = "s\r"; //send the stop blending command Send_Data.Append(function); button8.Text = "Start Blend"; blending = 0; dispensed = 0; //need to dispense again to blend } port.Write(Send_Data.ToString()); } else MessageBox.Show("No ingredients dispensed yet"); } private void button9_Click(object sender, EventArgs e) //Delete recipe { if (listBox1.SelectedIndex >= 0) //recipe selected { //move the items in at the end of the array to where we want to delete Recipes[listBox1.SelectedIndex] = Recipes[next_recipe - 1]; for (int i = 0; i < 4; i++) { ingredient[listBox1.SelectedIndex, i] = ingredient[next_recipe - 1, i]; amount[listBox1.SelectedIndex, i] = amount[next_recipe - 1, i]; } listBox1.Items.Remove(listBox1.SelectedItem); //decrement the number of next_recipe by 1 next_recipe--; } else MessageBox.Show("No recipe selected"); } private void button6_Click(object sender, EventArgs e) //Download Recipes from MCU { Send_Data.Remove(0, Send_Data.Length); //send out the download recipe request command function = "l\r"; Send_Data.Append(function); //clear up current list first for (int i = 0; i < next_recipe; i++) listBox1.Items.Clear(); port.Write(Send_Data.ToString()); //if anything in the serial buffer, we will enter this function port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); //Load up list of recipes after we read them in if (CorrectString.Length > 0 && CorrectString[0] == '*') { for (int i = 0; i < next_recipe; i++) { listBox1.Items.Add(Recipes[i]); } } } void port_DataReceived(object sender, SerialDataReceivedEventArgs e) //This function will read the buffer from the serial port { temp = port.ReadLine(); if (temp[0] == '*') //we only care if the line starts with * { CorrectString = temp; //parsing recipe number next_recipe = int.Parse((CorrectString.Substring(1, 2)).ToString()); for (int i = 0; i < next_recipe; i++) { //parsing recipe name Recipes[i] = (CorrectString.Substring(3 + 26 * i, 10)).ToString(); for (int j = 0; j < 4; j++) { //parsing ingredient code ingredient[i, j] = int.Parse((CorrectString.Substring(13 + 4 * j + 26 * i, 1)).ToString()); //parsing amount for each ingredient amount[i, j] = int.Parse((CorrectString.Substring(14 + 4 * j + 26 * i, 3)).ToString()); } } } } } }