Author Topic: Convert seconds to hr: min: sec:  (Read 3552 times)

khewes

  • Newbie
  • *
  • Posts: 12
    • View Profile
Convert seconds to hr: min: sec:
« on: May 27, 2016, 01:18:55 PM »
Hello all,
I was wondering if there was a component that would allow me to convert an integer value (seconds) into hrs: mins: sec: on advanced HMI.  I'm not seeming to find one.  if there is not one, is there a way to develop my own?  I'm not the best at coding...but im sure there is already code online for this feature.  I just do not know how to input the code and make a component out of it.

khewes

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Convert seconds to hr: min: sec:
« Reply #1 on: May 27, 2016, 02:36:02 PM »
is there anyway to get your digital panel meter to show decimal values instead of rounding?

dmroeder

  • Global Moderator
  • Full Member
  • *****
  • Posts: 208
    • View Profile
Re: Convert seconds to hr: min: sec:
« Reply #2 on: May 27, 2016, 03:18:15 PM »
Yes.  There is a property called DecimalPosition, set it to where you want the decimal point.  For example, if you set it to 1, it will put it to the left of the first digit (move it one decimal point).  Then you might have to change the ValueScaleFactor to properly scale it.  You can test all this while the app is not running by typing a value into the Value property.

So I have DecimalPosition set to 1.  ValueScaleFactor set to 10 and Value set to 3.5 and my panel meter displays 3.5 (where by default it would have rounded to 4).

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI
Re: Convert seconds to hr: min: sec:
« Reply #3 on: May 27, 2016, 07:16:14 PM »
I was wondering if there was a component that would allow me to convert an integer value (seconds) into hrs: mins: sec: on advanced HMI.  I'm not seeming to find one.  if there is not one, is there a way to develop my own?  I'm not the best at coding...but im sure there is already code online for this feature.  I just do not know how to input the code and make a component out of it.
I did a project that needed to display a value in hh:mm format. I made some minor modifications to the BasicLabel like this:

In Solution Explorer, expand down the AdvancedHMIControls project and expand down the Controls folder. Right click BasicLabel.vb and select View Code. Then make these changes:

In the Basic Properties Region, add this code:
Code: [Select]
    Private m_DisplayAsTime As Boolean
    Public Property DisplayAsTime As Boolean
        Get
            Return m_DisplayAsTime
        End Get
        Set(value As Boolean)
            m_DisplayAsTime = value
        End Set
    End Property

In the Private Methods Region, find the Private Sub UpdateText. Go down until you find End Sub. Insert the code as shown  here:
Code: [Select]
        If m_DisplayAsTime Then
            Try
                If Value <> "" And Not Me.DesignMode Then
                    Dim Hours As Double
                    Hours = CDbl(Value) * m_ValueScaleFactor
                    Dim Minutes As Double = (Hours - Math.Floor(Hours)) * 60
                    ResultText = Math.Floor(Hours) & ":" & Format(Math.Floor(Minutes), "00")
                End If
            Catch ex As Exception
                MyBase.Text = ex.Message
                Exit Sub
            End Try
        End If


        MyBase.Text = ResultText
    End Sub

You will need to modify the code if you want hh:mm:ss

Godra

  • Hero Member
  • *****
  • Posts: 1438
    • View Profile
Re: Convert seconds to hr: min: sec:
« Reply #4 on: June 01, 2016, 11:50:31 PM »
Another possible way is to use HourMeter control (the last post is using Archie's controls which have Value declared as Integer):

http://advancedhmi.com/forum/index.php?topic=1295.0


Also, just by placing a Label and Timer controls on the form, one could use the code similar to following (with each tick of a timer a driver would read the seconds value from the PLC):

Code: [Select]
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim remainder As Integer
        Label1.Text = Math.DivRem(CInt(Me.ModbusTCPCom1.Read("40001")), 3600, remainder) & ":" & Math.DivRem(remainder, 60, remainder) & ":" & remainder
    End Sub

khewes

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Convert seconds to hr: min: sec:
« Reply #5 on: June 06, 2016, 10:46:01 AM »
Quote
I did a project that needed to display a value in hh:mm format. I made some minor modifications to the BasicLabel like this:

In Solution Explorer, expand down the AdvancedHMIControls project and expand down the Controls folder. Right click BasicLabel.vb and select View Code. Then make these changes:

In the Basic Properties Region, add this code:
Code: [Select]
    Private m_DisplayAsTime As Boolean
    Public Property DisplayAsTime As Boolean
        Get
            Return m_DisplayAsTime
        End Get
        Set(value As Boolean)
            m_DisplayAsTime = value
        End Set
    End Property

In the Private Methods Region, find the Private Sub UpdateText. Go down until you find End Sub. Insert the code as shown  here:
Code: [Select]
        If m_DisplayAsTime Then
            Try
                If Value <> "" And Not Me.DesignMode Then
                    Dim Hours As Double
                    Hours = CDbl(Value) * m_ValueScaleFactor
                    Dim Minutes As Double = (Hours - Math.Floor(Hours)) * 60
                    ResultText = Math.Floor(Hours) & ":" & Format(Math.Floor(Minutes), "00")
                End If
            Catch ex As Exception
                MyBase.Text = ex.Message
                Exit Sub
            End Try
        End If


        MyBase.Text = ResultText
    End Sub

You will need to modify the code if you want hh:mm:ss


Im having trouble getting this function to work.  I followed your instructions and my output is just the plc value that the hmi is receiving ("4500" counting down) and then ":00" . I dont mean for you to do my work for me, but some advice would be much appreciated.  If i have not said this yet, I am a huge newb to programming, but I do see in your code that you have "00" in your private methods part...should this not be a variable instead?

khewes

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Convert seconds to hr: min: sec:
« Reply #6 on: June 06, 2016, 11:03:54 AM »
I changed it up just a tad and it is working.  Im going to add the seconds part and then I will post my solution, Thanks everyone for the input this is saving me memory on my PLC and thus I do not have to upgrade to a new PLC! saving the company money :]]]]]

khewes

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Convert seconds to hr: min: sec:
« Reply #7 on: June 06, 2016, 11:11:45 AM »
I modified Archies code from above to be Hrs:mm:ss....here it is below, including Archies instructions to introduce it as a component.  Thank you very much all.


Quote
I did a project that needed to display a value in hh:mm format. I made some minor modifications to the BasicLabel like this:

In Solution Explorer, expand down the AdvancedHMIControls project and expand down the Controls folder. Right click BasicLabel.vb and select View Code. Then make these changes:

In the Basic Properties Region, add this code:
Code: [Select]
    Private m_DisplayAsTime As Boolean
    Public Property DisplayAsTime As Boolean
        Get
            Return m_DisplayAsTime
        End Get
        Set(value As Boolean)
            m_DisplayAsTime = value
        End Set
    End Property

In the Private Methods Region, find the Private Sub UpdateText. Go down until you find End Sub. Insert the code as shown  here:
Code: [Select]
        If m_DisplayAsTime Then
            Try
                If Value <> "" And Not Me.DesignMode Then
                    Dim Hours As Double
                    Hours = CDbl(Value) * .00027777777778
                    Dim Minutes As Double
                    Minutes = (Hours - Math.Floor(Hours)) * 60
                    Dim Seconds as Double
                    Seconds = (Minutes - Math.Floor(Minutes)) * 60
                    ResultText = Math.Floor(Hours) & ":" & (Math.Floor(Minutes), & ":" & (Math.Floor(Seconds)
                End If
            Catch ex As Exception
                MyBase.Text = ex.Message
                Exit Sub
            End Try
        End If


        MyBase.Text = ResultText
    End Sub
« Last Edit: June 06, 2016, 11:13:20 AM by khewes »