Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Binary Reader Loop thru

Avatar of fastburst
fastburst asked on
.NET ProgrammingVisual Basic.NETVB Script
10 Comments1 Solution1007 ViewsLast Modified:
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()


            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)


            ' 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
                    skipcount = skipcount + 1
                End If
                skipcount = skipcount + 1
            End If
            test = (br.PeekChar() <> -1)
            test = (Not magicfound)
        End While
        If (magicfound = 1) Then
            Return skipcount
            Return (skipcount - 1)
        End If

    End Function
Avatar of shadow77

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answers