I´m trying to send sensor data to java app over sockets, the program works correctly but when it spends several minutes, the program crashes sending error 60 "Operation timed out" on the sockets threads.
But the orientation sensor still works correctly. Only crash the accel and rotation sensors.
What is my mistake?
Code:from sensor import * import e32 import socket import threading import time, calendar DEBUG = False # The remote host HOST = "localhost" # The same port as used by the servers ACCELERATION_PORT = 1235 ROTATION_PORT = 1236 ORIENTATION_PORT = 1237 # Split flag SEPARATOR = "~" class SendSocketThread(threading.Thread): def __init__(self, data, port): threading.Thread.__init__(self) self.data = data self.lock = threading.RLock() # Gives support to lock/unlock the thread itself self.send = False self.exit = False self.port = port # Sends data to the server socket when it receives the data from the sensor. def run(self): while self.exit != True: while self.send: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(None) # keep alive s.setblocking(1) s.connect((HOST, self.port)) total_sent = s.send(self.data) if DEBUG: print("Bytes sended " + str(total_sent)) print("Sended data: " + self.data) s.close() self.send = False def setData(self, data): self.lock.acquire() # Locks the thread itself self.data = data self.send = True self.lock.release() # Unlocks the thread itself def stop(self): self.exit = True class OrientationMonitor(): def __init__(self): self.monitor_timestamp = 0 self.monitor_orientation = "Unknown" self.sendSocketThread = SendSocketThread(self.monitor_orientation + SEPARATOR + str(self.monitor_timestamp), ORIENTATION_PORT) self.sendSocketThread.start() self.orientation = OrientationData() self.orientation.set_callback(data_callback = self.send) def send(self): self.monitor_orientation = get_logicalname(DeviceOrientation, self.orientation.device_orientation) self.monitor_timestamp = int(time.time() * 1000) if DEBUG: print ("Gets Orientation: " + self.monitor_orientation + " Timestamp: " + str(self.monitor_timestamp)) self.sendSocketThread.setData(self.monitor_orientation + SEPARATOR + str(self.monitor_timestamp)) def run(self): self.orientation.start_listening() def stop(self): self.orientation.stop_listening() self.sendSocketThread.stop() class RotationMonitor(): def __init__(self): self.monitor_timestamp = 0 self.monitor_rot_x = 0 self.monitor_rot_y = 0 self.monitor_rot_z = 0 self.sendSocketThread = SendSocketThread(str(self.monitor_rot_x) + SEPARATOR + str(self.monitor_rot_y) + SEPARATOR + str(self.monitor_rot_z) + SEPARATOR + str(self.monitor_timestamp), ROTATION_PORT) self.sendSocketThread.start() self.rotation = RotationData() self.rotation.set_callback(data_callback = self.send) self.counter = 0 def send(self): # For stream sensor data the callback is hit 35 times per sec(On 5800). # The device cannot handle resource hungry operations like print in the # callback/send function for such high frequencies. A workaround is to # sample the data as demonstrated below. # 8 is 0.25, 18 is 0.5 if self.counter == 12: self.monitor_rot_x = self.rotation.x self.monitor_rot_y = self.rotation.y self.monitor_rot_z = self.rotation.z # it does not work correctly #self.monitor_timestamp = self.rotation.timestamp self.monitor_timestamp = long(time.time() * 1000) if DEBUG: print ("ROTATION x: " + str(self.monitor_rot_x) + " y: " + str(self.monitor_rot_y) + " z: " + str(self.monitor_rot_z) + " Timestamp:", str(self.monitor_timestamp)) self.sendSocketThread.setData(str(self.monitor_rot_x) + SEPARATOR + str(self.monitor_rot_y) + SEPARATOR + str(self.monitor_rot_z) + SEPARATOR + str(self.monitor_timestamp)) self.counter = 0 self.counter = self.counter + 1 def run(self): self.rotation.start_listening() def stop(self): self.rotation.stop_listening() self.sendSocketThread.stop() class AccelerationMonitor(): def __init__(self): self.monitor_timestamp = 0 self.monitor_acel_x = 0 self.monitor_acel_y = 0 self.monitor_acel_z = 0 self.sendSocketThread = SendSocketThread(str(self.monitor_acel_x) + SEPARATOR + str(self.monitor_acel_y) + SEPARATOR + str(self.monitor_acel_z) + SEPARATOR + str(self.monitor_timestamp), ACCELERATION_PORT) self.sendSocketThread.start() self.accelerometer = AccelerometerXYZAxisData(data_filter = LowPassFilter()) self.accelerometer.set_callback(data_callback = self.send) self.counter = 0 def send(self): # For stream sensor data the callback is hit 35 times per sec(On 5800). # The device cannot handle resource hungry operations like print in the # callback/send function for such high frequencies. A workaround is to # sample the data as demonstrated below. # 8 is 0.25, 18 is 0.5 if self.counter == 12: self.monitor_acel_x = self.accelerometer.x self.monitor_acel_y = self.accelerometer.y self.monitor_acel_z = self.accelerometer.z # it does not work correctly #self.monitor_timestamp = self.accelerometer.timestamp self.monitor_timestamp = long(time.time() * 1000) if DEBUG: print ("ACCELERATION x: " + str(self.monitor_acel_x) + " y: " + str(self.monitor_acel_y) + " z: " + str(self.monitor_acel_z) + " Timestamp:", str(self.monitor_timestamp)) self.sendSocketThread.setData(str(self.monitor_acel_x) + SEPARATOR + str(self.monitor_acel_y) + SEPARATOR + str(self.monitor_acel_z) + SEPARATOR + str(self.monitor_timestamp)) self.counter = 0 self.counter = self.counter + 1 def run(self): self.accelerometer.start_listening() def stop(self): self.accelerometer.stop_listening() self.sendSocketThread.stop() if __name__ == "__main__": acceleration = AccelerationMonitor() rotation = RotationMonitor() orientation = OrientationMonitor() acceleration.run() rotation.run() orientation.run() print "Running..."
Thanks!!

Reply With Quote

