Author Topic: Write Time to PLC  (Read 2448 times)

DavidSr

  • Full Member
  • ***
  • Posts: 170
    • View Profile
Write Time to PLC
« on: January 03, 2019, 10:41:33 PM »
As you probably know the PLC 5 clock drifts a bit, Ours gains time and it has no way to directly communicate with an NTP server so. .. I am not a VB programmer so I wondered if ADVHMI has a way to read the PC Clock or poll a NTP server and write the value to the PLC, Of course it would take several writes for each word of the time and date. 

David
David

Phrog30

  • Guest

DavidSr

  • Full Member
  • ***
  • Posts: 170
    • View Profile
Re: Write Time to PLC
« Reply #2 on: January 04, 2019, 07:29:36 PM »
Thank You!

 I will check that out and let you know--  MC uses pretty much the same format.

------David

David

DavidSr

  • Full Member
  • ***
  • Posts: 170
    • View Profile
Re: Write Time to PLC
« Reply #3 on: April 07, 2019, 05:03:09 AM »
https://www.advancedhmi.com/forum/index.php?topic=2239.msg12934#msg12934

This may work:

Code: [Select]
Dim localZone As TimeZone = TimeZone.CurrentTimeZone
'
AddHandler SystemEvents.TimeChanged, AddressOf SystemEvents_TimeChanged


Private Sub SystemEvents_TimeChanged(ByVal sender As Object, ByVal e As EventArgs)
      'Occurs when user manually changes the time or due to daylight saving time.                 
       Dim daylight As DaylightTime = localZone.GetDaylightChanges(DateTime.Now.Year)
       If localZone.IsDaylightSavingTime(DateTime.Now) Then           
            'If daylight.Start = DateTime.Now Or daylight.End = DateTime.Now Then
                'DST occur write to PLC
                PLC.Write("B3:0/14", 1)                 'TimeChange Enable bit
                PLC.Write("N9:3", DateTime.Now.Hour)
                PLC.Write("N9:4", DateTime.Now.Minute)
                PLC.Write("N9:5", DateTime.Now.Second)
                PLC.Write("B3:0/14", 0)
            'End If
       End If
End Sub


Trying to get a handle on this from that post: I want to grab the PC clock time which is updated every day from  time server   tick.usno.navy.mil or  tock.usno.navy.mil
  So,  If I set up a Timer in Advanced HMI for what ever time interval I want to send a time correction to the PLC.    I want to change the code in bachpi's post to chage time on a time interval but I can only imagine what I need to change in part of it.

So I started but got stuck. My changes thus far are in red, I only somewhat understand the code.


Dim localTimeAs PCTime = PCTime.CurrentTime
'
AddHandler SystemEvents.TimeChanged, AddressOf SystemEvents_TimeChanged


Private Sub SystemEvents_TimeChanged(ByVal sender As Object, ByVal e As EventArgs)
      'Occurs on Time interval.                 
       Dim time As TimeserverTime = NTPTime.GetTimeServerTime(DateTime.Now.Year)
       If localZone.IsDaylightSavingTime(DateTime.Now) Then           
            ' Time Interval ......                                             If daylight.Start = DateTime.Now Or daylight.End = DateTime.Now Then
                ' Time Interval Write                                    DST occur write to PLC
                PLC.Write("B3:0/0, 1)                 'TimeChange Enable bit
                PLC.Write("N16:0", DateTime.Now.Hour)
                PLC.Write("N16:1", DateTime.Now.Minute)
                PLC.Write("N16:2", DateTime.Now.Second)
                PLC.Write("B3:0/0", 0)
            'End If
       End If
End Sub



Two options for PLC 5/20 Logic
I like the second option best but some people get tripped up when you compact the logic like that.
« Last Edit: April 07, 2019, 08:18:26 AM by DavidSr »
David

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Write Time to PLC
« Reply #4 on: April 08, 2019, 09:35:25 AM »
Not sure if this is the reason for your problem, but I think you have to write the data first, then enable the time change bit, you can use a one shot to avoid having to turn it of, as it can be too fast for the PLC, and use a timer in the PLC to turn that signal off after a while.

Code: [Select]
Private Sub SystemEvents_TimeChanged(ByVal sender As Object, ByVal e As EventArgs)
      'Occurs on Time interval.                 
       Dim time As TimeserverTime = NTPTime.GetTimeServerTime(DateTime.Now.Year)
       If localZone.IsDaylightSavingTime(DateTime.Now) Then           
            ' Time Interval ......                                             If daylight.Start = DateTime.Now Or daylight.End = DateTime.Now Then
                ' Time Interval Write                                    DST occur write to PLC
                PLC.Write("N16:0", DateTime.Now.Hour)
                PLC.Write("N16:1", DateTime.Now.Minute)
                PLC.Write("N16:2", DateTime.Now.Second)
                PLC.Write("B3:0/0, 1)                 'TimeChange Enable bit
               
            'End If
       End If
End Sub



DavidSr

  • Full Member
  • ***
  • Posts: 170
    • View Profile
Re: Write Time to PLC
« Reply #5 on: April 08, 2019, 01:02:15 PM »
Not sure if this is the reason for your problem, but I think you have to write the data first, then enable the time change bit, you can use a one shot to avoid having to turn it of, as it can be too fast for the PLC, and use a timer in the PLC to turn that signal off after a while.

My problem is the VB code part,   I would say you are correct about that, I got the code from a referal by James from another post  by bachphi you can see in this thread. I don't know VB and I don't have time to really dig into it  to learn it well enough to stand on my own two feet so to speak. I don't mind plunking around and trying to figure out what I need for what I am doing but unfortunately I really don't have time to study it.  So I sometimes come here and beg for help on the VB side of it from anyone that can at least help me help myself. I will keep playing with the code and try to get the HMI side to build without errors. 

The code provided is a great help but I need to figure how to modify it for my application and their in is my difficulty.    I would just like to know if I am heading in the right direction.

The PLC side is not problem, I didn't bother showing all the actual logic. The Logic I did show is just some logic I put in a blank program. I have not put anything in the PLC yet.
 A one shot might be needed, that depends on what I use to write to the bit from the HMI,  Have not really thought about it much until I can at least write to the PLC from the HMI with this VB Code sending the PC time to the PLC words 

The PLC side will be easy, very basic programming. I am already doing it manually from the HMI but obviously that is not a long term workable solution.

Thank you!
« Last Edit: April 08, 2019, 01:09:57 PM by DavidSr »
David

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Write Time to PLC
« Reply #6 on: April 08, 2019, 05:00:15 PM »
If you already have a timer setup for interval, then double click  that timer, it will bring you the tick event and enter something like below:
Code: [Select]
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ' Time Interval Write                                    DST occur write to PLC
                PLC.Write("N16:0", DateTime.Now.Hour)
                PLC.Write("N16:1", DateTime.Now.Minute)
                PLC.Write("N16:2", DateTime.Now.Second)
                PLC.Write("B3:0/0, 1)                 'TimeChange Enable bit
    End Sub

btw, do you not want to turn the PLC code off afterwards?

« Last Edit: April 08, 2019, 05:03:29 PM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

DavidSr

  • Full Member
  • ***
  • Posts: 170
    • View Profile
Re: Write Time to PLC
« Reply #7 on: April 08, 2019, 07:36:06 PM »
If you already have a timer setup for interval, then double click  that timer, it will bring you the tick event and enter something like below:

btw, do you not want to turn the PLC code off afterwards?

Thank you !! -   I will try that this evening.

Yes I do want to turn it off I just haven't messed with anything in the PLC part yet, what I showed  in the images was just to illustrate how I would write the values to the PLC registers. I would reset the enable bit as soon as the plc clock is updated. I would have to do that or the PLC Clock would always have the same time on it until the next interval. Otherwise I would have to do a write every second.  Not too smart....

Do you have suggestions on using VB code to poll a time server instead of writing the PC Clock values?  My favorites are naturally US Navy's Tick and Tock
« Last Edit: April 08, 2019, 07:41:01 PM by DavidSr »
David

DavidSr

  • Full Member
  • ***
  • Posts: 170
    • View Profile
Re: Write Time to PLC
« Reply #8 on: April 08, 2019, 09:28:40 PM »
If you already have a timer setup for interval, then double click  that timer, it will bring you the tick event and enter something like below:
Code: [Select]
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        ' Time Interval Write                                    DST occur write to PLC
                PLC.Write("N16:0", DateTime.Now.Hour)
                PLC.Write("N16:1", DateTime.Now.Minute)
                PLC.Write("N16:2", DateTime.Now.Second)
                PLC.Write("B3:0/0, 1)                 'TimeChange Enable bit
    End Sub

btw, do you not want to turn the PLC code off afterwards?

I am getting this error every time the timer reaches set point:

************** Exception Text **************
System.NullReferenceException: Object variable or With block variable not set.
   at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance)
   at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
   at MfgControl.AdvancedHMI.MainForm.PLC_TIME_Tick(Object sender, EventArgs e) in C:\Program Files\AdvancedHMI\BETA_30\AdvancedHMI\MainForm.vb:line 216
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

After doing some research I modified the code slightly to this:-



Code: [Select]
Private Sub PLC_TIME_Tick(sender As Object, e As EventArgs) Handles PLC_TIME.Tick        ' Time Interval Write time to PLC
        PLC.Write("N14:50", Date.Now.Hour)
        PLC.Write("N14:51", Date.Now.Minute)
        PLC.Write("N14:52", Date.Now.Second)
        PLC.Write("B3:10/9, 1")  'Write Time Enable bit     
    End Sub
When I get it to write, I will reset the enable bit in the plc after the write to the RTC
« Last Edit: April 08, 2019, 09:46:56 PM by DavidSr »
David