Question Comport reads errors after number of good reads...

SimonTempler

New member
Joined
Jan 28, 2009
Messages
2
Programming Experience
Beginner
Hi All, any input would be appreciated.

I have just dabbled with VB in the past so excuse my ignorance if you see something obvious.

I have a hardware which sends out packets containing 17 bytes to the comport in bursts of 3. the little test VB that I have written receives and displays the bytes in a line of 17 bytes long and works fine until it gets to around 170th packet, then it starts to mix up (looks like shifting the data left) the bytes shift mostly by one digit sometimes by two. Then it will continue to give almost a packet correct and a packet shifted for about 50 packets. Then it will correct itself for a long while, but then the speed at which it receives drops.

All the packets are sent 3 times, and before things go wrong (within the first 170 packets) the packet counter shows that it receives 3 packet each time but after the problem it goes to 2 packet per burst and then to one.If i close my VB monitoring program say at packet 165 and immediately open it again, it wont go wrong for another 170 packets, this points the finger at VB. I have check my hardware, there is no problem with that.

The program reads port - waits for 17 bytes - once 17 has been received it will go and buffer the packet in an array. In fact they are 2 arrays but printing fro either results in the same problem.

I have tried whatever i could think of but to no avail, i am wondering if some kind of house keeping needs to be done to clear something... I am sorry the program is a bit messy but I have been making many changes to find out what the problem is - my code

below:
VB.NET:
Public Class Form1

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

        'Call Display_Word()
        'checks to see if you have default com port available
        Try
            SerialPort12.Open()
        Catch ex As Exception
            MessageBox.Show("Unable To Open Comm Port")
        End Try
        SerialPort12.DiscardInBuffer()
        'SerialPort12.DiscardInBuffer()

        N_Pkts = 1
    End Sub

    'data received event
    Private Sub SerialPort12_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort12.DataReceived

        'fires when data is received in the input buffer
        'If SerialPort12.BytesToRead < 17 Then
        '    Exit Sub
        'End If
        'myRxData(SerialPort12.ReadByte)
        'number of bytes to rx before start processing
        ComByts = 16

        If SerialPort12.BytesToRead >= 17 Then
            Do
                CheckForIllegalCrossThreadCalls = False
                myRxData(SerialPort12.ReadByte)
                If SerialPort12.BytesToRead = 0 Then

                    Exit Do
                End If
            Loop
        End If
        SerialP_In_Counter = 0
    End Sub
    'check incoming stream and store them in array.
    Private Sub myRxData(ByVal mydata As Integer)
        'Disables errors when calling functions from different threads
        'not the best way but suitable for our purpose.....
        CheckForIllegalCrossThreadCalls = False

        'pass the data to the array.
        Serial_Data(SerialP_In_Counter) = mydata
        If SerialP_In_Counter = ComByts Then

            'display packet numbers.
            TextBox19.Text = N_Pkts

            'go buffer data.
            Call Buffer_data()

            'go filter Word and Bytes
            'Call BW_Convert()

            'printing process.
            For PrintS_Counter As Byte = 0 To 16     'counts words (UShort)
                'TextBox1.Text &= Serial_Data(PrintS_Counter) & Chr(13) & Chr(10)
                ''TextBox1.Text &= My_Proced_Short(PrintS_Counter) & Chr(13) & Chr(10)
                'TextBox1.SelectionStart = TextBox1.TextLength
                'TextBox1.ScrollToCaret()
                TextBox1.Text &= SerialP_In(PrintS_Counter) & " " ' & Chr(13) & Chr(10)
                TextBox1.SelectionStart = TextBox1.TextLength
                TextBox1.ScrollToCaret()
            Next
            TextBox1.Text &= Chr(13) & Chr(10)
            TextBox1.SelectionStart = TextBox1.TextLength
            TextBox1.ScrollToCaret()
            'TextBox1.Text &= "========" & Chr(13) & Chr(10)
            'TextBox1.SelectionStart = TextBox1.TextLength
            'TextBox1.ScrollToCaret()

            'For PrintB_Counter As Byte = 0 To 2     'counts bytes

            '    TextBox18.Text &= My_Proced_Bytes(PrintB_Counter) & Chr(13) & Chr(10)
            '    TextBox18.SelectionStart = TextBox18.TextLength
            '    TextBox18.ScrollToCaret()
            'Next


            'reset array counter
            SerialP_In_Counter = 0
            Array.Clear(SerialP_In, 0, 16)
            Array.Clear(Serial_Data, 0, 16)
            'SerialPort12.DiscardInBuffer()
            Exit Sub
        End If
        'if array is not filled increase it
        If SerialP_In_Counter < ComByts Then
            SerialP_In_Counter += 1

        End If
        'TextBox1.Text &= mydata '& Chr(13) & Chr(10)
    End Sub
    'buffer the live straming data in to an array.
    Sub Buffer_data()
        Dim data_counter As Integer
        For data_counter = 0 To ComByts
            SerialP_In(data_counter) = Serial_Data(data_counter)
        Next
        'Serial_Data.
        N_Pkts += 1

        'SerialPort12.DiscardInBuffer()
    End Sub
    '====================================================
    'this part is not used at the moment
    '====================================================

    'convert data stream stored in the array to Short(Word) and Bytes.
    Sub BW_Convert()
        'local counter for Word variables.
        Dim pack As Byte = 0
        'convert the first 7 Words out of 14 bytes.
        For data_counter = 0 To 13 Step 2 'step index.
            'converts the array of bytes to shorts.
            My_Proced_Short(pack) = BitConverter.ToUInt16(Serial_Data, data_counter)
           
            'make sure it stays within 5.
            If pack < 6 Then
                pack += 1
            End If

        Next
        ''set array to the righ index to start processing bytes 1-14 are shorts, 15-17 are bytes.
        pack = 14
        'counter for the remaining bytes.
        For data_counter = 0 To 2
            My_Proced_Bytes(data_counter) = Serial_Data(pack)
            If pack <= ComByts Then
                pack += 1
            End If
        Next
        'pack = ComByts

    End Sub


    'TX not used.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'type something into textbox2 to transmit
        'SerialPort12.Write(TextBox2.Text & vbCrLf)

    End Sub
    'clear veiwing textboxes.
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Clear()
        TextBox18.Clear()
    End Sub
    'discard serial buffer.
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        SerialPort12.DiscardInBuffer()
    End Sub
    'clear RTB and reset number of packets.
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox19.Clear()
        N_Pkts = 1
    End Sub
End Class
 
Top Bottom