Gracias por contestar,
primero abro el puerto con esta instrucción al activar un botón:
Private Sub BtnConnectar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnectar.Click
If BtnConnectar.Text = "CONECTAR" Then
Try
With SpPort
.BaudRate = 9600
.DataBits = 8
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.PortName = CboPorts.Text
.Encoding = System.Text.Encoding.GetEncoding(1252)
.DiscardNull = False
.Open()
End With
BtnConnectar.Text = "DESCONECTAR"
BtnEnviarDades.Enabled = True
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
ElseIf BtnConnectar.Text = "DESCONECTAR" Then
BtnConnectar.Text = "CONECTAR"
BtnEnviarDades.Enabled = False
SpPort.Close()
End If
End Sub
Luego para recibir los datos entrantes hago servir delegados:
Private Sub AccesFormPrincipial(ByVal TextForm As String)
StrBufferEntrada = TextForm
TxtDadesRebudes.Text &= StrBufferEntrada 'Muestro las dadas recibidas en un textbox
End Sub
Private Sub DadaRebuda(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SpPort.DataReceived
Dim DadaInterrupcio As String
DadaInterrupcio = SpPort.ReadExisting
PortaAccesInterrupcio(DadaInterrupcio)
End Sub
Private Sub PortaAccesInterrupcio(ByVal BufferIn As String)
Dim TextInterrupcio() As Object = {BufferIn}
Dim DelegatInterrupcio As DelegatAcces
DelegatInterrupcio = New DelegatAcces(AddressOf AccesFormPrincipial)
MyBase.Invoke(DelegatInterrupcio, TextInterrupcio)
End Sub
Sería esta parte