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.
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