Binary Reader Loop thru

fastburst
fastburst used Ask the Experts™
on
I have a binary log file that i need to open and show the results in a RichTextBox so I can export the results in plain text. Now I have have the code in place and it reads it however it is only reading one section and doesnt pass thru the rest of the file like it should.

I have tried everything I can think of from For Each, Do, Do While etc etc and I cannot get it to work properly. Please Assist and show example code.
Public Shared magic_number As UShort
    Public Shared flag_block1 As ULong
    Public Shared flag_block2 As ULong
    Public Shared serial_no As String
    Public Shared bytes(0 To 9) As Byte
    Public Shared dig_fw_ver As UShort
    Public Shared lcd_fw_ver As UShort
    Public Shared sd_fw_ver As UShort
    Public Shared com1_fw_ver As UShort
    Public Shared com2_fw_ver As UShort
    Public Shared index As VariantType

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            'Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
            Dim fs As New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
            Dim br As New System.IO.BinaryReader(fs)
            fs.Position = 0 'sets the position to the beginning of the stream

            'While Not fs.EndOfStream

            magic_number = br.ReadUInt16()


            find_next_magic_number(br)

            flag_block1 = br.ReadUInt64
            flag_block2 = br.ReadUInt64
            bytes = br.ReadBytes(10)
            serial_no = System.Text.Encoding.UTF8.GetString(bytes).ToString
            dig_fw_ver = br.ReadUInt16
            lcd_fw_ver = br.ReadUInt16
            sd_fw_ver = br.ReadUInt16
            com1_fw_ver = br.ReadUInt16
            com2_fw_ver = br.ReadUInt16

            RichTextBox1.AppendText(magic_number & ",")
            RichTextBox1.AppendText(flag_block1 & ",")
            RichTextBox1.AppendText(flag_block2 & ",")
            RichTextBox1.AppendText(serial_no & ",")
            RichTextBox1.AppendText(dig_fw_ver & ",")
            RichTextBox1.AppendText(lcd_fw_ver & ",")
            RichTextBox1.AppendText(sd_fw_ver & ",")
            RichTextBox1.AppendText(com1_fw_ver & ",")
            RichTextBox1.AppendText(com2_fw_ver & vbCrLf)

            br.Close()
            fs.Close()

            ' byte (8 bit)
            ' ushort (16 bit)
            ' uinteger (32 bit)

        End If
    End Sub

    ' finds next magic number setting the position in the current 
    ' stream to the first byte after the magic number.  
    ' Returns the number of bytes skipped to get to the magic number
    ' or 0 if no bytes had to be skipped, -1 if end of file was reached, 
    ' -2 or less if bytes were skipped until the end of file.
    Public Shared Function find_next_magic_number(ByRef br As System.IO.BinaryReader) As Integer
        Dim skipcount As Integer = 0
        Dim magicfound As Integer = 0
        Dim test As Integer = 0
        While ((br.PeekChar() <> -1) And (magicfound = 0))
            If br.ReadByte() = &H55 Then
                If (br.PeekChar() <> -1) And br.ReadByte() = &HAA Then
                    magicfound = 1
                Else
                    skipcount = skipcount + 1
                End If
            Else
                skipcount = skipcount + 1
            End If
            test = (br.PeekChar() <> -1)
            test = (Not magicfound)
        End While
        If (magicfound = 1) Then
            Return skipcount
        Else
            Return (skipcount - 1)
        End If

    End Function

Open in new window

logged-data-sample-2.log
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
It's not clear what you're trying to do here.  Are you trying to display a chunk of your log file each time you click Button1?  If so, this code will always read only the first part of the file because Button1_Click always opens and then closes the file.

Where did you put the "For Each, Do, Do  While etc etc" to which you refer?

What is the magic number and what is its significance?

What are you trying to do?


Author

Commented:
I took out the for each and all that because I couldnt get it to work.
the magic_number is the unique identifier and is the start of each record.
 What i need it to do is after you click and open the file display everything and not just stop.

I tried doing a loop on hte magic number and when i did it would break and wouldn't read any further.

Commented:
I have loaded up your code and will try it.  Please show me where you want the loop and any other code.  I assume the file            logged-data-sample-2.log you attached will suffice as a test.

I need to take a break for a couple of hours for family matters.  Will get back to you if you haven't received a solution.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
The sample log file attached is the test log, should have 4 records in it.

It should do a loop thru the entire file and break out results after each magic_number. Using the code i provided you will see the results of the second record. not sure why it skips the first to begin with.
Commented:
Replace Button1_Click with this code (see comments).

What are you doing in Line 83?  It doesn't make sense to me.  Your comments do not agree with your return statements.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            'Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
            Dim fs As New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
            Dim br As New System.IO.BinaryReader(fs)
            fs.Position = 0 'sets the position to the beginning of the stream

            'While Not fs.EndOfStream

            ' this positions you after the first maguc number, so find_next_magic_number moves to the second, skipping the first record
            'magic_number = br.ReadUInt16()
            magic_number = &55AA


            Do While True
                Dim skipCount As Integer = find_next_magic_number(br)
                If skipCount < 0 Then Exit Do ' Add code for error conditions
                If fs.Length - fs.Position < 32 Then Exit Do ' Make sure you have a full record to read

                flag_block1 = br.ReadUInt64
                flag_block2 = br.ReadUInt64
                bytes = br.ReadBytes(10)
                serial_no = System.Text.Encoding.UTF8.GetString(bytes).ToString
                dig_fw_ver = br.ReadUInt16
                lcd_fw_ver = br.ReadUInt16
                sd_fw_ver = br.ReadUInt16
                com1_fw_ver = br.ReadUInt16
                com2_fw_ver = br.ReadUInt16

                RichTextBox1.AppendText(magic_number & ",")
                RichTextBox1.AppendText(flag_block1 & ",")
                RichTextBox1.AppendText(flag_block2 & ",")
                RichTextBox1.AppendText(serial_no & ",")
                RichTextBox1.AppendText(dig_fw_ver & ",")
                RichTextBox1.AppendText(lcd_fw_ver & ",")
                RichTextBox1.AppendText(sd_fw_ver & ",")
                RichTextBox1.AppendText(com1_fw_ver & ",")
                RichTextBox1.AppendText(com2_fw_ver & vbCrLf)
            Loop

            br.Close()
            fs.Close()

            ' byte (8 bit)
            ' ushort (16 bit)
            ' uinteger (32 bit)

        End If
    End Sub

Open in new window

Author

Commented:
On line 13 of your code: magic_number = &55AA
I get an error right after &55AA (Expression Expected.)

AS far as line 83 goes on the that function it was pre-written.

Commented:
Sorry; that line was an afterthought.  It should be
    magic_number = &H55AA
(forgot the H).

Author

Commented:
Okay I was finally able to test and see wha tyou did, however it does work but the magic_number should be as follows:

magic_number= &HAA55
but
I need it to use magic_number = br.ReaUdInt16()

when we make it use that it only shows 3 records instead of 4, any ideas?
Commented:
Right; I forget that Intel does things backwards (http://en.wikipedia.org/wiki/Endianness).

Yes.  When you do
     magic_number = br.ReaUdInt16()
you position the reader after the magic number.  Thus when you do
    find_next_magic_number(br)
you start searching at byte 2 and you skip the rest of the first record.

To fix this, move
    fs.Position = 0
after
     magic_number = br.ReaUdInt16()

Author

Commented:
Thanks shadow77 that did it!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial