|
-
Jan 15th, 2020, 09:04 PM
#1
Thread Starter
Lively Member
[RESOLVED] Save Serial Data to Text File
Hi Guys,
I'm trying to save data that i received from serial port into text file. So far, there is nothing in the file. What should i do?
Here is the code:
Code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If SerialPort1.IsOpen Then
Data_received &= SerialPort1.ReadExisting
If Len(Data_received) > 0 Then
ReceiveTextBox.Text = Data_received
HistoryTextBox.Text = TimeOfDay.ToLongTimeString + " : " + Data_received + vbNewLine + HistoryTextBox.Text
Dim writeFile As New System.IO.StreamWriter("Read.txt")
writeFile.Write(SerialPort1.ReadExisting())
writeFile.Close()
End If
End If
End Sub
-
Jan 15th, 2020, 09:25 PM
#2
Re: Save Serial Data to Text File
Your writeFile command shouldn't be doing another serial port read. It should be writing the data already read from the Serial port that you've done in the code above it.
That being said, once you get that issue fixed, since your Data_received variable (which isn't declared in this block of code, so I'm assuming it is declared at the Form or Module level) is being concatenated rather overwritten, once data has been received the first time, in every execution of your Timer tick event after that your If statement will be True, even if no NEW data has been received, because Len(Data_received) will always be > 0. So with nothing new coming in to the serial port, your log file will continuously report the same data as being read, over and over again, as often as your Timer Tick event is raised.
-
Jan 15th, 2020, 09:43 PM
#3
Thread Starter
Lively Member
Re: Save Serial Data to Text File
thanks, i manage to get the data into text file now with following code. but how can i arrange the data to be recorded to the next line every second? right now all new data on one single line in the text file.
Code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If SerialPort1.IsOpen Then
Data_received &= SerialPort1.ReadExisting
If Len(Data_received) > 0 Then
ReceiveTextBox.Text = Data_received
HistoryTextBox.Text = TimeOfDay.ToLongTimeString + " : " + Data_received + vbNewLine + HistoryTextBox.Text
Dim writeFile As New System.IO.StreamWriter("Read.txt")
writeFile.Write(HistoryTextBox.Text)
writeFile.Close()
End If
End If
-
Jan 15th, 2020, 09:50 PM
#4
Re: Save Serial Data to Text File
Use writeFile.WriteLine instead of writeFile.Write
-
Jan 15th, 2020, 10:12 PM
#5
Thread Starter
Lively Member
Re: Save Serial Data to Text File
-
Jan 15th, 2020, 10:13 PM
#6
Re: Save Serial Data to Text File
-
Jan 15th, 2020, 10:17 PM
#7
Thread Starter
Lively Member
Re: Save Serial Data to Text File
never mind, any idea how to stop the streamwriter?
something like Streamwriter.stop
-
Jan 16th, 2020, 01:56 AM
#8
Thread Starter
Lively Member
Re: Save Serial Data to Text File
alright, manage to get everything in perfect order:
Code:
Private Sub Log_button_Click(sender As Object, e As EventArgs) Handles Log_button.Click
Log_button.BackColor = Color.LimeGreen
StopLog_button.BackColor = Color.Transparent
StopLog_button.Enabled = True
End Sub
Private Sub StopLog_button_Click(sender As Object, e As EventArgs) Handles StopLog_button.Click
StopLog_button.BackColor = Color.Red
Log_button.BackColor = Color.Transparent
Log_button.Enabled = True
End Sub
'READ DATA STRING INPUT
Dim Data_received As String
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If SerialPort1.IsOpen Then
Data_received &= SerialPort1.ReadExisting
If Len(Data_received) > 0 Then
ReceiveTextBox.Text = Data_received
HistoryTextBox.Text = TimeOfDay.ToLongTimeString + " : " + Data_received + vbNewLine + HistoryTextBox.Text
If Log_button.BackColor = Color.LimeGreen Then
Dim writeFile As New System.IO.StreamWriter("Read.txt", IO.FileMode.Append)
writeFile.Write(TimeOfDay.ToLongTimeString + " : " + Data_received + vbCrLf)
writeFile.Close()
End If
End If
End Sub
Last edited by joko markono; Jan 23rd, 2020 at 08:12 PM.
-
Jan 16th, 2020, 02:18 AM
#9
Re: [RESOLVED] Save Serial Data to Text File
p.s. I see you posted while I was working on my post and examples...
If I wanted to write lines to a file, that is constantly open, written to, and closed, as you seem to want, I would use one of the IO.File methods.
Here's a brief example of one way.
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Static counter As Integer
counter += 1
IO.File.AppendAllLines("c:\c\testAppend.txt", {String.Format("Line {0}", counter)})
End Sub
End Class
That will open the file, append the string and automatically add the new line after the string is written.
The method actually takes a string array and will write each element of the array on a new line, which is why the {} is put around the string to turn it into a literal array, or anonymous array, or whatever the technical term should be.
The other obvious choice for a single string would be to use AppendAllText which expects a single string, and add the newline yourself.
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Static counter As Integer
counter += 1
IO.File.AppendAllText("c:\c\testAppend.txt", String.Format("Line {0}{1}", counter, vbNewLine))
End Sub
End Class
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jan 16th, 2020, 02:33 AM
#10
Thread Starter
Lively Member
Re: [RESOLVED] Save Serial Data to Text File
Thanks for your idea. I will give a try.
Tags for this Thread
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
|