Results 1 to 6 of 6

Thread: [2008] COM ports, timers and relays HELP!

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    7

    Exclamation [2008] COM ports, timers and relays HELP!

    I am putting together a relay timing program that is going to control 16 different relays, with at least 2 on/off points each daily.

    This program is going to run 24/7. Each on/off point is going to be user selected by a DateTimePicker (time only. no date). I want to set the timer intervals that trigger my relay on/off commands to only fire when needed, if possible because when you try to manually activate the relay with a on/off button, the relay command associated with the timer is still active and firing every timer interval, thus making the button useless. It also ties up the network with "useless" traffic.

    I started making a program but with 32+ timers it was getting a little confusing and the network was very busy. Can this be done with only a couple of timers? ...And if so can they only fire when needed so I can have an On/Off button option?

    BTW, I'm shooting for a timer interval of 1-2 seconds (5 seconds if need be)

    Here is a basic timing program I made with 2 on/off timers and an on and an off button

    If someone can show me this in detail (and i mean in great detail because what you see below is nearly the extent of my VB skills...I mean like edit a copy of my code and post it. lol ) I would greatly appreciate it.


    Code:
    Public Class Form1
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim t As Date                               'timer 1 ON/OFF
            t = Date.Now
            If t > DateTimePicker1.Value And t < DateTimePicker2.Value Then
                TurnOnRelay()
            Else
                TurnOffRelay()
            End If
        End Sub
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim t As Date                                'timer 2 ON/OFF
            t = Date.Now
            If t > DateTimePicker4.Value And t < DateTimePicker3.Value Then
                TurnOnRelay()
            Else
                TurnOffRelay()
            End If
        End Sub
    
        Private Sub TurnOnRelay()     'Relay 1 ON command
            Dim data(0 To 3) As Byte
            data(0) = 254
            data(1) = 108
            data(2) = 1
            SerialPort1.Write(data, 0, 3)
        End Sub
    
        Private Sub TurnOffRelay()       'Relay 1 OFF command
            Dim data(0 To 3) As Byte
            data(0) = 254
            data(1) = 100
            data(2) = 1
            SerialPort1.Write(data, 0, 3)
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TurnOnRelay()                     ' On Button
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            TurnOffRelay()                    'Off Button
        End Sub
    End Class

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] COM ports, timers and relays HELP!

    I'd think you can get away with 1 timer for each relay and 1 "master" timer to start the relay's timer. And here is the sketch:

    1. Use a 2-d Date array to store the start and stop time of each relay.

    2. The master timer interval can be set to 1 second. In the Tick event handler, you check the current time to the relays' start time's, it they equal, do:
    a) set that relay's timer.interval = the timespan of (stop time - start time)
    b) relay's timer
    c) turn on the relay

    3. In each relay's timer tick event handler, you do:
    a) stop the timer
    b) turn off the relay.

    In other words, the master timer will handle turning on the relays and starting the relay timers. Individual relay timers will turn of the relay and stop itself.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] COM ports, timers and relays HELP!

    1. What kind of resolution for on / off times, meaning how exact are the on / off times, ms., secs., minutes, etc. If the resolution is 1-2 seconds and there are only 16 devices, one timer should suffice.

    2. Is the command format
    byte = 254
    byte = 108 = on, 100 = off
    byte = relay#

    3. Make certain that you set the writetimeout for the serial port. The default is to wait indefinitely for the write to complete.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    7

    Re: [2008] COM ports, timers and relays HELP!

    stanav, could you give me an idea of the VB commands to use?


    dbasnett, I sent you an email earlier today regarding my previous post.

    The resolution could be 1 minute max. I would much rather have a 1-5 second resolution if possible.

    The command format is:

    Byte: Description:
    254 = Enter Command Mode
    100 = Relay "0" OFF in specified bank (relay 1)
    101 = Relay "1" OFF in specified bank (relay 2)
    108 = Relay "0" ON in specified bank (relay 1)
    109 = Relay "1" ON in specified bank (relay 2)
    1 = *On relay bank 1

    *all relays bank will be 1; There will be 8 different COM ports only using bank one and relay 1 & 2.

    EDIT: Here is the Command Set PDF: http://www.electricdesignonline.com/...mand%20Set.pdf
    Last edited by darkgrey3k; Sep 11th, 2008 at 02:20 PM.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] COM ports, timers and relays HELP!

    Code:
        Const numRelays As Integer = 16
        Const ctOfRelays As Integer = numRelays - 1
        Dim relayA() As relays = New relays(ctOfRelays) {}
        Public Structure relays
            Dim relay As Byte 'relay id
            Dim comP As Byte 'com port ID
            Dim onTime As DateTime 'time on
            Dim offTime As DateTime 'time off
            Dim Rstate As Boolean 'on or off currently
        End Structure
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            For x As Integer = 0 To ctOfRelays
                If relayA(x).onTime >= DateTime.Now AndAlso relayA(x).offTime <= DateTime.Now Then
                Else
                End If
            Next
        End Sub
    Last edited by dbasnett; Sep 12th, 2008 at 03:32 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    7

    Re: [2008] COM ports, timers and relays HELP!

    dbasnett, How would I incorporate the datetimepicker and my relay on/off into this?


    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width