Project

General

Profile

RE: Port 22222 ยป SonarClient.py

Anonymous, 10/01/2014 02:07 PM

 
import socket
import cStringIO
import time

TCP_IP = '192.168.2.12'
TCP_PORT = 22222
BUFFER_SIZE = 4096


def read_until_space(data):
buf = []
ch = data.read(1)
while ch not in [" ", "\n", ""]:
buf.append(ch)
ch = data.read(1)
return "".join(buf)


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


def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.sendall("Read")
data = cStringIO.StringIO()
chunk = s.recv(BUFFER_SIZE)
while chunk:
data.write(chunk)
chunk = s.recv(BUFFER_SIZE)
# print "read %d bytes\n\n" % len(data.getvalue())
s.close()

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

# skip the next 3 ints...not sure what they are anyway
read_until_space(data) # 9
read_until_space(data) # 0
read_until_space(data) # 0

version = int(read_until_space(data))
timestamp = float(read_until_space(data))
ping_period_seconds = float(read_until_space(data))
gain = int(read_until_space(data))
pulse_length_seconds = float(read_until_space(data))
sound_velocity = float(read_until_space(data))
num_samples = int(read_until_space(data))
num_channels = int(read_until_space(data))

# now read the lists...
angles = read_list(data)
ranges = read_list(data)
magnitude = read_list(data)
raw_data = read_list(data, int)
end = time.clock()

data.close()

print " version: %d" % version
print " timestamp: %f" % timestamp
print " ping period (s): %f" % ping_period_seconds
print " gain: %d" % gain
print " pulse length (s): %f" % pulse_length_seconds
print "sound velocity (m/s): %f" % sound_velocity
print " samples: %d" % num_samples
print " channels: %d" % num_channels
print ""
print "Read %d angles" % len(angles)
print "Read %d ranges" % len(ranges)
print "Read %d magnitudes" % len(magnitude)
print "Read %d raw data points" % len(raw_data)
print ""
print "Deserialization took %f seconds" % (end - start)


if __name__ == "__main__":
main()
    (1-1/1)