Need help faster way to read byte from serial-VBForums
Results 1 to 4 of 4

Thread: Need help faster way to read byte from serial

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    2

    Need help faster way to read byte from serial

    Hi All,
    I am at a beginner level. I want to read all byte from serial until &H29. I have simple code below but it take over 6 seconds.
    Could you help to make it faster? or share better method. ( I try same data with commercial software it less than 1 sec )

    Code:
        Function GetBytes() As String
            Dim serialbyte(350000) As Byte 'For example set maximum byte is 350000
            Dim count As Integer = 0
            Dim i As Integer = 0
            Dim rcb As Byte
            While rcb <> &H29
                rcb = SerialPort2.ReadByte
                serialbyte(i) = rcb
                i += 1
            End While
            Dim str = "Nothing"
            Return str
        End Function
    Last edited by Shaggy Hiker; Feb 7th, 2018 at 10:45 AM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,171

    Re: Need help faster way to read byte from serial

    That isn't Visual Basic 6 (i.e. VB6) code. VB6 code would be for the 1998 version of VB and earlier, so you are posting in the wrong forum.

    That is VB.Net code, of some ilk, i.e. from Visual Studio 2017, 2015, 2013 etc... hopefully nothing earlier than 2010.
    I'll inform a moderator to move this thread to the vb.net forum.

    That code doesn't make a lot of sense since it will always return "Nothing".
    Is the purpose to drain the serial buffer until the &H29 byte is received in order to synchronize access to the stream on later reads?

    If you were returning the bytes read until you received &H29 then it would probably make more sense to have a permanent buffer, either static within the function or declared outside the function, then use ReadExisting rather than ReadByte to read multiple bytes into a local buffer at one call. Then process the local buffer and storing data in the permanent buffer and when the &H29 is found, returning the portion of the data collected, and maintaining the rest of any other data read for the next time.

    It would be better still to involve the DataReceived event to collect the data from the serial port, rather than poll the port.
    A better idea of what you're actually trying to do would allow better advice on approaches that can be taken.

    The amount of time a serial read takes will also depend on the baudrate and how long the stream is until you get the &H29.

  3. #3
    Addicted Member
    Join Date
    Nov 2011
    Posts
    136

    Re: Need help faster way to read byte from serial

    As passel suggests the SerialPort.DataReceived event might be a better way to capture the data and then read whatever is in the receive buffer at that time. You could try the following and see how that works out but there are maybe other factors to consider such as baud , buffer size etc.

    Code:
     Private WithEvents ComPort As New SerialPort
        Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles ComPort.DataReceived
    
            Dim sData As Byte
    
            ComPort.ReadTimeout = 20
    
            Do While ComPort.BytesToRead > 0
    
                Try
    
                    sData = ComPort.ReadByte()
    
                    'do something with sData before the next read
    
                Catch
    
                End Try
    
            Loop
    
        End Sub
    Edit: The code I first posted made it too easy to enter an infinite loop so I altered the test condition of the do while
    Last edited by Mc_VB; Feb 8th, 2018 at 10:55 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    2

    Re: Need help faster way to read byte from serial

    passel & Mc_VB
    Thanks and sorry for post in the wrong forum.

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