dcsimg
Results 1 to 10 of 10

Thread: [RESOLVED] Save Serial Data to Text File

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2019
    Posts
    53

    Resolved [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

  2. #2
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    1,000

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2019
    Posts
    53

    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

  4. #4
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    1,000

    Re: Save Serial Data to Text File

    Use writeFile.WriteLine instead of writeFile.Write

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2019
    Posts
    53

    Re: Save Serial Data to Text File

    it doesn't work.

  6. #6
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    1,000

    Re: Save Serial Data to Text File

    Bummer.

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2019
    Posts
    53

    Re: Save Serial Data to Text File

    never mind, any idea how to stop the streamwriter?
    something like Streamwriter.stop

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2019
    Posts
    53

    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
    Name:  log.jpg
Views: 86
Size:  136.0 KB
    Last edited by joko markono; Jan 23rd, 2020 at 08:12 PM.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,907

    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

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2019
    Posts
    53

    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
  •  



Featured


Click Here to Expand Forum to Full Width