继电器Relay:ZZR08

继电器常识:

      继电器有三个接线柱:常开(NO),常闭(NC),接地(C)

      如果连接的时间长,偶尔需要断电, 那么接NC 和 C, 这样继电器set on 时为断电。除此之外,继电器还可以控制按键,以及长按。

      继电器接通时,该继电器的灯会亮。ZZR08有8个继电器,以一个字节表示其整体状态,以每位的位高(bit high)代表对应继电器的状态。例如:第一个继电器set on 时为00000001,set off 时该位为0;第二个set on 时为00000010(2), set off 时该位为0。以此类推.

怎样使用继电器:

1. Command “ls /dev/tty*”, you can find like this
/dev/ttyRelayCard

    在接入继电器之前运行“ls /dev/tty*”, 接入之后再运行一次,多出来的就是继电器的标识。

 

2. Check the file /etc/udev/rules.d/ACS.rules. In this file, there have a code like

this:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60",
SYMLINK+="ttyRelayCard", MODE="0666", OWNER="buildbot"

If there don’t have this code, please add it by manual. And then re-plugin the

relay card’s USB cable.

    查看是否存在/etc/udev/rules.d/ACS.rules,如果不存在,则建立文件:touch ACS.rules,文件内容如上。

 

3. 查看权限:ls -l /dev/ttyRelayCard 结果crw-rw---- 1 root dialout.

    可以更改继电器权限:chmod a+rw /dev/ttyRelayCard. 这样坏处是,每次插拔继电器都要更改。

    继电器的user是root, group是dialout。可以使当前用户从属于dialout用户组。查看user的用户组:groups user。更改user的附属用户组list: sudo usermod -G dialout,adm,...

4. python script

import serial
import sys, time
'''
ser = serial.Serial(
port=None,              # number of device, numbering starts at
# zero. if everything fails, the user
# can specify a device string, note
# that this isn't portable anymore
# if no port is specified an unconfigured
# an closed serial port object is created
baudrate=9600,          # baud rate
bytesize=EIGHTBITS,     # number of databits
parity=PARITY_NONE,     # enable parity checking
stopbits=STOPBITS_ONE,  # number of stopbits
timeout=None,           # set a timeout value, None for waiting forever
xonxoff=0,              # enable software flow control
rtscts=0,               # enable RTS/CTS flow control
interCharTimeout=None   # Inter-character timeout, None to disable
)
'''

device = sys.argv[1] # /dev/ttyRelayCard
ser = serial.Serial(device, 19200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_TWO, None)
W = 0.05

def get_version():
    cmd = 90
    print "get the relay software version"
    ser.write(chr(cmd))
    time.sleep(W)
    a = ser.read(1)
    print ord(a)
    b = ser.read(1)
    print ord(b)
    time.sleep(1)

def set_relay_on(i):
    ''' relay No is from 1 to 8'''
    cmd = 100 + i
    print "set relay %d on "%(i)
    ser.write(chr(cmd))
    time.sleep(W)
    ser.write(chr(91)) # get relay states
    a = ser.read(1)
    print ord(a)
    time.sleep(W)

def set_relay_off(i):
    ''' relay No is from 1 to 8'''
    cmd = 110 + i
    print "set relay %d off"%(i)
    ser.write(chr(cmd))
    time.sleep(W)
    ser.write(chr(91))
    a = ser.read(1)
    print ord(a)
    time.sleep(W)
    
set_relay_off(1)
# set_relay_on(1)

 newer edition:

import serial
import sys, time
'''
base API of controlling Relay.
'''
class RelayControl(object):
    def __init__(self, relay_id='/dev/ttyACM0', interval=0.05):
        self.relay_id = relay_id
        self.interval = interval
        self.handler = serial.Serial(relay_id, 19200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_TWO, None)

    def get_version(self, cmd=90):
        print "get the relay software version"
        self.handler.write(chr(cmd))
        time.sleep(self.interval)
        a = self.handler.read(1)
        print ord(a)
        b = self.handler.read(1)
        print ord(b)
        time.sleep(1)

    def get_relay_status(self, cmd=91):
        self.handler.write(chr(cmd))
        a = self.handler.read(1)
        print ord(a)
        time.sleep(self.interval)
        return ord(a)

    def set_relay_on(self, relay_no):
        ''' relay No is from 1 to 8'''
        cmd = 100 + relay_no
        print "set relay %d on "%(relay_no)
        self.handler.write(chr(cmd))
        time.sleep(self.interval)
        self.get_relay_status()

    def set_relay_off(self, relay_no):
        ''' relay No is from 1 to 8'''
        cmd = 110 + relay_no
        print "set relay %d off"%(relay_no)
        self.handler.write(chr(cmd))
        time.sleep(self.interval)
        self.get_relay_status()

    def set_all_relay_on(self, cmd=100):
        print "set all relays on "
        self.handler.write(chr(cmd))
        time.sleep(self.interval)
        self.get_relay_status()

    def set_all_relay_on(self, cmd=110):
        print "set all relays off "
        self.handler.write(chr(cmd))
        time.sleep(self.interval)
        self.get_relay_status()

 

附录

ZZR08 command Table

 

Command Action
Character DEC HEX  
  90 5A Get software version - returns 2 bytes, the first being the module
ID which is 8, followed by the software version
  91 5B Get relay states - sends a single byte back to the controller, bit high
meaning the corresponding relay is powered
  92 5C Set relay states - the next single byte will set all relays states, All on
= 255 (11111111) ,All off = 0
d 100 64 All relays on
e 101 65 Turn relay 1 on
f 102 66 Turn relay 2 on
g 103 67 Turn relay 3 on
h 104 68 Turn relay 4 on
i 105 69 Turn relay 5 on
j 106 6A Turn relay 6 on
k 107 6B Turn relay 7 on
l 108 6C Turn relay 8 on
n 110 6E All relays off
o 111 6F Turn relay 1 off
p 112 70 Turn relay 2 off
q 113 71 Turn relay 3 off
r 114 72 Turn relay 4 off
s 115 73 Turn relay 5 off
t 116 74 Turn relay 6 off
u 117 75 Turn relay 7 off
v 118 76 Turn relay 8 off

 

 

posted @ 2015-12-02 17:10  道以万计  阅读(791)  评论(0编辑  收藏  举报