0% found this document useful (0 votes)
118 views5 pages

Control Puerto Serial Con Visual Basic

This document describes the design and programming of a Visual Basic .NET form for controlling a serial port. The form contains combo boxes, text boxes, and buttons to select a serial port, send commands to devices, and view received data. The code behind the form opens and closes the serial port, sends bytes when buttons are clicked, and handles data received events to update the UI based on the received values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views5 pages

Control Puerto Serial Con Visual Basic

This document describes the design and programming of a Visual Basic .NET form for controlling a serial port. The form contains combo boxes, text boxes, and buttons to select a serial port, send commands to devices, and view received data. The code behind the form opens and closes the serial port, sends bytes when buttons are clicked, and handles data received events to update the UI based on the received values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CONTROL PUERTO SERIAL CON VISUAL BASIC.

NET
DISEO DEL FORMULARIO

COMBOBOX1

TEXTBOX1

BUTTON1
BUTTON5
BUTTON6
TEXTBOX2

BUTTON2

BUTTON3

BUTTON4

PROPIEDADES

FORMULARIO

COMBOBOX1

BUTTON5

TEXTBOX2

TEXTBOX1

BUTTON1

BUTTON2

BUTTON3

BUTTON6

BUTTON4

CODIGO DE PROGRAMACION

CODIGO FORMULARIO

Imports System.IO.Ports
Public Class Form1
Dim envio As Byte() = New Byte(0) {}
Dim recibido As String

EVENTOS

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


CheckForIllegalCrossThreadCalls = False
Button6.BackColor = Color.DarkCyan
Try
ComboBox1.Items.Clear()
For Each i As String In My.Computer.Ports.SerialPortNames
ComboBox1.Items.Add(i)
Next
Catch ex As Exception
End Try
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As


System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
End Sub

CODIGO BUTTON5
Doble clic en

Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click


Try
If Button5.Text = "CONECTAR" Then
Button5.Text = "DESCONECTAR"
Try
With SerialPort1
If .IsOpen Then
.Close()
End If
.PortName = ComboBox1.Text
.BaudRate = 9600
.Open()
ComboBox1.Enabled = False
TextBox2.AppendText("PUERTO " & .PortName & " ABIERTO" & vbCrLf)
TextBox2.AppendText("BAUDIOS " & .BaudRate & vbCrLf)
End With
Catch ex As Exception
MsgBox("Error al abrir el puerto serial: " & ex.Message, MsgBoxStyle.Critical)
End Try
Else
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
Button5.Text = "CONECTAR"
ComboBox1.Enabled = True
TextBox2.AppendText("PUERTO " & SerialPort1.PortName & " DESCONECTADO" & vbCrLf)
End If
Catch ex As Exception
End Try
End Sub

CODIGO BUTTON2
Doble clic en

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click


envio(0) = Asc("a")
SerialPort1.Write(envio, 0, envio.Length)
End Sub

CODIGO BUTTON3
Doble clic en

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click


envio(0) = Asc("b")
SerialPort1.Write(envio, 0, envio.Length)
End Sub

CODIGO BUTTON1
Doble clic en

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


envio(0) = Asc(TextBox1.Text)
SerialPort1.Write(envio, 0, envio.Length)
TextBox2.AppendText(TextBox1.Text & vbCrLf)
End Sub

CODIGO BUTTON4
Doble clic en

Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click


If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
End
End Sub

EVENTO PUERTO SERIAL

Private Sub SerialPort1_DataReceived(sender As Object, e As


System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
recibido = SerialPort1.ReadExisting
Try
TextBox2.AppendText(recibido & vbCrLf)
If recibido = 1 Then
Button2.BackColor = Color.Green
Button3.BackColor = Color.Aqua
End If
If recibido = 2 Then
Button3.BackColor = Color.Red
Button2.BackColor = Color.Aqua
End If
If recibido = 3 Then
Button6.BackColor = Color.LawnGreen
Button6.Text = "PULSADOR PRESIONADO 1"
End If
If recibido = 4 Then
Button6.BackColor = Color.LightSkyBlue
Button6.Text = "PULSADOR PRESIONADO 2"
End If
Catch ex As Exception
End Try
End Sub

You might also like