Project

General

Profile

RE: Port 22222 ยป SonarClient.py

Anonymous, 10/01/2014 02:07 PM

 
1
import socket
2
import cStringIO
3
import time
4

    
5
TCP_IP = '192.168.2.12'
6
TCP_PORT = 22222
7
BUFFER_SIZE = 4096
8

    
9

    
10
def read_until_space(data):
11
    buf = []
12
    ch = data.read(1)
13
    while ch not in [" ", "\n", ""]:
14
        buf.append(ch)
15
        ch = data.read(1)
16
    return "".join(buf)
17

    
18

    
19
def read_list(data, cls=float):
20
    list_data = []
21
    list_size = int(read_until_space(data))
22
    # skip the first value since it's not a valid value...not sure what it's for!
23
    read_until_space(data)  # 0
24
    for x in xrange(list_size):
25
        list_data.append(cls(read_until_space(data)))
26
    return list_data
27

    
28

    
29
def main():
30
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
31
    s.connect((TCP_IP, TCP_PORT))
32
    s.sendall("Read")
33
    data = cStringIO.StringIO()
34
    chunk = s.recv(BUFFER_SIZE)
35
    while chunk:
36
        data.write(chunk)
37
        chunk = s.recv(BUFFER_SIZE)
38
    # print "read %d bytes\n\n" % len(data.getvalue())
39
    s.close()
40

    
41
    # attempt to deserialize
42
    start = time.clock()
43
    data.reset()
44
    header = data.read(int(read_until_space(data)) + 1)
45
    if header.strip() != "serialization::archive":
46
        print "not Boost archive serialized: %s" % header
47
        return
48

    
49
    # skip the next 3 ints...not sure what they are anyway
50
    read_until_space(data)  # 9
51
    read_until_space(data)  # 0
52
    read_until_space(data)  # 0
53

    
54
    version = int(read_until_space(data))
55
    timestamp = float(read_until_space(data))
56
    ping_period_seconds = float(read_until_space(data))
57
    gain = int(read_until_space(data))
58
    pulse_length_seconds = float(read_until_space(data))
59
    sound_velocity = float(read_until_space(data))
60
    num_samples = int(read_until_space(data))
61
    num_channels = int(read_until_space(data))
62

    
63
    # now read the lists...
64
    angles = read_list(data)
65
    ranges = read_list(data)
66
    magnitude = read_list(data)
67
    raw_data = read_list(data, int)
68
    end = time.clock()
69

    
70
    data.close()
71

    
72
    print "             version: %d" % version
73
    print "           timestamp: %f" % timestamp
74
    print "     ping period (s): %f" % ping_period_seconds
75
    print "                gain: %d" % gain
76
    print "    pulse length (s): %f" % pulse_length_seconds
77
    print "sound velocity (m/s): %f" % sound_velocity
78
    print "             samples: %d" % num_samples
79
    print "            channels: %d" % num_channels
80
    print ""
81
    print "Read %d angles" % len(angles)
82
    print "Read %d ranges" % len(ranges)
83
    print "Read %d magnitudes" % len(magnitude)
84
    print "Read %d raw data points" % len(raw_data)
85
    print ""
86
    print "Deserialization took %f seconds" % (end - start)
87

    
88

    
89
if __name__ == "__main__":
90
    main()
    (1-1/1)
    Go to top
    Add picture from clipboard (Maximum size: 1 GB)