Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.NET
Public Shared Function ReceiveData() As String
            Dim RXCnt As Integer = 0
            Dim RXByte As Byte = 0
            Dim reply As String = String.Empty
            Dim STX As Byte() = {&H2, &HD}
            Dim hexwrite As String = String.Empty
            Dim ack As Double = 0
            Dim errorCode As String = Nothing
            Dim strText As String = Nothing
            Dim RXArray As Char() = New Char(2047) {}
            Dim errorDescription = String.Empty

            Dim LookUpTable As String = "0123456789ABCDEF"

            Do
                '----- Start of communication protocol handling ---------------------------------------------

                RXCnt = 0
                Do
                    RXByte = COMPort.ReadByte

                    RXArray(RXCnt) = LookUpTable(RXByte >> 4) ' Convert each byte to two hexadecimal characters

                    RXCnt = RXCnt + 1
                    RXArray(RXCnt) = LookUpTable(RXByte And 15)
                    RXCnt = RXCnt + 1

                Loop While Not ((COMPort.BytesToRead = 0))

                '----- End of communication protocol handling -------------------------------------------------------------

                recievesend = False

                reply = RXArray(0).ToString & RXArray(1).ToString

                If reply = "06" Then
                    If readwrite = True Then

                        Try
                            COMPort.Write(STX, 0, STX.Length)
                        Catch ex As TimeoutException
                            errorCodeText = String.Concat(errorCodeText, ex.Message)
                        Catch ex As InvalidOperationException
                            errorCodeText = String.Concat(errorCodeText, ex.Message)
                        Catch ex As UnauthorizedAccessException
                            errorCodeText = String.Concat(errorCodeText, ex.Message)
                        End Try

                        recievesend = True
                        hexwrite = "020D"
                    ElseIf readwrite = False Then
                        If ack < 1 Then
                            ack = ack + 1

                            Try
                                COMPort.Write(writebytes, 0, writebytes.Length)
                            Catch ex As TimeoutException
                                errorCodeText = String.Concat(errorCodeText, ex.Message)
                            Catch ex As InvalidOperationException
                                errorCodeText = String.Concat(errorCodeText, ex.Message)
                            Catch ex As UnauthorizedAccessException
                                errorCodeText = String.Concat(errorCodeText, ex.Message)
                            End Try

                            recievesend = True
                            hexstring = StringToHex(writedata)
                        Else
                            ack = 0
                        End If
                    End If
                ElseIf reply = "15" Then
                    errorCode = RXArray(3).ToString()
                End If
                Thread.Sleep(300) 'sleep between incoming bytes
            Loop While Not ((COMPort.BytesToRead = 0))

		strText = New String(RXArray, 0, RXCnt) & vbCrLf

            Return DecodeHexString(strText)

        End Function


What I have tried:

This code i am using to read data from the port is very slow, please help me if there are any new options to try to fasten the reading.
Posted
Updated 10-Aug-16 6:58am
Comments
[no name] 10-Aug-16 12:57pm    
You have a Sleep(300)...
Member 10701091 11-Aug-16 9:30am    
Yes, if i remove the sleep then the device is read too fast resulting in not reading any bytes from the device.
[no name] 11-Aug-16 9:37am    
It is not my meaning to remove it, but maybe make it something smaller than 300, i.e. 20 to check if this improves Speed a Little bit.
Member 10701091 11-Aug-16 16:19pm    
It works if it is more than 200

Probably, there aren't : serial ports run at a fixed speed (known as a Baud Rate) which controls the maximum speed at which characters can be transfered between devices connected to it. If you serial port is set to 9600 baud, then you can transfer up to 960 characters per second (approximately) and no more.
 
Share this answer
 
Comments
Member 10701091 11-Aug-16 9:31am    
This is my setting for reading the port

BaudRate = 9600
DataBits = 8
HandShake = Handshake.None
Parity = Parity.None
StopBits = StopBits.One
WriteTimeOut = 1000
ReadTimeOut = 1000
OriginalGriff 11-Aug-16 9:45am    
So...9600 baud, 8 bpc, no parity, one SB.
That's around 1000 characters per second - and that's why your data is slow...
There is nothing you can do about that if those parameters are what the other end device expects.
Member 10701091 11-Aug-16 16:21pm    
Are there any recommended settings to speed up the reading.
OriginalGriff 11-Aug-16 16:41pm    
Sorry, but no. It's a "physical limit" in that 9600 baud is the speed at which the two devices talk. And that means they can transfer 9,600 bits of information per second: no more.
And each character takes 8 data bits, plus a start bit, and a stop bit, so that's 10 bits per character, and that means up to 960 characters per second at best.
If you can reconfigure the remote device to use a faster speed, then you could improve that, but there is a limit on serial ports, which is 115200 baud, or 11,520 characters per second - if and only if the remote device can do it, which will depend on the device.
Member 10701091 11-Aug-16 17:39pm    
The device can accept a baudrate of 57600 will this improve.
Quote:
This code i am using to read data from the port is very slow
By design, Serial port communications are very slow.
On real serial port, maximum speed is 115200 bauds, which means 11520 chars par second if there is a continuous flow.

What is your actual speed in baud and how many char per seconds do you receive ?
 
Share this answer
 
Comments
Member 10701091 11-Aug-16 9:28am    
This is my setting for reading the port

BaudRate = 9600
DataBits = 8
HandShake = Handshake.None
Parity = Parity.None
StopBits = StopBits.One
WriteTimeOut = 1000
ReadTimeOut = 1000
Patrice T 11-Aug-16 9:32am    
You can expect 960 chars per seconds at maximum rate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900