7

I want to get data from the color sensor with Bluetooth. I can get data but when the program terminates, the color sensor dose not close even though I call function close() on the color sensor. The following code is what I did with EV3 Bluetooth connection.

RemoteEV3 ev3 = new RemoteEV3("10.0.1.1");
ev3.setDefault();
Keys keys1 = ev3.getKeys();
color_sensor = new EV3ColorSensor(SensorPort.S2);

do{
  color_id = color_sensor.getColorID();
  System.out.println("Here is color id: "+color_id);
  Delay.msDelay();
  keys1.waitForAnyPress();
}while(keys.getButtons()!=Keys.ID_ESCAPE);

color_sensor.close();

And it throws the following exceptions:

Exception in thread "main" lejos.hardware.DeviceException: unable to open port
at lejos.internal.ev3.EV3Port.open(Unknown Source)
at lejos.hardware.motor.BaseRegulatedMotor.<init>(Unknown Source)
at lejos.hardware.motor.EV3LargeRegulatedMotor.<init>(Unknown Source)
at lejos.remote.ev3.RMIRemoteRegulatedMotor.<init>(Unknown Source)
at lejos.remote.ev3.RMIRemoteEV3.createRegulatedMotor(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.createRegulatedMotor(Unknown Source)
at lejos.remote.ev3.RemoteEV3.createRegulatedMotor(Unknown Source)
at test_bluetooth.Bluetooth.main(Bluetooth.java:186)

I am not sure that color sensor is really closed because when I run the program for the second time it yields the exception that it's unable to open the port of that color sensor. However, after I restart the brick, it works fine for the first time the program is run, then I need to restart again to use that color sensor port. Can anyone tell me how to properly close the color sensor?

8
  • I using Java and moreover, I notice that when I connect my EV3 by Bluetooth with my Laptop the motor is not rotating smoothly like when I write the program in compile it directly with Brick itself. Can you explain it to me? Nov 3, 2014 at 12:00
  • Also - where are you seeing the error coming from - is it when you try and instantiate the EV3ColorSensor or when you instantiate the RemoteEV3? Both of these appear to be able to throw the error you're seeing. One other thing: according to this thread it might be more efficient to use the createSampleProvider method when dealing with remote bricks and sensors. Nov 3, 2014 at 12:14
  • There is no error in the first run but when I run it the second time it throws the error that it is unable to open the port. I think because I cannot close the color sensor. Nov 3, 2014 at 12:17
  • Yes, I got that part - I'm really asking if there's any more information on the error about where in your codebase the exception is being thrown - there are two possible lines that can throw that - in your pasted code these are lines 1 and 4 - I was hoping there would be stack trace (especially if this is happening on your Laptop rather than on the Brick). Nov 3, 2014 at 12:19
  • 1
    I got the problem, I think I don't close the motor sensor that lead to above problem. Thank Zhaph For pointing out the problem. Nov 4, 2014 at 3:31

1 Answer 1

2

Looking at the stack trace in the exception you've posted, the error seems to be coming from a call to RemoteEV3.createRegulatedMotor:

at lejos.remote.ev3.RemoteEV3.createRegulatedMotor(Unknown Source)
at test_bluetooth.Bluetooth.main(Bluetooth.java:186)

After that, it's just going through the motions to try and open a port on a remote device...

From looking at the LeJOS source, that method isn't called by the main RemoteEV3 constructor when setting up the ports, so it is probably something you're calling in your main block.

Both Sensors and Motors need to be closed.

As a point of note, it's recommended that instead of connecting to the remote sensors directly as you're doing there, you instead use the createSampleProvider method:

RemoteEV3 ev3 = new RemoteEV3("10.0.1.1");
RMISampleProvider color_sensor = ev3.createSampleProvider("S2",
               "lejos.hardware.sensor.EV3ColorSensor", "Color ID");
float[] color_id= new float[1];
color_sensor.fetchSample(color_id,0);

It is apparently more efficient because instead of running the sensor driver on the PC and sending requests to the remote EV3 you're creating the sensor driver on the remote EV3 and sending the samples to the PC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.