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?
EV3ColorSensoror when you instantiate theRemoteEV3? 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 thecreateSampleProvidermethod when dealing with remote bricks and sensors. – Zhaph - Ben Duguid Nov 03 '14 at 12:14RemoteEV3.createRegulatedMotorrather than on your EV3ColorSensor - this doesn't appear to be called by the RemoteEV3 constructor, but something in yourmainblock? – Zhaph - Ben Duguid Nov 03 '14 at 14:31