Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Form1()
- {
- InitializeComponent();
- }
- private void AccesoForm(String accion)
- {
- //----------------------------------Aqui llegan los datos
- strBufferIn = accion;
- TxtDatosRecibidos.Text = strBufferIn;
- string input = TxtDatosRecibidos.Text;
- string[] sp;
- sp = input.Split(',');
- textBox1.Text = sp[0];
- textBox2.Text = sp[1];
- textBox3.Text = sp[2];
- textBox4.Text = sp[3];
- textBox5.Text = sp[4];
- textBox6.Text = sp[5];
- textBox7.Text = sp[6];
- textBox8.Text = sp[7];
- //----------------------------------
- }
- private void AccesoInterrupcion(String accion)
- {
- DelegadoAcceso Var_DelegadoAcceso;
- Var_DelegadoAcceso = new DelegadoAcceso(AccesoForm);
- object[] arg = { accion };//me permite pasar datos en delegado
- base.Invoke(Var_DelegadoAcceso, arg);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- strBufferIn = "";//limpias la variable al inicio de la aplicacion
- strbufferOut = "";
- BtnConectar.Enabled = false;//limpias la conexon de los botones
- BtnEnviarDatos.Enabled = false;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- String[] PuertosDisponibles = SerialPort.GetPortNames();//array que me permite almacenar los nombre de los puertos
- CboPuertos.Items.Clear();//se limpia los valores de la varialbe
- foreach (String puerto_simple in PuertosDisponibles)//se crea una variable llamada puerto simple dedicada a buscar púertos
- {
- CboPuertos.Items.Add(puerto_simple);//se añaden loss valores
- }
- if (CboPuertos.Items.Count > 0)//si la variable puertos es mayor a 0 puertos significa que hay puertos
- {
- CboPuertos.SelectedIndex = 0;
- MessageBox.Show("SELECCIONAR PUERTO DE TRABAJO");
- BtnConectar.Enabled = true;//se habilita el boton de conectar
- }
- else
- {
- MessageBox.Show("NINGUN PUERTO DETECTADO");
- CboPuertos.Items.Clear();//se limpia los valores del puerto
- CboPuertos.Text = " ";
- strBufferIn = "";
- strbufferOut = "";
- BtnConectar.Enabled = false;//se restringe el uso de botones
- BtnEnviarDatos.Enabled = false;
- }
- }
- private void BtnConectar_Click(object sender, EventArgs e)
- {
- try
- {
- if (BtnConectar.Text == "CONECTAR")//
- {
- SpPuertos.BaudRate = Int32.Parse(CboBaudRate.Text);//se escoge el puerto
- SpPuertos.DataBits = 8;//valor de bis de datos
- SpPuertos.Parity = Parity.None;
- SpPuertos.StopBits = StopBits.One;//el detener los bits
- 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());//me indica el erro de conexion
- }
- }
- else if (BtnConectar.Text == "DESCONECTAR")
- {
- SpPuertos.Close();
- BtnConectar.Text = "CONECTAR";
- BtnEnviarDatos.Enabled = false;
- }
- }
- catch (Exception exc)
- {
- MessageBox.Show(exc.Message.ToString());
- }
- }
- private void BtnEnviarDatos_Click(object sender, EventArgs e)
- {
- SpPuertos.DiscardOutBuffer();//limpiar el mensaje de salida
- strbufferOut = TxtDatos_a_Enviar.Text;
- SpPuertos.Write(strbufferOut);
- }
- private void CboBaudRate_SelectedIndexChanged(object sender, EventArgs e)
- {
- }
- private void DatoRecibido(object sender, SerialDataReceivedEventArgs e)
- {
- AccesoInterrupcion(SpPuertos.ReadExisting());
- /* String Data_in = SpPuertos.ReadExisting();
- MessageBox.Show(Data_in);
- TxtDatosRecibidos.Text = Data_in;*/
- }
- private void TxtDatos_a_Enviar_TextChanged(object sender, EventArgs e)
- {
- }
- private void TxtDatosRecibidos_TextChanged(object sender, EventArgs e)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement