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

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()
Many Thanks for your help,End Sub
Andrew
Jim Sn
Hi,
See the entry on file for the various ways of reading, writing files etc.>>
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.
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.
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.
The following example checks to see if a file exists and if so, uses the FileCopy function to copy it to a new file.
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.
Public OrderNumber As Integer
Public Name As String
Public OrderDate As Date
End Structure
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.
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.
The following example uses the StreamReader class to read the contents of a text file.
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.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 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 following example uses an instance of the FileSystemObject object to read a file and write its contents.
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
TWild
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