Ubuntu server, 8 relay usb board need to turn on more than one at ounce

Viewed 46

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.

1 Answers

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.

Related