Author Topic: Capture time on bit value  (Read 2135 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Capture time on bit value
« on: August 04, 2016, 08:44:14 PM »
Hi Archie.  I'm starting a project where I need to record time that a bit changes state in a Modbus device.  I will be using the Modbus TCP driver.  I want to keep it pretty simple by using something that resembles a label.  Looking at the toolbox, it looks like I can use the MessageDisplaybyBit control and write to its text field to display PC time using the BitChanged event.  My question is how do I access the bit "status" of this control?  I only want to write to the text of this control when the bit is off.  When the bit turns back on, I need to record that time in a different label.text as to not over write the time that it turned off.  Basically I need to timestamp when a bit goes off then again when it goes on.  Hope that makes sense.  Thanks.   

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5268
    • View Profile
    • AdvancedHMI
Re: Capture time on bit value
« Reply #1 on: August 04, 2016, 09:16:34 PM »
What about using a DataSubscriber and 2 Labels.

- From the All Windows Forms group in the Toolbox, add 2 Labels to the form
- Add a DataSubscriber to the form
- Set PLCAddressValue to the bit to monitor
- Double click the DataSubscriber
- Enter code like this:
Code: [Select]
    Private Sub DataSubscriber21_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber21.DataChanged
        If e.Values(0) = "True" Then
            Label1.Text = Now
        Else
            Label2.Text = Now
        End If
    End Sub

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Capture time on bit value
« Reply #2 on: August 04, 2016, 10:15:33 PM »
I had hoped to avoid datasubscribers because I plan to have near 100 controls and keeping track of vast numbers of datasubscribers is difficult. It is much easier if the control is on the form. Hoping for another way. Thanks.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5268
    • View Profile
    • AdvancedHMI
Re: Capture time on bit value
« Reply #3 on: August 05, 2016, 03:04:39 AM »
Another option would be to make a copy of the BasicLabel.vb file, give it a new name, then edit the code to make a custom control.

- At Line 30, change to Public Class TimeStampLabel
- Go to Line 519 and strip all of the code from the UpdateText subroutine
- Add this code:
Code: [Select]
Private LastValue
Private Sub UpdateText()
  If m_Value="True" and LastValue<>"True" then
     MyBase.Text=Now
  End If
  Lastvalue=m_Value
End Sub
 

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Capture time on bit value
« Reply #4 on: August 05, 2016, 10:14:41 AM »
This look good Archie!  Can we expand this and add a new property called "Trigger" and have a selectable option "on high" and "on low".  That way I can use the same control for when the bit turns off.  Thanks

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5268
    • View Profile
    • AdvancedHMI
Re: Capture time on bit value
« Reply #5 on: August 05, 2016, 01:19:42 PM »
In the Property Region, add this code:
Code: [Select]
    Public Enum TriggerTypeEnum
        RisingEdge
        FallingEdge
    End Enum

    Private m_TriggerType As TriggerTypeEnum = TriggerTypeEnum.RisingEdge
    Public Property TriggerType As TriggerTypeEnum
        Get
            Return m_TriggerType
        End Get
        Set(value As TriggerTypeEnum)
            m_TriggerType = value
        End Set
    End Property

Then modify the code from above to:
Code: [Select]
Private LastValue
Private Sub UpdateText()
  If m_TriggerTpe=TriggerTypeEnum.RisingEdge then
  If m_Value="True" and LastValue<>m_Value then
     MyBase.Text=Now
  End If
else
  If m_Value="False" and LastValue<>m_Value then
     MyBase.Text=Now
  End If
end if
  Lastvalue=m_Value
End Sub
 

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Capture time on bit value
« Reply #6 on: August 07, 2016, 10:48:08 PM »
Works nicely Archie!  One last thing, How do I use the Value property to enter text for this new control?  Apparently this code got deleted when I deleted the Update text Sub code.  Thanks. 

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Capture time on bit value
« Reply #7 on: August 07, 2016, 11:55:46 PM »
Just to clarify, I would like the text in the new control to show TimeStampLabel1,TimestampLabel2 etc... when I add them to the form.  Then it can be changed with the value property, thank you. 

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5268
    • View Profile
    • AdvancedHMI
Re: Capture time on bit value
« Reply #8 on: August 08, 2016, 09:36:03 AM »
Near the top of  the file, in the Properties region, find and modify the Value property similar to this:
Code: [Select]
    Public Property Value As String
        Get
            Return m_Value
        End Get
        Set(ByVal value As String)
            If value <> m_Value Then
                If value IsNot Nothing Then
                    m_Value = value
                   ' UpdateText()
                     MyBase.Text=value
                    OnvalueChanged(EventArgs.Empty)
                Else
                    '* version 3.99f
                    If m_Value <> "" Then OnvalueChanged(EventArgs.Empty)
                    m_Value = ""
                    MyBase.Text = ""
                End If
                '* Be sure error handler doesn't revert back to an incorrect text
                OriginalText = MyBase.Text
            End If
        End Set
    End Property

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Capture time on bit value
« Reply #9 on: August 08, 2016, 04:07:19 PM »
I commented out the code that removed the text property and that worked but this worked as well.  However,I also am not able to change fore color and back color. Not sure what other properties don't work yet until I experiment more.  Thank you!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5268
    • View Profile
    • AdvancedHMI
Re: Capture time on bit value
« Reply #10 on: August 08, 2016, 09:30:09 PM »
All of that stuff was controlled in the original UpdateText routine, so when it was removed, it removed a lot of the functionality that was part of the original BasicLabel.

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Capture time on bit value
« Reply #11 on: August 08, 2016, 09:38:35 PM »
Yes I see.  I had to comment out this code and now I can change the back color and same for the forecolor.  So far I don't need those other features such as Highlight forecolor, prefix, etc so I'm good to go.  I will put this in the feature request for a full functioning TimeStampLabel for a future release.  Thanks Archie!!

   'Private m_BackColor As Color = Color.Black
    'Public Shadows Property BackColor As Color
    '    Get
    '        Return m_BackColor
    '    End Get
    '    Set(value As Color)
    '        If m_BackColor <> value Then
    '            m_BackColor = value
    '            UpdateText()
    '        End If
    '    End Set
    'End Property

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Capture time on bit value
« Reply #12 on: August 08, 2016, 11:18:49 PM »
More FYI than an a problem but I just discovered the last proposed code change to the public property resulted in "true" and "false" rather than the PC time.  I had to revert to the original code and comment out the code to eliminate the text property. So far so good, thanks.