Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need some tricks in VB handling

Status
Not open for further replies.
Hi guys.I'm trying to write a program which is kind of like a simple Flash memory programmer.I can read the *.BIN file into an array of byte buffer variables.And I can display the content of the BIN file in a text box.But the process is really slow and it even freezes the entire PC.What I did first is assigning each byte the respective value read from the file.Just like the last segment of the code listed down there.This process takes about 3 seconds to open a file with a size of 261KB,and the computer freezes during this period.

To display the content I used samilar structure as this:

TextName.Text = TextName.Text & .......
and the computer freezes a lot longer before the result comes out.

I'm working on hardware most of the time so Visual Basic isn't really familiar to me.I bet I chose the worst way to preview the BIN file. :? Could someone here show me some better ways?Thank you in advance!

Regards,Alex



'===============
Dim i As Long
Dim Lib() As Byte
Dim Buf() As String
Dim Ptr As Long
Dim Lp As Integer
'---------------
'Open file
Path.Action = 1
Open Path.FileName For Binary Access Read As #1
'---------------
'Redefination
i = LOF(1) 'FileLen(Path.FileName)
ReDim Lib(i) As Byte
ReDim Buf(i) As String
'---------------
'Display file information
TextName.Text = "File Path: " & Path.FileName & Chr(13) + Chr(10)
TextName.Text = TextName.Text & "File Length: " & i & "Bytes"
TextName.Text = TextName.Text & " (" & CInt(i / 1024) & "KB)" & Chr(13) + Chr(10) & Chr(13) + Chr(10)
'---------------
'Read file into buffer
Ptr = 1 'Attention:Starting index = 1 rather than 0x0 in MC systems.
For Ptr = 1 To i
Get #1, Ptr, Lib(Ptr)
Buf(Ptr) = Hex(CInt(Lib(Ptr))) & "H "
If Lib(Ptr) < 16 Then
Buf(Ptr) = "0" & Buf(Ptr)
End If
Next Ptr
Close #1
'---------------
 
you are killing your computer by reading byte by byte. don't do it. :lol: :lol:
read the whole thing into memory and then manipulate it any way you like
but for your HDDs sake, avoid reading byte by byte.

files on any media (HDD, FDD, ...) are stored in blocks.
if your HDD is formated to use 4kb blocks than this whole block
is taken even if the file in it is one byte. if the file is larger, it will take
multiples of blocks.
when your program tries to read or write from file, it sends requests to operating system to read or write. operating system finds right block(s) and read the whole block (or series of blocks) at once.

....even if you only asked for one byte...!!!!

if you are reading 260kb file from HDD that uses 4kb blocks, your pc is reading total of 65 blocks. each block is read 4096 times (4kb=4096 byte).
it means you are asking your HDD to do same job 4096 times instead of once. since HDD is still just a MECHANICAL device that need to move disks, heads etc., this is major reason why it takes forever...

fortunatelly HDDs usually have some cache to help them in such cases but
in a windows enviroment there is tons of other things running at the same time and accessing the HDD. although this does reduce stress on the system, it doesn't do miracles...
 
Sounds scary :shock: Anyway I will need a better idea.

I don't knwo how VB handles the file when the open file operation:
Open Path.FileName For Binary Access Read As #1
is executed.Does it just designate a pointer to the location of the file on HDD and read each byte for HDD when GET BYTE operation is executed later?

What would you suggest that I did other than the current method?Thanks!

Regards.
 
vb read file into textbox

Heres some code that will read your file into a textbox

create a new project and form

create a button command1 on your form
create a textbox text1 on your form

add this code and change the filename to your file and path:


'""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Private Sub Command1_Click()
Dim filenumber As Integer, length As Long
filenumber = FreeFile

Dim filename As String

filename = "c:\test.txt"

Open filename For Input As filenumber
length = LOF(filenumber)
If length < 32767 Then
Text1.Text = Input$(length, filenumber)
Else
MsgBox "This file is longer than 32Kb"
End If
Close filenumber

End Sub
'""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

it should be much faster

Mike
 
Alex_rcpilot said:
What would you suggest that I did other than the current method?Thanks!

Since the file will always be pretty small relative to your computer's available RAM, just read the whole file in one pass, put everything in RAM and then do your byte by byte processing on the RAM copy of your file.
 
You also have to give other apps runing on your PC some CPU power.So that it dosent freze in case your opening an big file.

I forgot how that comand is caled in VB.Dose anybody remember?
 
I have been working on some binary files:

Code:
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        Dim fs As System.IO.FileStream = New System.io.FileStream(OpenFileDialog1.FileName, System.IO.FileMode.Open)
        Dim r As System.IO.BinaryReader = New System.IO.BinaryReader(fs)

        Dim i, j, k As Integer
        Dim working As String

        For i = 0 To 15

            k = 0

            For j = 15 To 0 Step -1

                working = DecimalToBinary(r.ReadByte())

                working = working.PadLeft(5, "0")

                maze(k, i, 1) = working.Substring(Len(working) - 1, 1)
                maze(k, i, 2) = working.Substring(Len(working) - 2, 1)
                maze(k, i, 3) = working.Substring(Len(working) - 3, 1)
                maze(k, i, 4) = working.Substring(Len(working) - 4, 1)
                maze(k, i, 5) = 0
                maze(k, i, 6) = 0

                k = k + 1

            Next

        Next

        r.Close()
        Me.Refresh()

    End Sub

This reads each byte then uses an external function to create a binary string (canny show u that, sorry) and then parses the string into the array (as the byte is a load of bit flags)

[damn i need to comment my code...]

Regards,

- Martyn
 
Someone Electro said:
You also have to give other apps runing on your PC some CPU power.So that it dosent freze in case your opening an big file.

I forgot how that comand is caled in VB.Dose anybody remember?

application.doevents() ?

somming like that

oh and BTW i am using VS.net 2003 (oh yeah, got it free from uni)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top