skip to Main Content

I bought a china usb 8 relay switch board. It shows up in ubuntu server as a serial device. I figured out the commands to get it to work.

*Set Baud Rate of relay board

stty -F /dev/ttyUSB0 9600 -parity cs8 -cstopb

To start relay controller:

echo -e -n "x50" > /dev/ttyUSB0 

To activate relay for commands:

echo -e -n "x51" > /dev/ttyUSB0

To turn on relay

echo -e -n "x(number)" > /dev/ttyUSB0*

But my issue is if I turn on relay 1 (/x01) and then turn on relay 2 (/x02) relay 1 turns off. I’m trying to automate and will need it to turn on more than one relay at a time. I went thru with testers and started mapping each number, Like 03 turns on relay 01 and 02. But there has to be an easier way. Any help would be grateful.

2

Answers


  1. It is a bitwise control.

    For example to turn on only port 0 (the first), you send xFE.

    (echo "obase=16" ; echo $((255 ^ 1)))|bc| sed -e 's/^/\x/'
    0xFE
    

    Essentially take 255 and subtract these values for each port you wish to start:
    |Port|Number|
    |–| —|
    |0|1|
    |1|2|
    |2|4|
    |3|8|
    |4|16|
    |5|32|
    |6|64|
    |7|128|

    To turn relay 1, 4, 5, and 7 you need to send:

    255 (all 8 bits stop)  - 2 (bit 1 start) - 16 (bit 4 start) - 128 (bit 7 start) = 109.
    

    In hex 109 is 6D, so send x6D to the device.

    Login or Signup to reply.
  2. A more visual example:

        #!/bin/bash 
        varfolder=/dev/shm
        
        for i in $(find -L /sys/bus/usb/devices/ -maxdepth 2 -name "ttyUSB*"); do
                usb=$(egrep -i "2303" $i/../modalias >/dev/null && echo "${i##*/}")
        check=$(grep PRODUCT= /sys/bus/usb-serial/devices/$usb/../uevent)
        if [ "$check" == "PRODUCT=67b/2303/300" ]; then
        releport=/dev/$usb
        fi
           done
        
        ## this word "modif" depends on the linux interface language
        modf_time=$(stat /sys/bus/usb-serial/devices/$usb | grep  modif)
        init_time=$(<$varfolder/init_usb_relay)
        
        if [ "$modf_time" != "$init_time" ] ; then 
        echo -e -n "x50" > $releport
        echo -e -n "x51" > $releport
        echo $modf_time > "$varfolder/init_usb_relay"
        fi
        
        # 87654321
        # turn off 6 and 5 relay
        relstatusbin=11001111
        relstatus16=$(echo "ibase=2;obase=10000;$relstatusbin"|bc)
        echo -e -n "x$relstatus16" > $releport
    

    Known issues:

    • If the USB wire does not carry enough electricity to hold the relay coil then the relay will not energize. In this case, additional power is required. Or simultaneously turn off a smaller number of relays.
    • Contacts can stick.
    • Contacts may burn.
    • If the device stopped accepting commands, then in order not to reboot Linux, you need to remove the device rm -f /dev/ttyUSB0
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search