Author Topic: VB2010 question - serial port communication  (Read 16900 times)

VBnovice96

  • Member
  • ***
  • Posts: 1
VB2010 question - serial port communication
« on: September 28, 2016, 03:46:11 am »
My project is to write a simple program that displays the target temperature in a textbox when you press a button

The sensor I am working with is the Optris CT sensor and here is the list of commands attached which you’ll find useful as it describes how it works.

Here is my code so far. I do not understand why this will not work.
Code: [Select]
Option Strict On

Imports System.IO.Ports

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each s In System.IO.Ports.SerialPort.GetPortNames()
            lstPorts.Items.Add(s)
        Next s
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        If lstPorts.SelectedIndex = -1 Then
            MsgBox("Please select a port")
            Exit Sub
        Else
            SerialPort1.BaudRate = 9600
            SerialPort1.DataBits = 8
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.StopBits = IO.Ports.StopBits.One
            'SerialPort1.PortName = "COM20"
            SerialPort1.Open()
            Dim b() As Byte = {1}
            SerialPort1.Write(b, 0, 1)
            Timer1.Start()
        End If
    End Sub

    Dim q As Queue(Of Byte) = New Queue(Of Byte)

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        While SerialPort1.BytesToRead > 0
            q.Enqueue(CByte(SerialPort1.ReadByte))
        End While
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Timer1.Tick
        SyncLock q
            While q.Count > 0
                Dim C As Byte = CByte(q.Dequeue)
                TextBox1.Text &= Hex(C) & " "
            End While
        End SyncLock
    End Sub
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        SerialPort1.Close()
        Timer1.Stop()
    End Sub
End Class


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: VB2010 question - serial port communication
« Reply #1 on: September 28, 2016, 02:38:34 pm »
Debugging tools such as breakpoints can help you isolate the problem.

For example, set a breakpoint in SerialPort1_DataReceived to find out if the the port is receiving data.

If not, using a protocol analyzer or other tool to find out whether the sensor is sending anything would provide useful information.

If the program stops at the breakpoint in SerialPort1_DataReceived, you can examine the received data for problems.

Also see:

http://stackoverflow.com/questions/8218249/how-do-i-enable-visual-studio-2010-to-break-when-a-first-chance-exception-happen