Results 1 to 4 of 4

Thread: Serial port delay

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Posts
    43

    Serial port delay

    I am having an issue reading from the serial port....

    I am sending a 10 byte message to a microcontroller, which upon receipt, sends the exact message back immediately to acknowlege. I am 100% certian that the microcontroller is accurately doing its job without adding any significant delay.

    On the visual basic side (running a Dell lattitude laptop, W7, lots of ram, etc...) I send out the 10 bytes to the microcontroler, and then immediately read the serial port using the serialport.read method. Unless I add a 250 ms or greater delay using threading.thread.sleep, I dont catch all of the bytes until the next read because the bytes are not in the buffer yet. 250 ms is successful, 200 causes me to loose about half of the 10 byte array. BTW I am running at 9600 baud.

    My questions is - Once the serial port outputs a few bytes, does it remain unavailable to do anything else for a while?

    Thanks

  2. #2
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: Serial port delay

    The SerialPort Class has an event that is triggered when data is received.
    http://msdn.microsoft.com/en-us/libr...areceived.aspx
    There is an example in the link showing you how to read the data.

    I really doubt it takes 250ms for the round trip. Its more how Windows works by letting you know when data has arrived instead of demanding it up front.
    Think.... Question.....

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Serial port delay

    At 9600 baud you would be looking at around 20ms - 30ms for the data to be transmitted [around 1 ms per character each way] + any handshaking and/or flowcontrol in use + whatever delay there would be on the micorcontroller side in reading and resending the data.

    250 ms sounds high have you checked to see how much time passes from the time the data is sent and the data leaves the micro controller?

  4. #4
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: Serial port delay

    There is latency, but not 250 mS of latency. You didn't post your receive code, but I wrote this simple-minded routine:

    SerialPort1.Write("0123456789")
    Dim Buffer As String = ""
    Dim StartTime As Double = Now.TimeOfDay.TotalMilliseconds
    Do Until SerialPort1.BytesToRead >= 10
    Loop
    Buffer = SerialPort1.ReadExisting
    Dim ElapsedTime As Double = Now.TimeOfDay.TotalMilliseconds - StartTime
    Debug.Print(ElapsedTime)

    In my microcontroller, I waited until I had received all 10 characters and then send 10 from the micro. The total elapsed time was 16 mS from Tx to Rx. The Tx latency was ignored because StartTime was determined after the return from Write. If I use this code:

    Dim StartTime As Double = Now.TimeOfDay.TotalMilliseconds
    SerialPort1.Write("0123456789")
    Dim Buffer As String = ""
    Do Until SerialPort1.BytesToRead >= 10
    Loop
    Buffer = SerialPort1.ReadExisting
    Dim ElapsedTime As Double = Now.TimeOfDay.TotalMilliseconds - StartTime
    Debug.Print(ElapsedTime)
    End Sub

    I see 30 mS elapsed time. The reason is because Write is a blocking method. Other statments after it do not execute until after all data to be sent have been buffered to the UART Tx FIFO.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

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