1

基本上,我被赋予了从前一个学生继续的任务。我有几个传感器可以检测实验室的温度、湿度和亮度。这些来自传感器的数据需要传输到协调器。我能够调试程序,但无法达到 GUI 中显示的结果。读数值无法传输。以下是前一个学生给出的代码,他们能够实现它。

可能是什么问题呢??我怀疑它可能是串行端口问题。

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Windows.Forms.DataGridView

Public Class frmGUI

Dim FromSerialPort
Dim dSet As New DataSet
Dim COORDINATOR0 = 0
Dim strData(0 To 1)
Dim data As String
Dim count As Integer = 0
Dim number As Integer
Dim data2 As String
Dim x As Integer = 0

Dim txt1 As Integer = 0
Dim txt2 As Integer = 0
Dim txt3 As Integer = 0
Dim txt4 As Integer = 0

Dim hour As String = 0
Dim min As String = 0
Dim sec As String = 0
Dim hour2 As String = 0
Dim min2 As String = 0
Dim sec2 As String = 0

Dim LumVal1 As Integer

Delegate Sub SetTextCallback(ByVal [text] As String)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If SerialPort1.IsOpen Then
        SerialPort1.Close()
    End If

    System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\Welcome.vbs")

    strData(0) = "ZED02"

    TSSL1.Text = Format(Now, "Short Date")
    TSSL2.Text = "Welcome"
    TSSL3.Text = Format(Now, "Long Time")

    PictureBox1.Image = System.Drawing.Bitmap.FromFile( _
  My.Application.Info.DirectoryPath & "\Capture.PNG")

    txtCOM.Focus()


End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

    Dim intResponse As Integer
    intResponse = MsgBox(" Are u sure u want to QUIT ??!!", vbYesNo + vbExclamation, "Quit")
    If intResponse = vbYes Then
        End

    End If

End Sub


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick

    TSSL3.Text = Format(Now, "Long Time")


End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Tick

    If count = 1 Then
        sec = sec + 1
        If sec = 10 Then
            sec = 0
            sec2 = sec2 + 1
            If sec2 = 6 Then
                sec2 = 0
                min = min + 1
                If min = 10 Then
                    min = 0
                    min2 = min2 + 1
                    If min2 = 6 Then
                        min2 = 0
                        hour = hour + 1
                        If hour = 10 Then
                            hour = 0
                            hour2 = 1
                            If hour = 4 And hour2 = 2 Then
                                hour = 0
                                hour2 = 0
                            End If
                        End If
                    End If
                End If
            End If
        End If
        TSSL2.Text = ("Connected : " + hour2 + hour + ":" + min2 + min + ":" + sec2 + sec)
    End If

End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click


    If txtCOM.Text = "" Then
        'MsgBox("Please enter a COM port value", vbOKOnly & vbExclamation, "Error")
        txtCOM.Text = "34"
    Else
        count = count + 1

    End If
    If (count = 1) Then

        System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\Connected.vbs")

        txtCOM.Enabled = False

        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If

        SerialPort1.PortName = "COM" & txtCOM.Text ' for the coordinator
        SerialPort1.BaudRate = 38400
        SerialPort1.Parity = System.IO.Ports.Parity.None
        SerialPort1.DataBits = 8
        SerialPort1.StopBits = System.IO.Ports.StopBits.One
        SerialPort1.RtsEnable = True
        SerialPort1.Open()


        hour = 0
        min = 0
        sec = 0

        hour2 = 0
        min2 = 0
        sec2 = 0

        lbl1.Text = " Reading Data . . ."
        TextBox5.Text &= "Connected at: " + Format(Now, "Short Time") + vbNewLine + vbNewLine
        btnConnect.Text = "Disconnect"
    End If

    If count = 2 Then

        System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\Disconnected.vbs")

        txtCOM.Enabled = True

        count = 0

        btnConnect.Text = " Connect "
        SerialPort1.Close()             'Close our Serial Port

        lbl1.Text = " Not Connected "
        TSSL2.Text = "Disconnected"

        TextBox5.Text &= "Disconnected at: " + Format(Now, "Short Time") + vbNewLine + vbNewLine

    End If


End Sub


Private Sub ReceivedText(ByVal [text] As String)


    If Me.TextBox5.InvokeRequired Then
        Dim x As New SetTextCallback(AddressOf ReceivedText)
        Me.Invoke(x, New Object() {(text)})
    Else

        data &= [text]

        If InStr(data, "Data not received") Then
            data = ""
            TextBox5.Text = "Data lost" + vbNewLine
        End If

        If InStr(data, "Lab Location: S.429") Then
            If InStr(data, "TemperatureValue:") Then
                If InStr(data, "Humidityvalue:") Then
                    If InStr(data, "LuminosityValue:") Then
                        txt1 = 1

                        txt2 = 0
                        txt3 = 0
                        txt4 = 0

                        data2 = data
                    End If
                End If
            End If
        End If

        If InStr(data, "Lab Location: S.434") Then
            If InStr(data, "Temperaturevalue:") Then
                If InStr(data, "HumidityValue:") Then
                    If InStr(data, "LuminosityValue:") Then
                        txt2 = 1

                        txt1 = 0
                        txt3 = 0
                        txt4 = 0

                        data2 = data
                    End If
                End If
            End If
        End If

        If InStr(data, "Lab Location: S.439") Then
            If InStr(data, "TemperatureValue:") Then
                If InStr(data, "HumidityValue:") Then
                    If InStr(data, "LuminosityValue:") Then
                        txt3 = 1

                        txt1 = 0
                        txt2 = 0
                        txt4 = 0

                        data2 = data
                    End If
                End If
            End If
        End If

        If InStr(data, "Lab Location: S.449") Then
            If InStr(data, "TemperatureValue:") Then
                If InStr(data, "HumidityValue:") Then
                    If InStr(data, "LuminosityValue:") Then
                        txt4 = 1

                        txt1 = 0
                        txt2 = 0
                        txt3 = 0

                        data2 = data
                    End If
                End If
            End If
        End If

        If txt1 = 1 Then
            TextBox1.Text &= data2
            TextBox5.Text &= "Data received from S.429 at: " + Format(Now, "Short Time") + vbNewLine + vbNewLine
            System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\data429.vbs")
            txt1 = txt1 + 1

            If txt1 = 2 Then
                txt1 = 0
                data = ""
            End If
        End If


        If txt2 = 1 Then
            TextBox2.Text &= data2
            TextBox5.Text &= "Data received from S.434 at: " + Format(Now, "Short Time") + vbNewLine + vbNewLine
            System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\data434.vbs")
            txt2 = txt2 + 1

            If txt2 = 2 Then
                txt2 = 0
                data = ""
            End If
        End If



        If txt3 = 1 Then
            TextBox3.Text &= data2
            TextBox5.Text &= "Data received from S.439 at: " + Format(Now, "Short Time") + vbNewLine + vbNewLine
            System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\data439.vbs")
            txt3 = txt3 + 1

            If txt3 = 2 Then
                txt3 = 0
                data = ""
            End If
        End If

        If txt4 = 1 Then
            TextBox4.Text &= data2
            TextBox5.Text &= "Data received from S.449 at: " + Format(Now, "Short Time") + vbNewLine + vbNewLine
            System.Diagnostics.Process.Start(My.Application.Info.DirectoryPath & "\data449.vbs")
            txt4 = txt4 + 1

            If txt4 = 2 Then
                txt4 = 0
                data = ""
            End If
        End If

    End If

End Sub

Private Sub TextBox1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    TextBox1.Clear()
End Sub

Private Sub TextBox2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    TextBox2.Clear()
End Sub

Private Sub TextBox3ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    TextBox3.Clear()
End Sub

Private Sub TextBox4ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    TextBox4.Clear()
End Sub

Private Sub TextBox5ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    TextBox5.Clear()
End Sub

Private Sub AllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    TextBox1.Clear()
    TextBox2.Clear()
    TextBox3.Clear()
    TextBox4.Clear()
    TextBox5.Clear()
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox1.Select(TextBox1.Text.Length, 0)
    TextBox1.ScrollToCaret()
End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    TextBox2.Select(TextBox2.Text.Length, 0)
    TextBox2.ScrollToCaret()
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    TextBox3.Select(TextBox3.Text.Length, 0)
    TextBox3.ScrollToCaret()
End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
    TextBox4.Select(TextBox4.Text.Length, 0)
    TextBox4.ScrollToCaret()
End Sub

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
    TextBox5.Select(TextBox5.Text.Length, 0)
    TextBox5.ScrollToCaret()
End Sub

Private Sub S429ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles S429ToolStripMenuItem.Click
    TextBox1.Clear()
End Sub

Private Sub SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SToolStripMenuItem.Click
    TextBox2.Clear()
End Sub

Private Sub S439ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles S439ToolStripMenuItem.Click
    TextBox3.Clear()
End Sub

Private Sub S449ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles S449ToolStripMenuItem.Click
    TextBox4.Clear()
End Sub

Private Sub BackLogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackLogToolStripMenuItem.Click
    TextBox5.Clear()
End Sub

Private Sub AllToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllToolStripMenuItem1.Click
    TextBox1.Clear()
    TextBox2.Clear()
    TextBox3.Clear()
    TextBox4.Clear()
    TextBox5.Clear()
End Sub

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
    MsgBox(" NYP SMART LAB " + vbNewLine + " By: Daniel and Mikaris ", vbQuestion, " About ")
End Sub

Private Sub HowToReadValuesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HowToReadValuesToolStripMenuItem.Click
    MsgBox("Temperature value < 26*C = Aircon is on" + vbNewLine + "Luminosity value > 1 = Lights are on" + vbNewLine, vbQuestion, "How to read values")
End Sub
End Class
4

0 回答 0