Results 1 to 2 of 2

Thread: Serial port reading is very slow

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Serial port reading is very slow

    Hi Folks

    I have a simple synchronous function that write n bytes to the serial port and reads the same number of bytes back. In the case of the code below n = 4.

    Now I have an oscilloscope connected on the serial line so I can see the actual real timings of the data on the lines and that the date is there.

    The external device responds very promptly, the write and response takes a grand total of 5 milliseconds. However the actual vb.net code (2010) does not complete the reads until after between 15 - 25 ms have passed!!

    I.E. The data is sent out, taking 2.08ms, the response starts 1.04ms later and takes 2.08ms to complete.

    Timing the code using Systems.Diagnostics.Stopwatch shows that the writebyte functions take effectively 0ms while the reads take a total of 20ms.... even though my scope shows the whole caboodle is finished in 5ms!


    eg.

    Code:
    Tx: ______[]_______________________________
    Rx:_________[]____________________________
              ^tx 4 bytes
                ^rx 4 bytes
                  ^should finish read here
                                      ^actually finishes read here
    Is this just due to how the serial port drivers are implemented in Windows or can I do something to improve this situation?

    Code:
    Imports System.IO.Ports
    Imports System.Diagnostics
    
    Private _port As New SerialPort()
    
    Public Sub OpenPort
        _port .PortName("COM14")
        _port.Parity = 0
        _port.BaudRate = 9600
        _port.StopBits = StopBits.One
        _port.DataBits = 8
        _port.ReadTimeout = 500
        _port.WriteTimeout = 500
        _port.Handshake = Handshake.None
        _port.Open()
    End Sub
    
    Public Overridable Overloads Function SendCommand(ByRef Data() As Byte, ByVal Write As Boolean) As Boolean
        Dim Length = Data.Length
        Dim Offset As Integer = 0
        Dim sw1 As New Stopwatch()
    
        While Length >= 4 'Send and read data in blocks of two bytes
            Try
                If Write = True Then 'Set or clear the R#/W bit
                    Data(Offset) = Data(Offset) Or &H80
                Else
                    Data(Offset) = Data(Offset) And Not &H80
                End If
    
                _port.Write(Data, Offset, 2) 'Write the data to the port.
    
                sw1 = Stopwatch.StartNew()
                Data(Offset) = _port.ReadByte() 'Save data that is read back
                Data(Offset + 1) = _port.ReadByte()
                Data(Offset + 2) = _port.ReadByte()
                Data(Offset + 3) = _port.ReadByte()
                sw1.Stop()
                Console.WriteLine("Read time: " & sw1.ElapsedMilliseconds.ToString)
            Catch ex As Exception
                _port.DiscardInBuffer()
                _port.DiscardOutBuffer()
                Throw
                Return False
            End Try
    
            Length = Length - 4 'Update indices
            Offset = Offset + 4
        End While
        Return True
    End Function
    Last edited by wolf99; Sep 21st, 2015 at 07:01 AM.
    Thanks

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Serial port reading is very slow

    Try reading all of the bytes at once using the Serialport.Read method. That would be about as fast as you will get with .net

    I think the syntax would be like this...

    Code:
    _port.Read(Data, Offset, 4)
    Kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

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
  •  



Click Here to Expand Forum to Full Width