none
delay in serial port received data RRS feed

  • Question

  • Hi all

    i am having problem in reading data.

    A machine sends continuous data to pc , pc reads data but last protocol / data send by machine is not receiving in on-received-data event handler. when pc sends a protocol / command / data to machine then that pending data  comes in serial port - datareceived event handler so pc always remains back by one protocol .



    • Changed type asb17470 Thursday, February 21, 2013 11:37 AM
    • Edited by asb17470 Thursday, February 21, 2013 12:05 PM
    • Moved by Mike FengModerator Friday, February 22, 2013 4:20 AM
    Thursday, February 21, 2013 11:36 AM

Answers

  • Invoke is a good choice for high speed communication, but remember to close the task from a different thread to avoid a deadlock.

    USB-serial converters courses some problems depending on the .Net version. If you experience any problems, always compile to version 2.0. This version throws an unhandled exception if you remove the USB plug, but all other versions may lose bytes or be completely useless.

    You can find much more information about Invoke, BeginInvoke, how to close the task from a different thread, the different .Net versions etc. in my VB SerialPort tutorial.


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    • Marked as answer by asb17470 Saturday, February 23, 2013 2:57 PM
    Saturday, February 23, 2013 10:03 AM

All replies

  • Its seems that the problem is with the communication protocol between the Machine & PC.

    Ensure the Communicaiton Protocol of the Machine & the corresponding implementation in the program.


    It all Happenz Sendil

    Thursday, February 21, 2013 1:44 PM
  • ok thanks i will check
    Thursday, February 21, 2013 5:27 PM
  • You can be quite sure that there is almost no delay in the DataReceived event.

    My guess is that you use for example Read or ReadExisting, which does not necessary return the full telegram.


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    Thursday, February 21, 2013 7:38 PM
  • Thanks

    No there is no delay in datareceived event. second i am using readchar method as readexisting & readline method are not effective while using usb to serial adapter.

    but i think i solved problem by implementing do . . . .loop until .

    my code is

    Dim RcvdChar As Char = New Char
            Do
                Try
                    RcvdData += ChrW(SerialPort1.ReadChar)
                Catch ex As Exception
                    MsgBox( ex.Message)
                Finally
                    '
                End Try
                If Trim(Mid(RcvdData, 1, 1)) = "C" Then
                    Me.Invoke(New MethodInvoker(AddressOf DataProcess))
                     '''''''Me.BeginInvoke(New AckoString(AddressOf DataProcess), rcvddata)
                End If
                If Trim(Mid(RcvdData, 1, 1)) = "F" Then
                    If Trim(Mid(RcvdData, 4, 1)) = vbCr Or Trim(Mid(RcvdData, 4, 1)) = Chr(13) Then
                        Me.Invoke(New MethodInvoker(AddressOf DataProcess))
                        ''''''Me.BeginInvoke(New AckoString(AddressOf DataProcess), rcvddata)
                    End If
                End If
            Loop Until (SerialPort1.BytesToRead = 0)

    previously there was no do ..loop until.

    Now i have to check it practicall on machine whether invoke or begininvoke is to be used. right now invoke gives me good result while using rs232 cable . now usb to serial adapter  testing is to be done.

    Please suggest.

    Saturday, February 23, 2013 5:44 AM
  • Invoke is a good choice for high speed communication, but remember to close the task from a different thread to avoid a deadlock.

    USB-serial converters courses some problems depending on the .Net version. If you experience any problems, always compile to version 2.0. This version throws an unhandled exception if you remove the USB plug, but all other versions may lose bytes or be completely useless.

    You can find much more information about Invoke, BeginInvoke, how to close the task from a different thread, the different .Net versions etc. in my VB SerialPort tutorial.


    Everything should be made as simple as possible, but not simpler. Any fool can write code that a computer can understand. Good programmers write code that humans understand.

    • Marked as answer by asb17470 Saturday, February 23, 2013 2:57 PM
    Saturday, February 23, 2013 10:03 AM