Jump to content
Sign in to follow this  

Better mousetrap for relay control

Recommended Posts

Hello. This is my first post and I am unfamaliar with the correct protocol. If I leave something out, please let me know what I can provide to help get an answer. My primary objective is to control the 110v power to a zone amplifier. I am using a PowerSwitch device which is triggered by low voltage. I am using the RTS lead (pins 5 & 7) on a RS232 serial cable via a Prolific USB/Serial adapter to provide the switch voltage.

My first pass at the working code is below. The simple design point is to call an executable once with the "ON" parameter and have the low voltage activate and stay on until the executable is called again later with the "OFF" parameter. The challenge is that the exe needs to return from first call, but doing so with a clean exit closes the port and releases RTS. Something needs to hang around and keep the port open. But then, I need to be able to talk to it when the "OFF" command is received to drop RTS.

I'm trying to find two ways of improvement. First, what is the most efficent way for a process to "hang around"? I'm using sleep loops now, but there's got to be something more system friendly out there. Second, what is best way to communicate with the spawned process to direct it to exit? I'm brute force killing it now, but once again I'm sure there's a better way. Any suggestions, examples or code snippets would be most appreciated.

The switching app:

Local $RTSLineApp
$RTSLineApp=@ScriptDir & "/RTShigh.exe" ;fully qualify
Switch StringLower($CmdLineRaw)
Case "on"
If ProcessExists("RTShigh.exe") Then ;don't run more than one instance
Case "off"
ProcessClose("RTShigh.exe")  ;kill process to close port, drop RTS
Case Else
MsgBox(0,"Usage", "Must be ON or OFF.")

The RTS app/ spawned process:

#include "CommMG.au3"    ;shoutout to Martin, CommMG guru
#include <misc.au3>
Local $PortErr
Local $myx
$myx = _CommSetPort(4,$PortErr) ;open COM4, default RTS high
If @error Then
MsgBox(16, "Error" & @error, $PortErr)
While 1

Thanks for any advice you can offer.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...