visual surge net informacion descargar descarga conceptos como vb.net serial-port

vb.net - surge - visual basic vb net download



Problema muy básico de VB.Net y Serial IO (1)

Después de buscar, sigo teniendo problemas para leer datos de un puerto serie en VB.Net/VS2010. Sé que el puerto serial funciona, puedo escribir bien en el puerto, pero al leerlo, no pasa nada. Solo he programado durante las últimas 3 semanas, así que todavía estoy tratando de entenderlo.

El programa debe ejecutarse para capturar los datos de un registrador de puerta, y luego enviaré los datos a una base de datos (aún no implementada, quiero que esta parte se ordene primero). He intentado utilizar varios programas de terminal, así como otro dispositivo que genera datos en la línea serie, sin mostrar nada en el cuadro de texto tbxIn .

Cualquier ayuda sería muy apreciada.

El código está abajo:

Imports System.IO.Ports Imports System.IO.Ports.SerialPort Public Class Form1 Dim comPort As IO.Ports.SerialPort = Nothing Dim sComPort As String = "" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load GetSerialPortNames() End Sub Sub GetSerialPortNames() '' Show all available COM ports. For Each sp As String In My.Computer.Ports.SerialPortNames lstPorts.Items.Add(sp) Next End Sub Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged sComPort = lstPorts.SelectedItem Button1.Enabled = True End Sub Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '' Open the serial port using the OpenSerialPort method Button1.Enabled = False Button2.Enabled = True Try comPort = My.Computer.Ports.OpenSerialPort(sComPort, 9600, IO.Ports.Parity.None, 8, 1) '' comPort.DtrEnable = True comPort.ReadTimeout = 500 Do comPort.WriteLine("Go") Dim sIncomming As String = comPort.ReadLine() tbxIn.Text = sIncomming & vbCrLf Loop Catch ex As TimeoutException tbxIn.Text &= "Error: Serial Port Read Timeout" & vbCrLf End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click comPort.Close() Button1.Enabled = True Button2.Enabled = False End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived tbxIn.Text = e.ToString End Sub End Class


De seguro, esto le dará lo que necesita. Usted NO necesita el componente Serial1 en el diseñador. Eliminar eso y usar este código:

Private comPort As IO.Ports.SerialPort = Nothing Private sComPort As String = "" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) GetSerialPortNames() End Sub Sub GetSerialPortNames() '' Show all available COM ports. For Each sp As String In My.Computer.Ports.SerialPortNames lstPorts.Items.Add(sp) Next End Sub Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged sComPort = lstPorts.SelectedItem Button1.Enabled = True End Sub Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '' Open the serial port using the OpenSerialPort method Button1.Enabled = False Button2.Enabled = True Try comPort = My.Computer.Ports.OpenSerialPort(sComPort, 9600, IO.Ports.Parity.None, 8, 1) '' comPort.DtrEnable = True ''must add handler AddHandler comPort.DataReceived, AddressOf SerialPort1_DataReceived comPort.ReadTimeout = 500 Do comPort.WriteLine("Go") Dim sIncomming As String = comPort.ReadLine() tbxIn.Text = sIncomming & vbCrLf Loop Catch ex As TimeoutException tbxIn.Text &= "Error: Serial Port Read Timeout" & vbCrLf End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click comPort.Close() ''remove handler RemoveHandler comPort.DataReceived, AddressOf SerialPort1_DataReceived Button1.Enabled = True Button2.Enabled = False End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) tbxIn.Text = e.ToString End Sub