将当前与先前的串行数据visual basic进行比较 [英] Compare current with previous serial data visual basic

查看:73
本文介绍了将当前与先前的串行数据visual basic进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码



Here is the code

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







我的串行数据设备是速度传感器。所以你可以看到我得到一个连续的速度测量。每一条新线都是新的速度。我想要做的是将每一个新线与前一个线进行比较,看看是否有任何变化。



例如:



如果ritchtextbox.newline<> richtextbox.previousline然后

做点什么....

结束如果



类似的东西..



我尝试过的事情:



i尝试过做同样的事情数组或数据gridview但我没有得到任何结果。我不知道如何语法...任何syggestion将是非常有帮助的。谢谢您的时间。




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.

推荐答案

问题是您的应用程序不会将信息存储在数据模型中。您使用显示/编辑控件作为数据容器,这不是一个好习惯。这也是让你很难进行这种比较的原因。


在你的代码中创建一个数据模型来存储数据。使用RichTextBox控件显示数据或任何您想要的控件。切勿使用控件作为数据模型。
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.


这篇关于将当前与先前的串行数据visual basic进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆