Пример #1
0
def serial_cmd(cmd, serial):
    # send command to serial port
    try:
        serial.write(cmd + '\r');
        serial.reset_output_buffer()
    except:
        logging.info('Not connected')
Пример #2
0
def serial_read2(cmd, no, serial):
    # send command to serial port
    serial.reset_input_buffer()
    serial.reset_output_buffer()
    serial.write(cmd+'\r');

    # read data from serial port
    c = serial.read(no)
    return c
Пример #3
0
def serial_cmd(cmd, serial):
    # send command to serial port
    serial.write(cmd + '\r')
    serial.reset_input_buffer()
    serial.reset_output_buffer()
    serial.flush()

    # read data from serial port
    c = serial.read(1300)
    return c
Пример #4
0
def serial_read(cmd, no, serial):
    # send command to serial port
    serial.write(cmd + '\r')
    #serial.reset_input_buffer()
    serial.reset_output_buffer()
    serial.flush()

    # read data from serial port
    c = serial.read(no)
    if (len(c) == 0):
        return 'NO CONN'
    else:
        return c
Пример #5
0
def serial_read(cmd, serial):
    # send command to serial port
    serial.reset_input_buffer()
    serial.reset_output_buffer()
    serial.write(cmd+'\r');

    cnt = 0
    tracedData = []

    while True:
        line = serial.readline()
	cnt += 1
        #print cnt, line,
	tracedData.append(line)

	if (cnt > 240):
	    break

    return tracedData
Пример #6
0
ms = b'\x53\x6E\x69\x66'  #magic sequence 'Snif'

# initialize ports
port = "/dev/ttyACM0"
serial = serial.Serial(port,
                       baudrate=115200,
                       parity=serial.PARITY_NONE,
                       stopbits=serial.STOPBITS_ONE,
                       bytesize=serial.EIGHTBITS,
                       xonxoff=False,
                       rtscts=False,
                       write_timeout=None)

# reset I/O buffers of both sniffer and replayer
serial.reset_input_buffer()
serial.reset_output_buffer()

print "Serial port opened!"

# socket initialization and connection
server_ip = "127.0.0.1"
server_port_send = 9999
server_port_recv = 8888

client_socket_send = socket_manager(server_ip, server_port_send)
client_socket_recv = socket_manager(server_ip, server_port_recv)

# path of the log files
log_file_name_snif = "/home/hino/Scrivania/client_snif.log"
log_file_name_rply = "/home/hino/Scrivania/client_rply.log"
Пример #7
0
def Main(args):
    try:
        buff = ''
        if len(args) != 2:
            return 1

        serial.port = args[1]
        serial.baudrate = 115200
        serial.timeout = 10
        serial.open()

        framecount = 0

        while True:
            buff = input()
            if (buff == 'START'):
                #Set everything to zero to start
                framecount += 1
                b = 0
                y = 0
                select = 0
                start = 0
                up = 0
                down = 0
                left = 0
                right = 0
                a = 0
                x = 0
                lb = 0
                rb = 0
                home = 0
                app = 0
                click = 0
                volm = 0
                buff = input()
                mode = int(buff)
                if (mode == 1):
                    buff = input()
                    #<crapcode>
                    if (buff == ''):
                        buf = 0
                    else:
                        buf = float(buff)
                    if (buf < -0.55):
                        left = 1
                        right = 0
                    elif (buf < -0.35):
                        left = 1 if (framecount % 3 != 0) else 0
                        right = 0
                    elif (buf < -0.20):
                        left = 1 if ((framecount % 5) % 2 == 0) else 0
                        right = 0
                    elif (buf < -0.08):
                        left = 1 if (framecount % 2 == 0) else 0
                        right = 0
                    elif (buf > 0.55):
                        right = 1
                        left = 0
                    elif (buf > 0.35):
                        right = 1 if (framecount % 3 != 0) else 0
                        left = 0
                    elif (buf > 0.20):
                        right = 1 if ((framecount % 5) % 2 == 0) else 0
                        left = 0
                    elif (buf > 0.08):
                        right = 1 if (framecount % 2 == 0) else 0
                        left = 0
                    #</crapcode>
                    buff = input()
                    if (buff): home = 1
                    up = home
                    buff = input()
                    if (buff): app = 1
                    down = app
                    if (home or app): a = 1
                    buff = input()
                    if (buff): click = 1
                    lb = click
                    buff = input()
                    if (buff): volm = 1
                    start = volm
                    #<crapcode2.0>
                    buff = input()
                    if (buff == ''):
                        buf = 0
                    else:
                        buf = int(buff)
                    if (buf < 128 and buf > 0):
                        y = 1
                        b = 0
                    elif (buf > 128):
                        b = 1
                        y = 0
                    else:
                        b = 0
                        y = 0
                    buff = input()
                    pass
                    #</crapcode2.0>
                if (mode == 2):
                    buff = input()
                    pass
                    buff = input()
                    if (buff): home = 1
                    b = home
                    buff = input()
                    if (buff): app = 1
                    x = app
                    buff = input()
                    pass
                    buff = input()
                    pass
                    buff = input()
                    xTouch = int(buff) - 128 if buff else 0
                    buff = input()
                    yTouch = int(buff) - 128 if buff else 0
                    #Do the math to turn the touchpad to a d pad
                    if (xTouch != 0 and yTouch != 0
                            and (math.sqrt(xTouch**2 + yTouch**2)) > 55):
                        angle = (math.atan2(xTouch, yTouch) * 360 / 6.28) + 180
                        if (angle < 45 or angle > 315):
                            left = 1
                        elif (angle > 45 and angle < 135):
                            down = 1
                        elif (angle > 135 and angle < 225):
                            right = 1
                        elif (angle > 225 and angle < 315):
                            up = 1
            buff = ''

            controllerData = str(b) + str(y) + str(select) + str(start) + str(
                up) + str(down) + str(left) + str(right) + str(a) + str(
                    x) + str(lb) + str(rb) + '0000'
            contDataBytes = bitstring_to_bytes(controllerData)
            print(contDataBytes)

            serial.reset_output_buffer()
            serial.write(contDataBytes)

            echo = serial.read(len(contDataBytes))
            if len(echo) != len(contDataBytes):
                print("BAD!")

            #print(controllerData)
    except KeyboardInterrupt:
        sys.stdout.flush()
        serial.close()
        pass

    print("DONE")