Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code

VB
Imports System Imports System.IO.Ports

Public Class Form1
Dim comPORT As String
Dim receivedData As String = ""



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Enabled = False
    comPORT = ""
    For Each sp As String In My.Computer.Ports.SerialPortNames
        comPort_ComboBox.Items.Add(sp)
    Next
End Sub


Private Sub comPort_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
    If (comPort_ComboBox.SelectedItem <> "") Then
        comPORT = comPort_ComboBox.SelectedItem
    End If
End Sub


Private Sub connect_BTN_Click(sender As Object, e As EventArgs) Handles connect_BTN.Click
    If (connect_BTN.Text = "Connect") Then
        If (comPORT <> "") Then
            SerialPort1.Close()
            SerialPort1.PortName = comPORT
            SerialPort1.BaudRate = 9600
            SerialPort1.DataBits = 8
            SerialPort1.Parity = Parity.None
            SerialPort1.StopBits = StopBits.One
            SerialPort1.Handshake = Handshake.None
            SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
            SerialPort1.ReadTimeout = 10000

            SerialPort1.Open()
            connect_BTN.Text = "Dis-connect"
            Timer1.Enabled = True
            Timer_LBL.Text = "Timer: ON"
        Else
            MsgBox("Select a COM port first")
        End If
    Else
        SerialPort1.Close()
        connect_BTN.Text = "Connect"
        Timer1.Enabled = False
        Timer_LBL.Text = "Timer: OFF"
    End If


End Sub


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    receivedData = ReceiveSerialData()
    RichTextBox1.Text &= receivedData
End Sub


Function ReceiveSerialData() As String
    Dim Incoming As String
    Try
        Incoming = SerialPort1.ReadExisting()
        If Incoming Is Nothing Then
            Return "nothing" & vbCrLf
        Else
            Return Incoming
        End If
    Catch ex As TimeoutException
        Return "Error: Serial Port read timed out."
    End Try

End Function



Private Sub clear_BTN_Click(sender As Object, e As EventArgs) Handles clear_BTN.Click
    RichTextBox1.Text = ""
End Sub



My serial data device is a speed sensor.So as you can see i m getting a continuouasly speed measure. Every new line is a new coming in speed. What i m trying to do is to compare every new line with the previous one to see if there is any change.

For example :

if ritchtextbox.newline <> richtextbox.previousline then
do something....
end if

something like that..

What I have tried:

i have tried do the same with an array or data gridview but i didnt get ny result. I dont know how to syntax that.. Any syggestion will be very helpful. Thank you for your time.
Posted
Updated 11-Jan-17 4:17am

1 solution

The problem is that you're app doesn't store the information in a data model. You're using a display/edit control as your data container and that's not good practice. It's also what's making it very difficult for you to do this comparison.

Create a data model in your code to store the data. Use the RichTextBox control to show the data, or whatever control you want. NEVER use a control as your data model.
 
Share this answer
 
Comments
Member 11475482 11-Jan-17 10:35am    
thank you so much. Your advicw was very usefull

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900