Serial Relay Control

From SifWiki

Revision as of 19:22, 20 February 2009 by Siftah (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

This relies on the usage of the following kit available from Quasar Electronics: http://www.quasarelectronics.com/3108.htm. This is a basic serial controlled board which has 8 relays and 4 opto-isolated inputs.

I've decided to control the board using Python as this seems to be a fairly competent language for doing serial stuff on Linux, plus, I fancied learning Python.

This bit of code will show when any of the inputs are high or low on the device:

#! /usr/bin/env python
import serial
import time
import os

ser = serial.Serial( port=0, baudrate=9600, bytesize=8, parity='N', 
	stopbits=1, timeout=2, xonxoff=0, rtscts=0, interCharTimeout=None )

while True:
	ser.write('I0\r')
	line1 = ser.readline().strip()
	response = int(ser.readline().strip(),16)
	if response != 0:
		theTime = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
		if (response & 1) == 1:
			print "Pin 1 was high at %s" % (theTime)
		if (response & 2) >> 1 == 1:
			print "Pin 2 was high at %s" % (theTime)
		if (response & 4) >> 2 == 1:
			print "Pin 3 was high at %s" % (theTime)
		if (response & 8) >> 3 == 1:
			print "Pin 4 was high at %s" % (theTime)
		time.sleep(5)
		
ser.close()          # close port

Well, it's a start!

To Do:

  • I think this needs some sort of daemon approach to monitor the inputs in a loop.
  • Add the ability to change and check the state of the relays with some kind of sensible messaging system.