Very simple problem with VB.Net and Serial IO

After searching, I still have problems reading data from the serial port in VB.Net/VS2010. I know that the Serial Port works, I can write to the port normally, but reading it, nothing happens. I have only programmed the last 3 weeks, so I'm still trying to get around all this.

The program should be launched to collect data from the door recorder, after which I will output the data to the database (not yet implemented - I want to sort this part first). I tried to use several terminal programs, as well as another device that outputs data on a serial line, and nothing is displayed in the text box tbxIn.

Any help would be greatly appreciated.

Code below:

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
+3
1

, , . Serial1 . :

    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
+1

All Articles