Advanced file sytem stuff (i think so anyway :))

Hi,

I have a microprocessor that automatically (over a period of time) saves measurements to an SD card, as the microprocessor (micro) has limited resources I wrote my own disk operating system as a result windows cannot read any of the data saved on the card, so to access the data I have written a Visual basic application that first asks the mirco how many different results it has and the user has the option of viewing one of the records via a textbox. I have used stream writer so the user can create a text file from the data received in the txt file:-

NOTE: - I am using the serial port to communicate with the micro

My question is: - Is there a way to automatically get windows to create and name files upon request by the user, to explain more say I have 100 records on the disk, at the moment if the user wants to save all of these he/she would have to request each one and then save them to file. Which would take a lot of time; what I need is a way for the user to ask the micro for all 100 records and get visual basic to create 100 txt containing each separate record.

AT the moment the micro sends information in the following way:

<data><Line feed> and I use the visual basic serial command readline which gets the record, so I could send from the micro for each record the following:-

<Start file command><line feed>

<file name – Record 1><line feed>

<file date and time><line feed>

<data poini 1><line feed>

<data point 2><line feed>

<data point etc><line feed>

<close file command><line feed>

<Start file command><line feed>

< file name – Record 2><line feed>

<file date and time><line feed>

<data poini 1><line feed>

<data point 2><line feed>

<data point etc><line feed>

<close file command><line feed> ---- and so on 100 times


For the start/close command I could use a serial carriage return or something like that.


Does anybody have any suggestions of how to go about doing this in visual basic I am new to the language although understand C++, below is code I use for reciving the code from the micro: -


Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived

Dim RxString As String = SerialPort.ReadLine.ToString()

BeginInvoke(New myDelegate(AddressOf updateTextBox), RxString)

End Sub

Public Delegate Sub myDelegate(ByVal Buffer As String)

Public Sub updateTextBox(ByVal Buffer As String)

Dim dec As Integer

Dim value As Double

dec = CInt("&H" & Buffer) ` Converts the String to hex then to dec

value = dec * 0.001221 ` Converts the value to a voltage

Any help/code suggestion or even example projects with something similar would be a great help,

NOTE: - IT DOES NOT HAVE TO BE A TXT FILE, IT COULD BE AN XML, I ALSO NEED TO BE ABLE TO READ THE TXT FILE BACK INTO A VB VARIABLE AS I HAVE A BASIC GRAPH PROGRAM THAT DISPLAYS THE RESULTS.

Many Thanks for help,

Andrew




Answer this question

Advanced file sytem stuff (i think so anyway :))

  • netpicker9

    Hi,

    THanks all for your post especially S_DS

    Below is my code it basically works by looking at the serial buffer and when it detects charater a (which is sent by the micro processor) it knows that a file has started, at which point it fills and array with the values (sent by the micro processor ) until character b is sent by the micro presser. Then the write file function is called and the file created.

    I have a few questions:-

    a) How do i send the entire value byte array (as at the moment i am just sending value(3), is there a better way than: -

    while i <200
    writeline(value(i))
    i=i+1
    end while

    b) how can I automatically increment the file name as at the moment i can only write one file

    c) How do I set the default folder for the file to be saved in, and also how do I allow the user to change it

    d) Can any body think of a better way of doing the below code


    Public Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived


    Dim RxString As String = SerialPort.ReadLine


    SerialPort.DiscardInBuffer()


    BeginInvoke(New myDelegate(AddressOf updateTextBox), RxString)


    End Sub


    Public Delegate Sub myDelegate(ByVal Buffer As String)


    Public Sub updateTextBox(ByVal Buffer As String)


    Dim path As String = "C:\text.txt"

    If Buffer = "a" Then start = True : PSoCINtxtb.AppendText("File Start" & vbCrLf)

    If Buffer = "b" Then start = False : PSoCINtxtb.AppendText("File Stop" & vbCrLf)


    If start = True Then value(i) = Buffer : i = i + 1

    If start = False Then PSoCINtxtb.AppendText(value(3) & vbCrLf) : WriteTextFile(path, value(3))


    End Sub


    Sub WriteTextFile(ByVal path As String, ByVal value As String)

    Dim sr As StreamWriter

    sr = New StreamWriter(path)

    sr.WriteLine(value)

    sr.Close()

    End Sub

    Many Thanks for your help,

    Andrew


  • Jim Sn

    Hi,

    See the entry on file for the various ways of reading, writing files etc.>>

    See this little lot too.>>
    Choosing Among File I/O Options in Visual Basic .NET
    See Also:-

    File Access with Visual Basic .NET | File Access with Visual Basic Run-Time Functions | Accessing Files with FileSystemObject
    Visual Basic .NET presents three possible approaches to file I/O for the programmer: runtime methods contained in Visual Basic .NET; FileSystemObject; and the Common Language Runtime File methods.
    File Methods in Visual Basic .NET
    The Microsoft.VisualBasic namespace is populated by a host of functions and methods familiar to users of earlier versions of Visual Basic. While some names have shifted slightly, the vast majority are the same: Dir, Input, Print, Seek, Write, and so forth.
    A few changes have occurred in the functions used in these different types of file access. The most noticeable change is the substitution of the FilePut/FilePutObject and FileGet/FileGetObject functions for Put and Get. In Visual Basic .NET, FilePut and FileGet map to the Put and Get functions; FilePutObject and FileGetObject are used to write an object of type Object (the equivalent of Variant type in earlier versions of Visual Basic) to the file. For more information, see Variant Type Constant Changes in Visual Basic .NET.
    Advantages and Disadvantages
    Two principal advantages of the functions provided by the Visual Basic .NET runtime are familiarity and ease of use. The core functionality of Visual Basic .NET, which remains familiar, intuitive, and flexible, provides a comfortable launching place for exploration of .NET.
    Similarly, upgrading a program written in an earlier version of Visual Basic to its .NET counterpart or creating an application that interacts well with applications written in earlier versions should prove relatively simple. For more information on upgrading previous versions of code, see Preparing a Visual Basic 6.0 Application for Upgrading.
    The Visual Basic .NET functions also provide binary file access, which the FileSystemObject object (described in the next section) does not, making it a better choice when working with variable length fields.
    Finally, there are no special considerations, such as the need to import namespaces, when using the Visual Basic .NET built-in file I/O functions from within Visual Basic .NET.
    The fact that Visual Basic .NET file I/O functions do not support writing types other than String, Date, Integer, Long, Single, Double, Decimal, and structures and arrays of those types poses a disadvantage when using these functions. Beyond this, classes cannot be serialized and performance in such cases may not be as optimal as using the System.IO classes directly.
    Code Examples
    The following example checks to see if a file exists and if so, uses the FileCopy function to copy it to a new file.
    Private Sub CopyFiles()
    Dim checkFile As String
    checkFile = Dir$("c:\test.txt")
    If checkFile = "test.txt"
    FileCopy "c:\test.txt", "c:\new.txt"
    End If
    End Sub
    The following example uses the FilePut function, which corresponds to the Put function in previous versions of Visual Basic, to write data to a file. Three records of the structure CustomerRecord are written to the file.
    Structure CustomerRecord
    Public OrderNumber As Integer
    Public Name As String
    Public OrderDate As Date
    End Structure
    Sub WriteData()
    Dim MyRecord As CustomerRecord
    Dim RecordNumber As Integer
    Dim RecordDate As Date
    ' Open file for random access.
    FileOpen(1, "C:\TESTFILE.txt", OpenMode.Binary)
    For RecordNumber = 1 To 3 ' Loop 3 times.
    MyRecord.OrderNumber = RecordNumber ' Define OrderNumber.
    MyRecord.OrderDate = RecordDate ' Define OrderDate.
    MyRecord.Name = "My Name" & RecordNumber ' Create a string.
    FilePut(1, MyRecord) ' Write record to file.
    Next RecordNumber
    FileClose(1)
    End Sub
    .NET Framework Classes
    The System.IO namespace contains File and Directory classes, which provide the basic functionality that manipulates files and directories. Because all the methods are static or shared members of these objects, you can use them directly without creating an instance of the class first.
    Advantages and Disadvantages
    Venturing into the common language runtime yields a rich class library. For example, the System.IO namespace provides services beyond basic file I/O, such as the FileSystemWatcher class, which allows you to monitor changes to files, and the FileInfo and DirectoryInfo classes, which allow you to gather information about a specified file or directory. For more information, see FileSystemWatcher Class, FileInfo Class, and DirectoryInfo Class.
    Similarly, the common language runtime offers the FileIOPermission class, which allows you to control access to files and directories. This may be particularly important for developers working with Web Forms, which by default run within the context of a special local user named ASPNET, which is created as part of the ASP.NET and .NET Framework installations. When such an application requests access to a resource, that request is made within the context of that user. However, by default, the ASPNET user has limited permissions, which may prevent them from performing actions such as writing to a file from a Web application. For more information, see FileIOPermission Class.
    The common language runtime is also compatible with other .NET languages, such as Visual C# , and may aid in achieving consistency across applications, which may be particularly important at the enterprise level.
    Lack of familiarity with the common language runtime may create difficulties for a Visual Basic programmer who is still becoming familiar with these new approaches. If you are in that category, you may prefer to rely on the methods you know well.
    Code Examples
    The following example uses the StreamReader class to read the contents of a text file.
    Imports System
    Imports System.IO
    Function ReadTextFile(ByVal path As String) As String
    Dim sr As System.IO.StreamReader
    Dim Contents As String
    sr = New StreamReader(path)
    Contents = sr.ReadToEnd()
    sr.Close()
    Return Contents
    End Function
    The following code reverses the previous example, using the same approach with the StreamWriter class to write to a text file.
    Imports System
    Imports System.IO
    Sub WriteTextFile(ByVal path As String, ByVal value As String)
    Dim sr As StreamWriter
    Dim Contents As String
    sr = New StreamWriter(path)
    sr.Write(value)
    sr.Close()
    End Sub
    FileSystemObject
    Like the functions and methods provided by the Microsoft.VisualBasic namespace, the FileSystemObject object will be familiar to users of previous versions of Visual Basic.
    The FileSystemObject (FSO) model presents files, directories, and drives as COM objects, each with their own properties and methods. You can create and manipulate these objects and use their properties to discover information such as a directory's contents, a file's size, the time an object was created, and so on. You access the objects representing the system's files, directories, and drives by creating and accessing an instance of the FileSystemObject object.
    The FSO object model, which is in the Microsoft Scripting Runtime type library (Scrrun.dll), supports the creation and manipulation of text files through the TextStream object. For more information, see TextStream Object. The FileStream class exposes a stream around a file, allowing both synchronous and asynchronous read and write operations on a file system as well as buffering input and output for better performance. Information is written to the buffer and the contents of the buffer are written to the file only when the buffer is full or a Close method is called. For more information, see FileStream Class.
    Advantages and Disadvantages
    The primary advantage of the FSO is that it gathers a number of file I/O functions into a single object. By creating an instance of the object, you can access its methods and properties.
    The FSO object model is also well suited to threading. It provides a constructor for asynchronous I/O, such as when using the BeginRead and BeginWrite methods, which enables the main thread to continue, allowing the user to process the data later. Multiple I/O requests can be pending at the same time.
    However, there are several disadvantages to the FSO. As noted earlier, it works only with text files. To manipulate binary files, you must use pointers to an address in memory, or byte arrays, which are not supported by the object.
    Moreover, when you read or write a large amount of content, the information stored in the buffer may create a big memory hit. Finally, you cannot manage permissions or file and folder attributes.
    Code Example
    The following example uses an instance of the FileSystemObject object to read a file and write its contents.
    Public Class UsesScripting
    Public Shared Sub Main()
    Dim fileSystem As New Scripting.FileSystemObject()
    Dim file As Scripting.TextStream
    file = fileSystem.OpenTextFile("c:\test.txt", _
    Scripting.IOMode.ForReading, False, _
    Scripting.Tristate.TristateUseDefault)
    Dim contents As String = file.ReadAll()
    Console.WriteLine(contents)
    file.Close()
    End Sub
    End Class
    See Also
    File Access with Visual Basic .NET | File Access with Visual Basic Run-Time Functions | Accessing Files with FileSystemObject
    --------------------------------------------------------------------------------
    Send feedback on this topic to Microsoft
    c Microsoft Corporation. All rights reserved.
    Regards,
    S_DS


  • TWild

    HI,

    Formatting the Card in fat32 or fat would not of help as i would still have to write a program to read byte level inforamtion of the card and then reasemble it, i would aslo ihave had to make sure i did not write over sectors used for the filing system.

    It is much easier this way.

    How do i create a file (what are the VB commmands)

    creat file x
    name file x
    file file x
    close and save file x

    Andy


  • R_G_B

    Hi,

    Could you not have formatted the SD card on a standard card reader/writer as a standard FAT32 or NTFS filesystem and then use that

    I would handle the data received file names in a loop and ask for user input to ask for which records to fetch.

    Look at using FOR NEXT loops, string commands and InputBox for example.

     

    Dim response As Integer

    response=InputBox("How many records to get please ")

    Dim index As Integer

    For index = 1 to response

    Dim fileName As String

    fileName="Record " & CSTR(index) 'This will put RECORD 1 , RECORD 2 , RECORD 3 etc into the string variable fileName

    '<Start file command><line feed>

    '<file name – Record 1><line feed>

    '<file date and time><line feed>

    '<data poini 1><line feed>

    '<data point 2><line feed>

    '<data point etc><line feed>

    '<close file command><line feed>

    Next

     

    Of course this goes from the 1st record onwards but you could use a 2nd InputBox statement and ask for 1st and last record numbers and loop between them.

    Even better you could put the record numbers required into a textbox separated by spaces or commas and split the string using the String.Split function ,putting each value into an array.

    Get the array values in a FOR NEXT loop too. Use Ubound to get the upper bound of the array for your loop.

    Hope these ideas help

    See these for the MS help on Ubound and String.Split respectively.>>

     

     http://msdn2.microsoft.com/en-us/library/95b8f22f.aspx

     http://msdn2.microsoft.com/zh-cn/library/aa904305(VS.71).aspx

     

     

    Regards,

    S_DS

     



  • Advanced file sytem stuff (i think so anyway :))