|
-
May 4th, 2013, 02:17 AM
#1
Thread Starter
New Member
Receive serial and save to text file with time+date
I have a PICAXE sending serial text and I need to save the data received in a new line on a text file with the time and date next to it, I know the data can be received because I have seen it showing up in programs such as hyperterminal.
The line should look something like this:
<data received> | 17:08:25 | 22/05/13
Thanks in advance
-
May 4th, 2013, 08:05 AM
#2
Addicted Member
Re: Receive serial and save to text file with time+date
Hi, is the data a string of ASCII characters or a string of binary values. Do you have any control over the data such as adding a header or appending a carriage return. What is the baud rate.
-
May 5th, 2013, 02:38 AM
#3
Thread Starter
New Member
Re: Receive serial and save to text file with time+date
I have the ability to change what the micro controller sends to the computer and it is running at 4800
-
May 5th, 2013, 02:39 PM
#4
Addicted Member
Re: Receive serial and save to text file with time+date
Ok here is a quick snippet to test with, it is simply a windows application with one form a richtextbox control and a serialport. The serial port is configured in the forms load event handler, modify the baud rate and com port name to suit your hardware.
The reason I asked it you can modify the data from the microcontroller is that this example uses ReadLine which requires a Newline appended to the data, for example "Hello World" , 10
The richtextbox allows you to read the incoming stream while it is being written to file, the filename and path can be modified to whatever is needed.
I have used similar applications to this where I just display the data in the text box, I'm then able to read what I have and if I want to save it to file I save it using a savefiledialog, an option to consider.
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.BaudRate = 4800
SerialPort1.PortName = "COM4"
SerialPort1.Open()
End Sub
Public Delegate Sub mydelegate(ByVal mystring As String)
Private Sub text_out(ByVal s As String)
Dim file_text As String = s & " " & Date.Now & vbCrLf
RichTextBox1.AppendText(file_text)
My.Computer.FileSystem.WriteAllText("C:\Users\Owner\Test.txt", file_text, True)
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim rx_string As String = String.Empty
SerialPort1.ReadTimeout = 20
Try
rx_string = SerialPort1.ReadLine
Me.Invoke(New mydelegate(AddressOf text_out), rx_string)
Catch ex As Exception
End Try
End Sub
-
May 6th, 2013, 04:26 AM
#5
Thread Starter
New Member
Re: Receive serial and save to text file with time+date
Thanks for your help, I have the program working perfect.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|