If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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 )

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.

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.