CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    41

    serial Buffer issue with VB.net 2005

    Just wondering how to solve this problem i have a stream of data coming in to my serial port which is the following at the moment however in will be a random number 0 - 512 and it repeats:

    Code:
    190
    120
    150
    110
    i'm reading it with:

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            current_time.Text = DateTime.Now.ToLocalTime
    
            SerialPort1.Open()
    
            Dim data As String
            data = SerialPort1.ReadLine()
            SerialPort1.Close()
    
            Dim Setup_A_data As New StreamWriter("serial_data.text", True)
            Setup_A_data.WriteLine(data)
           
    
            Setup_A_data.Close()
    
    end sub
    the output im getting is

    Code:
    0
    120
    150
    1110
    190
    120
    1
    120
    150
    110
    150
    110
    190
    10
    190
    120
    1
    120
    150
    110
    150
    the output should be equal to the input from the serial line but its not
    do i need to be using a request to send or something else and if so how would i archive this?
    Thanks Michael

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: serial Buffer issue with VB.net 2005

    Check the value of the buffer. Perhaps send a ENDLINE or some other character instead.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: serial Buffer issue with VB.net 2005

    Checking the value of the buffer is usually a good idea.
    Also if you are using the readline method to get data then the data should be comming in as a line with the proper CRLF terminators.

    In any case it would not seem to be a good idea to open the port, read a line from it then close it all within a tick of the timer. There are many possible issues here.

    The port should be opened outside the timer routine and left open as long as data is expected. The code in the timer should first check to see if any data is there and then read what is there, either one line at a time or a block of data or all the data currently present. The port should remain open to continue receiving data into the buffer which will be there to read on the next timer cycle.

    Opening an closing the port in such a way as shown in the op pretty much does away with any chance of receiving data at any time other than the fraction of a second where the port is actually open. On a fast pc odds are this routine would never receive data without using a breakpoint on the data= line. It would also discard any data that may come after a end line sequence and clear it from the buffer effectively loosing that data should it exist.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured