示例#1
0
def start_client(args):
    trace.writeline('start client')

    sckt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sckt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sckt.bind(my_addr)
    sckt.settimeout(10)

    timeouted = 0
    try:
        for i in range(100):
            # print 'Type digit [0-9] >',
            # sys.stdout.flush()
            #c = raw_input()

            digit = i % 10
            c = chr(digit + 48)

            print i, c, '-->', xbee_addr

            sckt.sendto(c, xbee_addr)
            try:
                msg, addr = sckt.recvfrom(32)
                trace.writeline("Received ->", msg, addr)
            except socket.timeout as e:
                print e
                timeouted += 1
    finally:
        sckt.close()
        print 'timeouted =', timeouted
示例#2
0
    def get_adc(channel):

        # Only 2 channels 0 and 1 else return -1
        if ((channel > 1) or (channel < 0)):
            return -1

        cmd = 0b01101000 | (channel << 4)
        trace.writeline('cmd=', cmd)
        r = spi.xfer2([cmd, 0x00])
        trace.writeline('r=', r)
        return ((r[0] << 8) + r[1]) & 0x03FF
示例#3
0
def test():
    sd = serial.Serial(serial_dev,
                       baudrate=115200,
                       bytesize=serial.EIGHTBITS,
                       parity=serial.PARITY_NONE,
                       stopbits=serial.STOPBITS_ONE,
                       timeout=5)
    print sd
    for i in range(1000):
        digit = i % 10
        c = chr(digit + 48)
        trace.writeline(i, '>>', c)
        sd.write(c)
        confirm_ack(sd)
        time.sleep(0.01)
    sd.close()
示例#4
0
def xfer(clockpin, mosipin, misopin, cspin, dataout):

    datain = []

    GPIO.output(cspin, True)
    GPIO.output(clockpin, False)  # start clock low
    GPIO.output(cspin, False)  # bring CS low

    for tran in dataout:
        recv = 0x00

        for i in range(8):

            #
            # transmit data
            # Set MOSI to Hi/Low
            #
            if tran & 0x80:
                trace.writeline('OUT:', i, '1')
                GPIO.output(mosipin, True)
            else:
                trace.writeline('OUT:', i, '0')
                GPIO.output(mosipin, False)
            tran <<= 1

            #
            # recieve data
            # Read MISO
            #
            recv <<= 1
            if GPIO.input(misopin):
                trace.writeline('IN', i, '1')
                recv |= 0x01
            else:
                trace.writeline('IN', i, '0')

            # Clock in
            GPIO.output(clockpin, True)
            GPIO.output(clockpin, False)

        datain.append(recv)

    assert len(dataout) == len(datain)

    print '[SPI MOSI]:',
    for v in dataout:
        print format(v, '08b'),
    else:
        print '.'

    print '[SPI MISO]:',
    for v in datain:
        print format(v, '08b'),
    else:
        print '.'

    return datain
def start_client(args):
    trace.writeline("Start listening...")

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(my_addr)
    sock.settimeout(10)

    try:
        while True:
            try:
                frame, addr = sock.recvfrom(32)
                print datetime.datetime.now(), "Received -> %d bytes from " % len(frame), addr
                process_frame(frame)
            except socket.timeout:
                pass
    finally:
        sock.close()
示例#6
0
def confirm_ack(sd):
    ack = sd.read(3)
    trace.writeline('<<', ack)
    assert ack.upper() == 'ACK'