Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. public Form1()
  2. {
  3. InitializeComponent();
  4. }
  5.  
  6. private void AccesoForm(String accion)
  7. {
  8.  
  9. //----------------------------------Aqui llegan los datos
  10. strBufferIn = accion;
  11. TxtDatosRecibidos.Text = strBufferIn;
  12. string input = TxtDatosRecibidos.Text;
  13. string[] sp;
  14. sp = input.Split(',');
  15. textBox1.Text = sp[0];
  16. textBox2.Text = sp[1];
  17. textBox3.Text = sp[2];
  18. textBox4.Text = sp[3];
  19. textBox5.Text = sp[4];
  20. textBox6.Text = sp[5];
  21. textBox7.Text = sp[6];
  22. textBox8.Text = sp[7];
  23.  
  24.  
  25.  
  26.  
  27.  
  28. //----------------------------------
  29. }
  30.  
  31. private void AccesoInterrupcion(String accion)
  32. {
  33. DelegadoAcceso Var_DelegadoAcceso;
  34. Var_DelegadoAcceso = new DelegadoAcceso(AccesoForm);
  35. object[] arg = { accion };//me permite pasar datos en delegado
  36. base.Invoke(Var_DelegadoAcceso, arg);
  37. }
  38.  
  39.  
  40. private void Form1_Load(object sender, EventArgs e)
  41. {
  42. strBufferIn = "";//limpias la variable al inicio de la aplicacion
  43. strbufferOut = "";
  44. BtnConectar.Enabled = false;//limpias la conexon de los botones
  45. BtnEnviarDatos.Enabled = false;
  46. }
  47.  
  48.  
  49.  
  50. private void button1_Click(object sender, EventArgs e)
  51. {
  52. String[] PuertosDisponibles = SerialPort.GetPortNames();//array que me permite almacenar los nombre de los puertos
  53.  
  54. CboPuertos.Items.Clear();//se limpia los valores de la varialbe
  55. foreach (String puerto_simple in PuertosDisponibles)//se crea una variable llamada puerto simple dedicada a buscar púertos
  56. {
  57. CboPuertos.Items.Add(puerto_simple);//se añaden loss valores
  58. }
  59. if (CboPuertos.Items.Count > 0)//si la variable puertos es mayor a 0 puertos significa que hay puertos
  60. {
  61. CboPuertos.SelectedIndex = 0;
  62. MessageBox.Show("SELECCIONAR PUERTO DE TRABAJO");
  63. BtnConectar.Enabled = true;//se habilita el boton de conectar
  64. }
  65. else
  66. {
  67. MessageBox.Show("NINGUN PUERTO DETECTADO");
  68. CboPuertos.Items.Clear();//se limpia los valores del puerto
  69. CboPuertos.Text = " ";
  70. strBufferIn = "";
  71. strbufferOut = "";
  72. BtnConectar.Enabled = false;//se restringe el uso de botones
  73. BtnEnviarDatos.Enabled = false;
  74.  
  75. }
  76. }
  77.  
  78.  
  79.  
  80.  
  81. private void BtnConectar_Click(object sender, EventArgs e)
  82. {
  83. try
  84. {
  85. if (BtnConectar.Text == "CONECTAR")//
  86. {
  87. SpPuertos.BaudRate = Int32.Parse(CboBaudRate.Text);//se escoge el puerto
  88. SpPuertos.DataBits = 8;//valor de bis de datos
  89. SpPuertos.Parity = Parity.None;
  90. SpPuertos.StopBits = StopBits.One;//el detener los bits
  91. SpPuertos.Handshake = Handshake.None;
  92. SpPuertos.PortName = CboPuertos.Text;
  93.  
  94. try
  95. {
  96. SpPuertos.Open();
  97. BtnConectar.Text = "DESCONECTAR";
  98. BtnEnviarDatos.Enabled = true;
  99. }
  100. catch (Exception exc)
  101. {
  102. MessageBox.Show(exc.Message.ToString());//me indica el erro de conexion
  103. }
  104. }
  105. else if (BtnConectar.Text == "DESCONECTAR")
  106. {
  107. SpPuertos.Close();
  108. BtnConectar.Text = "CONECTAR";
  109. BtnEnviarDatos.Enabled = false;
  110.  
  111. }
  112. }
  113. catch (Exception exc)
  114. {
  115. MessageBox.Show(exc.Message.ToString());
  116.  
  117. }
  118. }
  119.  
  120.  
  121.  
  122.  
  123. private void BtnEnviarDatos_Click(object sender, EventArgs e)
  124. {
  125. SpPuertos.DiscardOutBuffer();//limpiar el mensaje de salida
  126. strbufferOut = TxtDatos_a_Enviar.Text;
  127. SpPuertos.Write(strbufferOut);
  128. }
  129.  
  130.  
  131.  
  132.  
  133. private void CboBaudRate_SelectedIndexChanged(object sender, EventArgs e)
  134. {
  135.  
  136. }
  137.  
  138. private void DatoRecibido(object sender, SerialDataReceivedEventArgs e)
  139. {
  140.  
  141. AccesoInterrupcion(SpPuertos.ReadExisting());
  142. /* String Data_in = SpPuertos.ReadExisting();
  143. MessageBox.Show(Data_in);
  144. TxtDatosRecibidos.Text = Data_in;*/
  145.  
  146.  
  147. }
  148.  
  149. private void TxtDatos_a_Enviar_TextChanged(object sender, EventArgs e)
  150. {
  151.  
  152. }
  153.  
  154. private void TxtDatosRecibidos_TextChanged(object sender, EventArgs e)
  155. {
  156.  
  157.  
  158.  
  159.  
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement