//*********************************************************** // User written functions //*********************************************************** // get_reply : Read a status reply from the MBED // // All commands will return a status reply on completing the specified command. // A returned valur of 0 will indicate that all is well public int get_reply() { string reply; int status; serialPort1.DiscardInBuffer(); serialPort1.ReadTimeout = READ_TIMEOUT; try { reply = serialPort1.ReadLine(); } catch (TimeoutException) { if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Readline timeout fail" + Environment.NewLine); } else { Debug_window.AppendText("Brak odpowiedzi systemu" + Environment.NewLine); } return(-1); } status = Convert.ToInt32(reply); if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Status = " + reply + Environment.NewLine); } else { Debug_window.AppendText(" Status " + reply + Environment.NewLine); } return(status); }
private void Motor_control_Click(object sender, EventArgs e) { try { string command = "b" + Convert.ToString(SensorUpDown.Value); Debug_window.AppendText(command + Environment.NewLine); serialPort1.WriteLine(command); if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Reading reply " + Environment.NewLine); } else { Debug_window.AppendText("" + Environment.NewLine); } int status = get_reply(); //Debug_window.Text = serialPort1.ReadLine(); } catch (TimeoutException) { if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Timeout Exception" + Environment.NewLine); } else { Debug_window.AppendText("" + Environment.NewLine); } } }
private void Read_Distance_Click(object sender, EventArgs e) { try { string command = "d"; Debug_window.AppendText(command + Environment.NewLine); serialPort1.WriteLine(command); if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Reading reply" + Environment.NewLine); } else { Debug_window.AppendText("" + Environment.NewLine); } int status = get_reply(); //Debug_window.Text = serialPort1.ReadLine(); if (status == 0) { if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Reading data" + Environment.NewLine); } else { Debug_window.AppendText("" + Environment.NewLine); } string data = serialPort1.ReadLine(); if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Data = " + data + Environment.NewLine); } else { Debug_window.AppendText(" = " + data + Environment.NewLine); } } } catch (TimeoutException) { if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Timeout exception" + Environment.NewLine); } else { Debug_window.AppendText("" + Environment.NewLine); } } }
private void Read_Sensor_Click(object sender, EventArgs e) { try { string command = "c"; Debug_window.AppendText(command + Environment.NewLine); serialPort1.WriteLine(command); if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Reading reply " + Environment.NewLine); } else { Debug_window.AppendText("Odpowiedz systemu" + Environment.NewLine); } int status = get_reply(); //Debug_window.Text = serialPort1.ReadLine(); if (status == 0) { if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Reading data" + Environment.NewLine); } else { Debug_window.AppendText("Odpowiedz systemu" + Environment.NewLine); } string data = serialPort1.ReadLine(); if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Data = " + data + Environment.NewLine); } else { Debug_window.AppendText(" Otrzymane wartosci " + data + Environment.NewLine); } } } catch (TimeoutException) { if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Timeout Exception" + Environment.NewLine); } else { Debug_window.AppendText("System wykryl problem" + Environment.NewLine); } } }
// Executed when form is first loaded. Main activity is to get a list of the available COM ports // on the computer and add them to the dropdown box (comboBox1). // /*private void Servo_Load(object sender, EventArgs e) * { * * * comboBox1.Items.Clear(); * foreach (string s in SerialPort.GetPortNames()) * { * comboBox1.Items.Add(s); * } * comboBox1.SelectedIndex = 0; * serialPort1.BaudRate = COM_BAUD; * global_error = OK; * Thread.Sleep(2000); // sleep 2 seconds * } */ private void Com_Choice_Click(object sender, EventArgs e) { string com_port = comboBox1.SelectedItem.ToString(); if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Selected COM Port = " + com_port + Environment.NewLine); } else { Debug_window.AppendText(" wybierz port " + com_port + Environment.NewLine); } try { if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Trying to open " + com_port + Environment.NewLine); } else { Debug_window.AppendText("Proba wybrania portu" + com_port + Environment.NewLine); } serialPort1.PortName = com_port; serialPort1.Open(); } catch { if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Cannot open " + com_port + Environment.NewLine); } else { Debug_window.AppendText("Nie mozna otworzyc portu" + com_port + Environment.NewLine); } return; } if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText(com_port + " now open" + Environment.NewLine); } else { Debug_window.AppendText(com_port + "Port otwarty" + Environment.NewLine); } }
private void Com_Choice_Click(object sender, EventArgs e) { string com_port = comboBox1.SelectedItem.ToString(); if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Selected COM Port = " + com_port + Environment.NewLine); } else { Debug_window.AppendText("Wybor poru = " + com_port + Environment.NewLine); } try { if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Trying to open " + com_port + Environment.NewLine); } else { Debug_window.AppendText("Sporobuj otworzyc port " + com_port + Environment.NewLine); } serialPort1.PortName = com_port; serialPort1.Open(); } catch { if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText("Cannot open " + com_port + Environment.NewLine); } else { Debug_window.AppendText("Port niedostepny " + com_port + Environment.NewLine); } return; } if (label2.Text.Equals("Sensor Control")) { Debug_window.AppendText(com_port + " now open" + Environment.NewLine); } else { Debug_window.AppendText(com_port + " otworz teraz" + Environment.NewLine); } }
// // send 's' servo command to MBED // private void Servo_Detail_Click(object sender, EventArgs e) { string command = "s " + Convert.ToString(numericUpDown1.Value) + " " + Convert.ToString(numericUpDown2.Value); Debug_window.AppendText(command + Environment.NewLine); serialPort1.WriteLine(command); if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Reading reply" + Environment.NewLine); } else { Debug_window.AppendText("" + Environment.NewLine); } int status = get_reply(); }
private void Stock_set_Click(object sender, EventArgs e) { string command = "w " + Convert.ToString(numericUpDown1.Value) + " " + Convert.ToString(numericUpDown2.Value) + " " + Convert.ToString(numericUpDown3.Value); Debug_window.AppendText(command + Environment.NewLine); serialPort1.WriteLine(command); if (label1.Text.Equals("Red")) { Debug_window.AppendText("Stock level set" + Environment.NewLine); } else { Debug_window.AppendText("Poziom kolummn ustalony" + Environment.NewLine); } }
// // send 'r' read command to MBED // private void Servo_Read_Click(object sender, EventArgs e) { String command = "r"; Debug_window.AppendText(command + Environment.NewLine); serialPort1.WriteLine(command); if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Reading reply" + Environment.NewLine); } else { Debug_window.AppendText("Odczyt systemu" + Environment.NewLine); } int status = get_reply(); // // only read data if status reply was 0 (i.e. was successful) // if (status == 0) { if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Reading data" + Environment.NewLine); } else { Debug_window.AppendText("Odczyt systemu" + Environment.NewLine); } string data = serialPort1.ReadLine(); if (Servo_Detail.Text.Equals("Go")) { Debug_window.AppendText("Data = " + data + Environment.NewLine); } else { Debug_window.AppendText("Dane odczytu" + data + Environment.NewLine); } } }
private void clearToolStripMenuItem_Click(object sender, EventArgs e) { Debug_window.Clear(); }
private void Com_Choice_Click(object sender, EventArgs e) { string com_port = comboBox1.SelectedItem.ToString(); if (label1.Text.Equals("Red")) { Debug_window.AppendText("Selected COM Port = " + com_port + Environment.NewLine); } else { Debug_window.AppendText(" Wybierz port" + com_port + Environment.NewLine); } try { if (label1.Text.Equals("Red")) { Debug_window.AppendText("Trying to open " + com_port + Environment.NewLine); } else { Debug_window.AppendText("Odczyt portu" + com_port + Environment.NewLine); } serialPort1.PortName = com_port; serialPort1.Open(); } catch { if (label1.Text.Equals("Red")) { Debug_window.AppendText("Cannot open " + com_port + Environment.NewLine); } else { Debug_window.AppendText("Nie mozna otworzyc" + com_port + Environment.NewLine); } return; } if (label1.Text.Equals("Red")) { Debug_window.AppendText(com_port + " now open" + Environment.NewLine); } else { Debug_window.AppendText(com_port + "System otwarty" + Environment.NewLine); } //Check Stock Levels------------------ string command = "q"; if (label1.Text.Equals("Red")) { Debug_window.AppendText("Checking stock levels..." + Environment.NewLine); } else { Debug_window.AppendText("Spradzanie poziomu kolumn" + Environment.NewLine); } serialPort1.WriteLine(command); int status = get_reply(); // // only read data if status reply was 0 (i.e. was successful) // if (status == 0) { string data = serialPort1.ReadLine(); string[] stock; stock = data.Split(); if (label1.Text.Equals("Red")) { Debug_window.AppendText(Environment.NewLine + "STOCK" + Environment.NewLine + Environment.NewLine + "Red = " + stock[0] + Environment.NewLine + "Green = " + stock[1] + Environment.NewLine + "Blue = " + stock[2] + Environment.NewLine + Environment.NewLine); } else { Debug_window.AppendText(Environment.NewLine + "Zasob" + Environment.NewLine + Environment.NewLine + " Czerwony " + stock[0] + Environment.NewLine + " Zielony " + stock[1] + Environment.NewLine + " Niebieski " + stock[2] + Environment.NewLine + Environment.NewLine); } numericUpDown1.Value = Int32.Parse(stock[0]); numericUpDown1.Minimum = Int32.Parse(stock[0]); numericUpDown2.Value = Int32.Parse(stock[1]); numericUpDown2.Minimum = Int32.Parse(stock[1]); numericUpDown3.Value = Int32.Parse(stock[2]); numericUpDown3.Minimum = Int32.Parse(stock[2]); } //------------------------------------ }
private void Clear_Click(object sender, EventArgs e) { Debug_window.Clear(); }