Usb relay

From SundanceWiki
Jump to navigation Jump to search

The USB relay box contains one of these:

It is a simple device that converts USB to a serial UART / Comport.

When special commands are sent to the comport it either energises or de-engeriges the relay, switching between NC (Normally Connected) and NO (Normally Open).

So if a wire is routed through NO -> COM, when the relay is switched on the contacts close and the circuit completes. When switched off again it opens and breaks the circuit.

NC is the other way round.

Windows

In windows the easiest way I found of controlling it was to use a program called "SSCOM32E". I've saved a copy of this to our FTP server HERE.

When the box is plugged into your windows PC, check the device manager to see which comport it has been allocated.

Open the program and select the comport from the drop down box (Green).

Set the string that you need to send to change the state of the relay (Red) and then press Send (Blue).

You should hear the relay click and see a second LED come on through the slot.

The commands to send are "A0 01 01 A2" to switch it on - no quotes. "A0 01 00 A1" to switch it off - no quotes.

Linux

In Linux you don't need any additional programs.

To find out which USB device it has been allocated run this command:

dmesg | awk '/tty/ && /USB/ {print "/dev/"$10}'|tail -1

This will tell you the ttyUSB number of the last thing that was connected to the PC.

This may well change each and every time you run it.

To control the relay enter the following commands in terminal (changing the ttyUSB number to the one reported):

To switch on: echo -en '\xA0\x01\x01\xA2' > /dev/ttyUSB3

To switch off: echo -en '\xA0\x01\x00\xA1' > /dev/ttyUSB3



This script below is probably overkill, but will switch the device on and off as many times as you tell it to.


It starts by asking you to unplug and reattach the box so that it can get the device ID of the USB dongle. It saves this ID to a file, and then reads the file to a variable (Probably can be done better?)


The script then uses this as the device to switch the relay on and off.


 #!/bin/bash
 
 echo "Please unlpug and reconnect the USB relay box so we can get the device ID"

 read -p $'\e[32m\e[107m Press any key to continue...\e[0m'
 
 touch /temp/usbport.txt
 port=/temp/usbport.txt
 
 dmesg | awk '/tty/ && /USB/ {print "/dev/"$10}'|tail -1 > "$port"
 
 port2usb=$(<"$port")
 
 echo "It found that it's on: $port2usb"
 
 counter=1
 
 read -p "Enter number of loops required: " user_max_counter
 
 while [ $counter -le $user_max_counter ]
 do
 echo $'\e[32m\e[107m Loop number' $counter $'of ' $user_max_counter $'\e[0m'
 ((counter++)) 
 
 echo -en '\xA0\x01\x01\xA2' > "$port2usb"
 echo "Relay On!"
 
 sleep 3s 
 
 echo -e '\xA0\x01\x00\xA1' > "$port2usb"
 echo "Relay Off!"
 
 sleep 3s 
 
 done
 echo All done
 

The box itself

On the box there is a USB-B connection on the rear. Connect this to your PC.


The two black terminals are connected together. Nothing is switched.


The two red terminals are switched via the relay ONLY when the master control switch in on. If the switch is as per the photo, the relay is bypassed.


The relay is set to be off by default.