private void MySerialMCUReceived(object sender, EventArgs e) { if (SerialPort_MCU.IsOpen) { int NumOfRcvs = SerialPort_MCU.BytesToRead; if (NumOfRcvs == 64) { byte[] buf = new byte[NumOfRcvs]; SerialPort_MCU.Read(buf, 0, NumOfRcvs); if ((Convert.ToChar(buf[0]) == 'S') && (Convert.ToChar(buf[63]) == 'T')) { if (buf[1] == 0x01) // Fire Detection [0x01] { if (buf[2] == 0x01) { PB_FIRE.Visible = true; } else if (buf[2] == 0x00) { PB_FIRE.Visible = false; } //MessageBox.Show("Send Message Complete", "System Message"); } } } } }
private void BTN_Conn_Com_MCU_Click(object sender, EventArgs e) { try { if (cbCom_MCU.Text == "") { MessageBox.Show("Please Select the Availabe ports", "System Message"); } else { try { SerialPort_MCU.PortName = cbCom_MCU.Text; SerialPort_MCU.BaudRate = 115200; SerialPort_MCU.StopBits = StopBits.One; SerialPort_MCU.DataReceived += new SerialDataReceivedEventHandler(SerialPort_MCU_DataReceived); SerialPort_MCU.Open(); if (SerialPort_MCU.IsOpen) { TB_Com_MCU.Text = cbCom_MCU.Text; BTN_Conn_Com_MCU.Enabled = false; BTN_Disconn_Com_MCU.Enabled = true; cbCom_MCU.Enabled = false; PB_Port_OFF_MCU.Visible = false; PB_Port_ON_MCU.Visible = true; MessageBox.Show("MCU Port Open", "System Message"); //timer_wtdg.Enabled = true; //timer_wtdg.Start(); } } catch (System.IO.IOException ex) { MessageBox.Show("Error: " + ex.ToString(), "ERROR"); } } } catch (UnauthorizedAccessException) { MessageBox.Show("Un authorized Access!!"); } }
private void BTN_Disconn_Com_MCU_Click(object sender, EventArgs e) { if (SerialPort_MCU.IsOpen) { SerialPort_MCU.DiscardInBuffer(); SerialPort_MCU.DiscardOutBuffer(); SerialPort_MCU.Close(); if (!SerialPort_MCU.IsOpen) { BTN_Conn_Com_MCU.Enabled = true; BTN_Disconn_Com_MCU.Enabled = false; cbCom_MCU.Enabled = true; PB_Port_OFF_MCU.Visible = true; PB_Port_ON_MCU.Visible = false; MessageBox.Show("COM Port Closed", "System Message"); } GetAvailablePort(); } }
private void MySerialReceived(object sender, EventArgs e) { if (SerialPort.IsOpen) { int NumOfRcvs = SerialPort.BytesToRead; if (NumOfRcvs == 64) { byte[] buf = new byte[NumOfRcvs]; SerialPort.Read(buf, 0, NumOfRcvs); if ((Convert.ToChar(buf[0]) == 'S') && (Convert.ToChar(buf[63]) == 'T')) { if (buf[1] == 0x01) // Spot Send Response [0x01] { lblState.Text = "MOVING"; lblState.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(253)))), ((int)(((byte)(255)))), ((int)(((byte)(169))))); PictureBox_Icon.Image = global::SprinklerProject.Properties.Resources.icon_moving; //MessageBox.Show("Send Message Complete", "System Message"); } else if (buf[1] == 0x02) // Move End [0x02] { byte[] packet; if (PB_FIRE.Visible == true) { lblState.Text = "SHOOT"; lblState.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(169)))), ((int)(((byte)(255)))), ((int)(((byte)(171))))); PictureBox_Icon.Image = global::SprinklerProject.Properties.Resources.icon_shoot; packet = Packaging_Spray_Start(); SerialPort_MCU.Write(packet, 0, packet.Length); //USB : 64byte packet = Packaging_MotionMoving(); SerialPort.Write(packet, 0, packet.Length); //USB : 64byte } else { lblState.Text = "MOVING"; lblState.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(253)))), ((int)(((byte)(255)))), ((int)(((byte)(169))))); PictureBox_Icon.Image = global::SprinklerProject.Properties.Resources.icon_moving; packet = Packaging_MoveHome(); SerialPort.Write(packet, 0, packet.Length); //USB : 64byte } } else if (buf[1] == 0x03) // Motion Moving End [0x03] { lblState.Text = "MOVING"; lblState.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(253)))), ((int)(((byte)(255)))), ((int)(((byte)(169))))); PictureBox_Icon.Image = global::SprinklerProject.Properties.Resources.icon_moving; byte[] packet = Packaging_Spray_Stop(); SerialPort_MCU.Write(packet, 0, packet.Length); //USB : 64byte packet = Packaging_MoveHome(); SerialPort.Write(packet, 0, packet.Length); //USB : 64byte } else if (buf[1] == 0x04) // Move to Home End [0x04] { lblState.Text = "READY"; lblState.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); PictureBox_Icon.Image = global::SprinklerProject.Properties.Resources.icon_ready; } else if (buf[1] == 0x05) // Recall { MessageBox.Show("Data Transmit Fail", "System Message"); } } } } }