EDN Admin
Well-known member
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form1
------------------------------------------------
Dim Puertos As Array
Delegate Sub SetTextCallback(ByVal [text] As String) Added to prevent threading errors during receiveing of data
------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Puertos = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(Puertos)
ComboBox2.SelectedIndex = 2
ComboBox1.SelectedIndex = 1
End Sub
------------------------------------------------
Private Sub ComboBox1_Click(sender As System.Object, e As System.EventArgs) Handles ComboBox1.Click
End Sub
------------------------------------------------
Private Sub BIniciar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BIniciar.Click
If ComboBox1.Text.Length > 1 And ComboBox2.Text.Length > 1 Then
SerialPort1.PortName = ComboBox1.Text
SerialPort1.BaudRate = ComboBox2.Text
SerialPort1.Open()
BIniciar.Enabled = False
BTerminar.Enabled = True
LEspere.Visible = True
Timer1.Enabled = True
ElseIf ComboBox1.Text.Length < 1 Then
MsgBox("Seleccione un puerto valido.", MsgBoxStyle.OkOnly, "atención")
ElseIf ComboBox2.Text.Length < 1 Then
MsgBox("Seleccione un valor de baudios.", MsgBoxStyle.OkOnly, "atención")
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String) input from ReadExisting
If Me.RichTextBox2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBox2.Text &= [text] append text
If Mid(text, 1, 1) = Chr(2) And Mid(text, 9, 1) = Chr(13) Then
LPeso.Text = text
End If
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If SerialPort1.IsOpen Then
RectangleShape1.FillColor = Color.GreenYellow
RectangleShape1.FillGradientColor = Color.Green
End If
If LPeso.Text.Length <> 6 Then
LEspere.Visible = False
End If
End Sub
Private Sub BCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCerrar.Click
End
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
LEspere.Visible = False
End Sub
Private Sub BTerminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTerminar.Click
SerialPort1.BreakState = True
Text = ""
SerialPort1.Close()
RectangleShape1.FillColor = Color.Red
RectangleShape1.FillGradientColor = Color.DarkRed
BTerminar.Enabled = False
BIniciar.Enabled = True
End Sub
End Class
So i have this code that gets data through a serial port, and i want the button BTerminar to close the serial port so the user can change ports if they selected the wrong one, but when i click on it the application crashes. what im doing wrong?
View the full article
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form1
------------------------------------------------
Dim Puertos As Array
Delegate Sub SetTextCallback(ByVal [text] As String) Added to prevent threading errors during receiveing of data
------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Puertos = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(Puertos)
ComboBox2.SelectedIndex = 2
ComboBox1.SelectedIndex = 1
End Sub
------------------------------------------------
Private Sub ComboBox1_Click(sender As System.Object, e As System.EventArgs) Handles ComboBox1.Click
End Sub
------------------------------------------------
Private Sub BIniciar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BIniciar.Click
If ComboBox1.Text.Length > 1 And ComboBox2.Text.Length > 1 Then
SerialPort1.PortName = ComboBox1.Text
SerialPort1.BaudRate = ComboBox2.Text
SerialPort1.Open()
BIniciar.Enabled = False
BTerminar.Enabled = True
LEspere.Visible = True
Timer1.Enabled = True
ElseIf ComboBox1.Text.Length < 1 Then
MsgBox("Seleccione un puerto valido.", MsgBoxStyle.OkOnly, "atención")
ElseIf ComboBox2.Text.Length < 1 Then
MsgBox("Seleccione un valor de baudios.", MsgBoxStyle.OkOnly, "atención")
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String) input from ReadExisting
If Me.RichTextBox2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBox2.Text &= [text] append text
If Mid(text, 1, 1) = Chr(2) And Mid(text, 9, 1) = Chr(13) Then
LPeso.Text = text
End If
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If SerialPort1.IsOpen Then
RectangleShape1.FillColor = Color.GreenYellow
RectangleShape1.FillGradientColor = Color.Green
End If
If LPeso.Text.Length <> 6 Then
LEspere.Visible = False
End If
End Sub
Private Sub BCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCerrar.Click
End
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
LEspere.Visible = False
End Sub
Private Sub BTerminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTerminar.Click
SerialPort1.BreakState = True
Text = ""
SerialPort1.Close()
RectangleShape1.FillColor = Color.Red
RectangleShape1.FillGradientColor = Color.DarkRed
BTerminar.Enabled = False
BIniciar.Enabled = True
End Sub
End Class
So i have this code that gets data through a serial port, and i want the button BTerminar to close the serial port so the user can change ports if they selected the wrong one, but when i click on it the application crashes. what im doing wrong?
View the full article