Imports System.IO.Ports
Enviado por elmordo • 26 de Septiembre de 2022 • Tarea • 1.464 Palabras (6 Páginas) • 56 Visitas
Imports System.IO.Ports
Public Class frmPrincipal
Dim WithEvents puerto As New SerialPort
Public Delegate Sub delegado()
Private Sub frmPrincipal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each com In SerialPort.GetPortNames
cmbPuertos.Items.Add(com)
Next
End Sub
Dim instruccion, proveniente As String
Private Sub btnConectar_Click(sender As Object, e As EventArgs) Handles btnConectar.Click
If puerto.IsOpen Then
puerto.Close()
End If
Try
'WIth nos evita escribir repedtidamente el nombre y saltamos a punto
With puerto
.PortName = cmbPuertos.Text
.BaudRate = 9600
.DataBits = 8
.StopBits = StopBits.One
.Parity = Parity.None
.Open()
lblConexion.Text = "Conectado a: " & .PortName
End With
Catch ex As Exception
MsgBox("Error:" & ex.Message)
End Try
End Sub
Private Sub btnDesconectar_Click(sender As Object, e As EventArgs) Handles btnDesconectar.Click
If puerto.IsOpen Then
puerto.Close()
End If
End Sub
Private Sub btnUno_Click(sender As Object, e As EventArgs) Handles btnUno.Click
If btnUno.Text = "ON" Then
btnUno.Text = "OFF"
Else
btnUno.Text = "ON"
End If
If btnUno.Text = "ON" Then
instruccion = "A"
ElseIf btnUno.Text = "OFF" Then
instruccion = "B"
End If
Try
puerto.Write(instruccion)
Catch ex As Exception
MsgBox("Error:" & ex.Message)
End Try
instruccion = ""
End Sub
Private Sub btnDos_Click(sender As Object, e As EventArgs) Handles btnDos.Click
If btnDos.Text = "ON" Then
btnDos.Text = "OFF"
Else
btnDos.Text = "ON"
End If
If btnDos.Text = "ON" Then
instruccion = "C"
ElseIf btnDos.Text = "OFF" Then
instruccion = "D"
End If
Try
puerto.Write(instruccion)
Catch ex As Exception
MsgBox("Error:" & ex.Message)
End Try
instruccion = ""
End Sub
Private Sub puerto_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles puerto.DataReceived
rtbIn.Invoke(New delegado(AddressOf recibir), New Object() {})
End Sub
Private Sub sldMotor_MouseDown(sender As Object, e As MouseEventArgs) Handles sldMotor.MouseDown
instruccion = "E"
...