EDN Admin
Well-known member
I have written code to read and write to a serial port. I need it to indefinitely loop, I have been unable to figure a way to place a button to stop the loop, it will stop if there are errors.
I have searched the internet and have tried peoples suggestions, but yet to get one actually work for me.
I am also unsure if the display data is updating frequently enough. This is my first time to use shapes.
I am still new to all this.
Thanking you in advance,
YvetteImports System.IO.Ports
Class form1
==CONTROL CHARACTERS- as per spec==
==start and stop values==
Dim STX As Byte = &H2
Dim ETX As Byte = &H3
==Read==
Dim read As String = "R"
==Acknowledgment==
Dim ACK As Byte = &H6
==class and address==
Dim DeviceClass As String = "E"
Dim DeviceAddress As String = "1"
==Host Commane==
Dim hostCommand As String
==STX E 1 R REG1 REG0 ETX==
==Command to read==
Dim readSlave As String
==STX E 1 ACK REG1 REG0 D1 D0 ETX==
==array of register values==
Dim REG = New String() {"22", "23", "2F", "30"}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
==set and open port==NB not using get portname- COM1 to be used as dedicated port as spec==
Private Sub btnStartReset_Click(sender As Object, e As EventArgs) Handles btnStartReset.Click
If SerialPort1.IsOpen = False Then
==Open and set COM1 as host==
Try
==Set COM1 as portname==
SerialPort1.PortName = "COM1"
==Port settings==
SerialPort1.BaudRate = 9600
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.DataBits = 8
SerialPort1.ReadTimeout = 100
==Open port==
SerialPort1.Open()
rtbCom1.Text = "COM1 Ready"
tmrPoll.Start()
Catch ex As Exception
rtbCom1.Text = "open error " & ex.Message
End Try
End If
End Sub
Private Sub tmrPoll_Tick(sender As Object, e As EventArgs) Handles tmrPoll.Tick
==timeout error counter==
Dim i As Integer = 0
==Prevent unnecessary timeout errors/allow time lag for port to open==
Do While SerialPort1.IsOpen = True
==Loop through Register==
For Each register In REG
==STX E 1 R REG1 REG0 ETX==
hostCommand = (STX & DeviceClass & DeviceAddress & read & register & ETX)
Try
==Loop Host Commands for Register==
SerialPort1.WriteLine(hostCommand)
Catch ex As Exception
rtbCom1.Text = "Write Error: " & ex.Message
End Try
Try
==readline to separate data==
readSlave = SerialPort1.ReadLine()
==display data in GUI==
lst1.Items.Add(readSlave)
==Get Register Value==
==STX E 1 ACK REG1 REG0 D1 D0 ETX==
==2-E-1-6-R-R-D-D-3==
==Get the Data Value for Individual Register==
Dim reg = readSlave.Substring(4, 2)
==convert data to integer, so data can be displayed graphically==
Dim D1 = CInt(readSlave.Substring(6, 1))
Dim D0 = CInt(readSlave.Substring(7, 1))
==Display received substring values==
Select Case reg
Case Is = "22"
list box until advised.
lst1.Items.Add(reg & D1 & D0)
Case Is = "23"
==display data as shape==
==0-100==
shpTemp.Width = (D1 + D0)
Case Is = "2F"
==0-5==
shpAmp.Width = (D1 + D0) * 20
Case Is = "30"
==0-40==
shpVolt.Width = (D1 + D0) * 2.5
End Select
Catch ex As Exception
rtbCom1.Text = "Read error: " & ex.Message
i += 1
End Try
If i > 2 Then
rtbCom1.Text = "Operation Aborted: 3 timeout errors."
==Stop program if 3 timeout errors- as spec/closed port==
SerialPort1.Close()
rtbCom1.Text = "port closed - Operation Aborted: 3 timeout errors."
shpAmp.Width = 1
shpTemp.Width = 1
shpVolt.Width = 1
tmrPoll.Stop()
Exit Do
End If
Next
Loop
End Sub
End Class
If at first you dont succeed = true then try try again catch mistakes and never end
View the full article