Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

Optimize & Handle Serial Port Reading Data

$
0
0
Dear Everyone!
I'm a starter in vb.
My project is reading data sent to com port from multiple devices connected to it. I wrote my code and it works good, but it seems it can be better and I want to optimize it if any one can suggest different way...
Let me to explain the Project:

The devices connected to serial port and sending string in this form "XnmY"
The First "X" and last "Y" are fixed and shows starting and ending of string.
The character "n" shows the device ID and can be "a" to "z".
The "m" character is device state and can be "A" to "D".
For example when you read the string "XfBY" from the port it means the device "f" is in "B" state.

In the facts the devices are terminals to gather votes from 26 terminal ("a" to "z" indicated with 2nd character of string) When a device send self state to port it shows which button is pressed. The 3rd character of string indicates the button. If it is "A" it means the noting pressed, "B"=Agree "C"= Dis Agree and "D"=neutral.

Any suggestion is welcome!
Now lets take a look at my code:

Code:


    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, _
                                        ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                        Handles SerialPort1.DataReceived
        If SerialPort1.IsOpen Then
            Try
                readbuffer = SerialPort1.ReadLine()
                If Not SerialPort1.ReadLine(2) = "A" Then Me.Invoke(New EventHandler(AddressOf DoUpdate))
            Catch ex As Exception
                MsgBox("read " & ex.Message)
            End Try
        End If
    End Sub

Code:


 Public Sub DoUpdate(ByVal sender As Object, ByVal e As System.EventArgs)
        i = 1
        Dim charray() As Char = readbuffer.ToCharArray
        Select Case charray(1)
            Case "a"
                If VoteResult(0) = 0 Then
                    AllVotes += 1
                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(0) = 1
                    Case "C"
                        VoteResult(0) = 2
                    Case "D"
                        VoteResult(0) = 3
                End Select
            Case "b"
                If VoteResult(1) = 0 Then
                    AllVotes += 1
                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(1) = 1
                    Case "C"
                        VoteResult(1) = 2
                    Case "D"
                        VoteResult(1) = 3
                End Select
            Case "c"
                If VoteResult(2) = 0 Then
                    AllVotes += 1
                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(2) = 1
                    Case "C"
                        VoteResult(2) = 2
                    Case "D"
                        VoteResult(2) = 3
                End Select
            Case "d"
                If VoteResult(3) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(3) = 1
                    Case "C"
                        VoteResult(3) = 2
                    Case "D"
                        VoteResult(3) = 3
                End Select
            Case "e"
                If VoteResult(4) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(4) = 1
                    Case "C"
                        VoteResult(4) = 2
                    Case "D"
                        VoteResult(4) = 3
                End Select
            Case "f"
                If VoteResult(5) = 0 Then
                    VoteResult(5) = 4
                    AllVotes += 1
                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(5) = 1
                    Case "C"
                        VoteResult(5) = 2
                    Case "D"
                        VoteResult(5) = 3
                End Select
            Case "g"
                If VoteResult(6) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(6) = 1
                    Case "C"
                        VoteResult(6) = 2
                    Case "D"
                        VoteResult(6) = 3
                End Select
            Case "h"
                If VoteResult(7) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(7) = 1
                    Case "C"
                        VoteResult(7) = 2
                    Case "D"
                        VoteResult(7) = 3
                End Select
            Case "i"
                If VoteResult(8) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(8) = 1
                    Case "C"
                        VoteResult(8) = 2
                    Case "D"
                        VoteResult(8) = 3
                End Select
            Case "j"
                If VoteResult(9) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(9) = 1
                    Case "C"
                        VoteResult(9) = 2
                    Case "D"
                        VoteResult(9) = 3
                End Select
            Case "k"
                If VoteResult(10) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(10) = 1
                    Case "C"
                        VoteResult(10) = 2
                    Case "D"
                        VoteResult(10) = 3
                End Select
            Case "l"
                If VoteResult(11) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(11) = 1
                    Case "C"
                        VoteResult(11) = 2
                    Case "D"
                        VoteResult(11) = 3
                End Select
            Case "m"
                If VoteResult(12) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(12) = 1
                    Case "C"
                        VoteResult(12) = 2
                    Case "D"
                        VoteResult(12) = 3
                End Select
            Case "n"
                If VoteResult(13) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(13) = 1
                    Case "C"
                        VoteResult(13) = 2
                    Case "D"
                        VoteResult(13) = 3
                End Select
            Case "o"
                If VoteResult(14) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(14) = 1
                    Case "C"
                        VoteResult(14) = 2
                    Case "D"
                        VoteResult(14) = 3
                End Select
            Case "p"
                If VoteResult(15) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(15) = 1
                    Case "C"
                        VoteResult(15) = 2
                    Case "D"
                        VoteResult(15) = 3
                End Select
            Case "q"
                If VoteResult(16) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(16) = 1
                    Case "C"
                        VoteResult(16) = 2
                    Case "D"
                        VoteResult(16) = 3
                End Select
            Case "r"
                If VoteResult(17) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(17) = 1
                    Case "C"
                        VoteResult(17) = 2
                    Case "D"
                        VoteResult(17) = 3
                End Select
            Case "s"
                If VoteResult(18) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(18) = 1
                    Case "C"
                        VoteResult(18) = 2
                    Case "D"
                        VoteResult(18) = 3
                End Select
            Case "t"
                If VoteResult(19) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(19) = 1
                    Case "C"
                        VoteResult(19) = 2
                    Case "D"
                        VoteResult(19) = 3
                End Select
            Case "u"
                If VoteResult(20) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(20) = 1
                    Case "C"
                        VoteResult(20) = 2
                    Case "D"
                        VoteResult(20) = 3
                End Select
            Case "v"
                If VoteResult(21) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(21) = 1
                    Case "C"
                        VoteResult(21) = 2
                    Case "D"
                        VoteResult(21) = 3
                End Select
            Case "w"
                If VoteResult(22) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(22) = 1
                    Case "C"
                        VoteResult(22) = 2
                    Case "D"
                        VoteResult(22) = 3
                End Select
            Case "x"
                If VoteResult(23) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(23) = 1
                    Case "C"
                        VoteResult(23) = 2
                    Case "D"
                        VoteResult(23) = 3
                End Select
            Case "y"
                If VoteResult(24) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(24) = 1
                    Case "C"
                        VoteResult(24) = 2
                    Case "D"
                        VoteResult(24) = 3
                End Select
            Case "z"
                If VoteResult(25) = 0 Then
                    AllVotes += 1

                End If
                Select Case charray(2)
                    Case "B"
                        VoteResult(25) = 1
                    Case "C"
                        VoteResult(25) = 2
                    Case "D"
                        VoteResult(25) = 3
                End Select
        End Select
        VotesSumLable.Text = AllVotes
        RemainLable.Text = PresentVoter - AllVotes

        ComBufferLable.Text = readbuffer


Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>