Hola ha todos estoy haciendo un interfaz para comunicarme con microcontrolador.Lo de enviar y recibir dato fuciona perfectamante lo que necesito es que
segun el caracte se me aloje en un sitio ò en otro por ejen 1ºTextBox al recibir "A" se ponga rojo,al recibir "a" torne a blanco 2ºTextBox al recibir "B" se ponga
rojo,al recibir "b" torne a blanco 3º textBox al recibir "C" se ponga rojo y al trecibir "c" blanbo 4ºTextBox al recibir "D" se ponga en rojo y "d" blanco.
El codigo que pongo a continuacion funciona todo.Lo que esta puesto entre +++++ no funciona esto me lo puso un compañero del foro,pero al igual falta algo
porque a mi no me funciona y podeis ayudarme os lo agradezco gracias pepe
Imports System.IO.Ports
Public Class Form_Principal
Dim Recibidos As String
Dim A() As Byte = {&H41}
Dim B() As Byte = {&H42}
Public Sub New()
' Llamada necesaria para el Diseñador de Windows Forms.
InitializeComponent()
' Agregue cualquier inicialización después de la llamada a InitializeComponent().
If Not SerialPort1.IsOpen Then
Try
SerialPort1.Open()
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End If
AddHandler SerialPort1.DataReceived, AddressOf Recepcion
End Sub
Private Sub Recepcion(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Recibidos += SerialPort1.ReadExisting()
Me.Invoke(New EventHandler(AddressOf Actualizar))
End Sub
Private Sub Actualizar(ByVal s As Object, ByVal e As EventArgs)
TextBox1.Text = Recibidos
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
If SerialPort1.ReadExisting.Contains("A") Then '(NOTA)
TextBox1.BackColor = Color.Red
ElseIf SerialPort1.ReadExisting.Contains("a") Then
TextBox2.BackColor = Color.PowderBlue
ElseIf SerialPort1.ReadExisting.Contains("B") Then
TextBox2.BackColor = Color.Red
ElseIf SerialPort1.ReadExisting.Contains("b") Then
TextBox2.BackColor = Color.PowderBlue
Else If SerialPort1.ReadExisting.Contains("C") Then
TextBox3.BackColor = Color.Red
ElseIf SerialPort1.ReadExisting.Contains("c") Then
TextBox3.BackColor = Color.PowderBlue
If SerialPort1.ReadExisting.Contains("D") Then '
TextBox4.BackColor = Color.Red
ElseIf SerialPort1.ReadExisting.Contains("d") Then
TextBox4.BackColor = Color.PowderBlue
End If
NOTA
Aqui si coincide el caracter ("A") ò ("41") tendria que leer
la siguiente instrucion si no se la salta.
Si dejo en blanco el ("") si lee la siguiente instruccion pero lo hace con todos los caracteres.
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mBuffer As Byte() = New Byte(0) {}
mBuffer(0) = &H41 'Aen ascii
SerialPort1.Write(mBuffer, 0, mBuffer.Length)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mBuffer As Byte() = New Byte(0) {}
mBuffer(0) = &H42 'B en ascii
SerialPort1.Write(mBuffer, 0, mBuffer.Length)
End Sub
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mBuffer As Byte() = New Byte(0) {}
mBuffer(0) = &H43 'C en ascii
SerialPort1.Write(mBuffer, 0, mBuffer.Length)
End Sub
Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim mBuffer As Byte() = New Byte(0) {}
mBuffer(0) = &H44 'D en ascii
SerialPort1.Write(mBuffer, 0, mBuffer.Length)
End Sub
End Class