none
I Want To Measure Time Between Two Serial Received Data RRS feed

  • Question

  • Hello Everyone,

    In my project I am receiving data from a microcontroller on my vb programme through serial communication and I am getting accurate data on my programme...

    Now next I want to measure the time between two serially received data... So plz help me in doing this as I am unable to this on my own. I Shall be very thankful

    I am attaching my code below  

     ''''''''''''''Variables for serial communication'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim i As Integer
        Dim WithEvents myPort As New SerialPort
        Dim inData As UInt32
        Dim n As UInt32
        Dim l As UInt32
        Public Delegate Sub myDelegate1(ByVal n As UInt32)
     
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            
            myPort.PortName = "COM2"
            myPort.BaudRate = 115200
            myPort.Parity = Parity.None
            myPort.DataBits = 8
            myPort.StopBits = StopBits.One
            myPort.Encoding = Encoding.ASCII
    
        End Sub
        Private Sub myPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myPort.DataReceived
            
            While myPort.BytesToRead > 0
                inData = myPort.ReadLine
               
                Me.BeginInvoke((New myDelegate1(AddressOf Graph1Delegate)), n)
            End While
        
        End Sub
        Private Function Graph1Delegate()
            n = n + inData
          
            l = n * 5
            TextBox3.Text = n
            TextBox2.Text = l
                 
    
    
        End Function

    Thursday, March 22, 2018 8:13 AM

All replies

  • Now next I want to measure the time between two serially received data... So plz help me in doing this as I am unable to this on my own. I Shall be very thankful

    When the first one is received, record the time.  When the second one is received, record the time again.  Subtract the first from the second to get the difference.

    The simplest implementation would be to add the current time to a collection (eg, a List(Of)) for every event, and do the difference calculation later.  

    • Proposed as answer by Cor Ligthert Thursday, March 22, 2018 10:13 AM
    Thursday, March 22, 2018 9:03 AM
  • Thanks for your reply..

    But can you please elaborate it more because I am new to programming I didn't got your point..

    Thursday, March 22, 2018 10:14 AM
  • But can you please elaborate it more because I am new to programming I didn't got your point..

    Use a List(Of DateTime).  https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

    Each time the event occurs, add the current time to the list.  For instance, if the event you want to record is the DataReceived event, then you would use code like:

    Dim EventTimes as New List(Of DateTime)
    ...
        Private Function Graph1Delegate()
            n = n + inData
            EventTimes.Add(Now)
            ...

    Then, when you want to analyse the times, you compare any two items in the list to find the time difference. 

        Dim TimeDelay As Integer = (EventTimes(1) - EventTimes(0)).Milliseconds

    Thursday, March 22, 2018 11:11 AM
  • Thanks for the example but the problem in this is that it will show the time after receiving data at least twice....

    but I want to measure the time for every single pulse received not the time difference between the two pulses.

    for example if I am receiving 500 pulses generated from an microcontroller.... Now I want to check the time of every single pulse received on my vb programme

    Thursday, March 22, 2018 11:35 AM
  • Your VB program is to slow for that and also behaving time slices because of the OS

    , use Intel Assembler

    https://software.intel.com/en-us/articles/introduction-to-x64-assembly


    Success
    Cor



    Thursday, March 22, 2018 11:52 AM
  • Here is one way to record many time intervals.

        Private serRcvIntrvl As New Stopwatch
        Private serRcvIntrvls As New Concurrent.ConcurrentQueue(Of TimeSpan)
        Private Sub myPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myPort.DataReceived
            If serRcvIntrvl.IsRunning Then
                serRcvIntrvl.Stop()
                serRcvIntrvls.Enqueue(serRcvIntrvl.Elapsed) 'store the interval.  NOTE - if not emptied periodically the program will fail
                serRcvIntrvl.Restart()
            Else
                serRcvIntrvl.Start() 'first time
            End If
            While myPort.BytesToRead > 0
                inData = myPort.ReadLine
    
    
                Me.BeginInvoke((New myDelegate1(AddressOf Graph1Delegate)), n)
    
            End While
        End Sub
    


    <div>&quot;Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it&quot; - from former MSDN User JohnWein </div> <div><a href="http://en.wikipedia.org/wiki/Multics">Multics - An OS ahead of its time.</a>&emsp;<a href="https://social.technet.microsoft.com/wiki/contents/articles/42520.vb-net-serial-port-data-reception.aspx">Serial Port Info</a></div> <div></div>

    Thursday, March 22, 2018 12:42 PM
  • for example if I am receiving 500 pulses generated from an microcontroller.... Now I want to check the time of every single pulse received on my vb programme

    If you mean that you want to measure the time between bits on the serial data line, you can't do that with VB .Net - bit information is used in the serial port device and is not available within the application. If you mean that you want to measure the time between characters received on the serial data line then you can't do that with VB .Net without special device drivers. The closest you are going to get is the time between data received events, and that has only a very loose connection with the actual data transmission times.

    If you are actually generating pulses on the serial data line, not data, then that will not be recognised by the serial data port device, and the VB program will not receive any information to use for timing.

    If you are pulsing one of the serial port control lines then you can use the events that are raised for that line.  See:
    https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.pinchanged(v=vs.110).aspx


    • Edited by Acamar Thursday, March 22, 2018 9:51 PM fmt
    • Proposed as answer by Stanly Fan Friday, March 23, 2018 2:54 AM
    Thursday, March 22, 2018 9:48 PM
  • Thanks for your answer Acamar..

    let me clear it that I am reading the pulses generated by an transducer on an micro controller...

    And for every single pulse I am sending "1" to vb programme through serial communication..

    So with this method I am able to identify the total number of pulses generated by the transducer on my vb programme....

    But now I want to measure the time in which the "1" has been arrived at vb programme... 

     
    Friday, March 23, 2018 10:25 AM
  • let me clear it that I am reading the pulses generated by an transducer on an micro controller...

    The code to determine the time between pulses should be in the micro-controller - that is exactly the task that the micro-controller is good at.  The micro-controller then sends the time between pulses to the PC (for instance, as a number of milliseconds) using the serial connection.

    Friday, March 23, 2018 11:10 AM