private void BotAbrirPuerto_Click(object sender, EventArgs e) // Al hacer click sobre el boton de abrir puerto { try // en el caso de no haber error { if (BotAbrirPuerto.Text == "ABRIR") // se el puerto estaba cerrado { SpPuertos.BaudRate = Int32.Parse(comboBaudRate.Text); // toma el BaudRate definido SpPuertos.DataBits = 8; // fija los bits del dato en 8 SpPuertos.Parity = Parity.None; // define datos sin paridad SpPuertos.StopBits = StopBits.One; // define un bit de parada SpPuertos.Handshake = Handshake.None; SpPuertos.PortName = ComboPuertos.Text; // guarda el nombre del puerto try { SpPuertos.Open(); // abre el puerto BotAbrirPuerto.Text = "CERRAR"; // modifica el texto del boton a "cerrar" } catch (Exception exc) // al producirse un error dispone el mensaje pertinente { MessageBox.Show(exc.Message.ToString()); } } else if (BotAbrirPuerto.Text == "CERRAR") // si el puerto estaba abierto, se cierra el puerto y cambia el texto del boton { SpPuertos.Close(); BotAbrirPuerto.Text = "ABRIR"; } } catch (Exception exc) // se dispone un mensaje ante cualquier error { MessageBox.Show(exc.Message.ToString()); } }
private void BtnConectar_Click(object sender, EventArgs e) { try { //'CONECTAR' configura el puerto, lo abre, cambia a 'DESCONECTAR' y habilita el botón 'Enviar Datos' if (BtnConectar.Text == "CONECTAR") { SpPuertos.BaudRate = Int32.Parse(CboBaudRate.Text); SpPuertos.DataBits = 8; SpPuertos.Parity = Parity.None; SpPuertos.StopBits = StopBits.One; SpPuertos.Handshake = Handshake.None; SpPuertos.PortName = CboPuertos.Text; try { SpPuertos.Open(); BtnConectar.Text = "DESCONECTAR"; BtnEnviarDatos.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } //'DESCONECTAR' cierra el puerto, cambia a 'CONECTAR' y desabilita el botón 'Enviar Datos' else if (BtnConectar.Text == "DESCONECTAR") { SpPuertos.Close(); BtnConectar.Text = "CONECTAR"; BtnEnviarDatos.Enabled = false; } } catch (Exception ex) { //Si hay algún problema conectándose se muestra por pantalla el error MessageBox.Show(ex.Message.ToString()); } }
private void btnConectar_Click(object sender, EventArgs e) { try { if (btnConectar.Text == "Conectar") { SpPuertos.BaudRate = Convert.ToInt32(cbxBaudrate.Text); SpPuertos.DataBits = 8; SpPuertos.Parity = Parity.None; SpPuertos.StopBits = StopBits.One; SpPuertos.Handshake = Handshake.None; SpPuertos.PortName = cbxPorts.Text.ToString(); try { SpPuertos.Open(); tsStatusCOM.Visible = true; tsStatusCOM.Text = " '" + cbxPorts.SelectedItem.ToString() + "' Seleccionado."; btnConectar.Text = "Desconectar"; } catch (Exception exc) { MessageBox.Show(exc.Message.ToString());; } } else if (btnConectar.Text == "Desconectar") { SpPuertos.Close(); listBox1.Items.Clear(); tsStatusCOM.Visible = false; btnConectar.Text = "Conectar"; } } catch (Exception exc) { MessageBox.Show(exc.Message.ToString()); } }
private void button2_Click(object sender, EventArgs e) { try { if (BtnConectar.Text == "CONECTAR") { SpPuertos.BaudRate = Int32.Parse(CboBaudRate.Text); SpPuertos.DataBits = 8; SpPuertos.Parity = Parity.None; SpPuertos.StopBits = StopBits.One; SpPuertos.Handshake = Handshake.None; SpPuertos.PortName = CboPuertos.Text; try { SpPuertos.Open(); BtnConectar.Text = "DESCONECTAR"; BtnEnviarDatos.Enabled = true; } catch (Exception exc) { MessageBox.Show(exc.Message.ToString()); } } else if (BtnConectar.Text == "DESCONECTAR") { SpPuertos.Close(); BtnConectar.Text = "CONECTAR"; BtnEnviarDatos.Enabled = false; } } catch (Exception exc) { MessageBox.Show(exc.Message.ToString()); } }
private void BtnConectar_Click(object sender, EventArgs e) { try { if (BtnConectar.Text == "Conectar") { SpPuertos.BaudRate = 115200; SpPuertos.DataBits = 8; SpPuertos.Parity = Parity.None; SpPuertos.StopBits = StopBits.One; SpPuertos.Handshake = Handshake.None; SpPuertos.PortName = CboPuertos.Text; try { SpPuertos.Open(); BtnConectar.Text = "Desconectar"; BtnEnviarDatos.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } else if (BtnConectar.Text == "Desconectar") { SpPuertos.Close(); BtnConectar.Text = "Desconectar"; BtnEnviarDatos.Enabled = false; } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }