Answers.Body: <p>Looking at the stack trace in the exception you've posted, the error seems to be coming from a call to <code>RemoteEV3.createRegulatedMotor</code>:</p>
  
  <pre><code>at lejos.remote.ev3.RemoteEV3.createRegulatedMotor(Unknown Source)
  at test_bluetooth.Bluetooth.main(Bluetooth.java:186)
  </code></pre>
  
  <p>After that, it's just going through the motions to try and open a port on a remote device...</p>
  
  <p>From looking at the LeJOS source, that method isn't called by the main <code>RemoteEV3</code> constructor when setting up the ports, so it is probably something you're calling in your main block.</p>
  
  <p>Both Sensors and Motors need to be closed.</p>
  
  <p>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 <code>createSampleProvider</code> method:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>It is <a href="http://www.lejos.org/forum/viewtopic.php?f=18&amp;t=6896#p25059" rel="nofollow noreferrer">apparently more efficient</a> 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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-11-04T09:25:25.387Z
Answers.LastActivityDate: 2020-03-18T11:01:20.990Z
Answers.LastEditDate: 2020-03-18T11:01:20.990Z
Answers.UserDisplayName: Zhaph - Ben Duguid
Body: <p>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 <code>close()</code> on the color sensor. The following code is what I did with EV3 Bluetooth connection.</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>And it throws the following exceptions:</p>
  
  <pre><code>Exception in thread "main" lejos.hardware.DeviceException: unable to open port
  at lejos.internal.ev3.EV3Port.open(Unknown Source)
  at lejos.hardware.motor.BaseRegulatedMotor.&lt;init&gt;(Unknown Source)
  at lejos.hardware.motor.EV3LargeRegulatedMotor.&lt;init&gt;(Unknown Source)
  at lejos.remote.ev3.RMIRemoteRegulatedMotor.&lt;init&gt;(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)
  </code></pre>
  
  <p>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?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-03T12:00:38.913Z
Comments.Text: 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?
Comments.UserDisplayName: Phav Makara
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-03T12:14:34.780Z
Comments.Text: 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](http://www.lejos.org/forum/viewtopic.php?f=18&t=6896#p24914) it might be more efficient to use the `createSampleProvider` method when dealing with remote bricks and sensors.
Comments.UserDisplayName: Zhaph - Ben Duguid
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-03T12:17:00.587Z
Comments.Text: 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.
Comments.UserDisplayName: Phav Makara
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-03T12:19:14.810Z
Comments.Text: 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).
Comments.UserDisplayName: Zhaph - Ben Duguid
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-03T13:02:45.050Z
Comments.Text: I post that exceptions but can you help me with one more problem? I use RMIRegulatedMotor but it is not smoothly rotating the motor. Can I use RegulatedMotor with remote control?
Comments.UserDisplayName: Phav Makara
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-03T14:31:49.277Z
Comments.Text: Hmm, looking at your stack trace, it seems to be throwing at `RemoteEV3.createRegulatedMotor` rather than on your EV3ColorSensor - this doesn't appear to be called by the RemoteEV3 constructor, but something in your `main` block?
Comments.UserDisplayName: Zhaph - Ben Duguid
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-04T02:16:55.587Z
Comments.Text: You mean I don't close the motor sensor properly?
Comments.UserDisplayName: Phav Makara
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-11-04T03:31:32.513Z
Comments.Text: 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.
Comments.UserDisplayName: Phav Makara
Tags: <ev3><bluetooth><remote-control><sensor-colour><lejos>
Title: How to close color Sensor properly?
UserDisplayName: Phav Makara
ThreadText: # How to close color Sensor properly?
  
  (Asked by: username_0 on 2014-11-01)
  
  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.(Unknown Source)
  at lejos.hardware.motor.EV3LargeRegulatedMotor.(Unknown Source)
  at lejos.remote.ev3.RMIRemoteRegulatedMotor.(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?
  
  **Tags:** ev3, bluetooth, remote-control, sensor-colour, lejos
  > **Comments:**
  > **username_0**: 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?  
  > **username_1**: 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](http://www.lejos.org/forum/viewtopic.php?f=18&t=6896#p24914) it might be more efficient to use the `createSampleProvider` method when dealing with remote bricks and sensors.  
  > **username_0**: 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.  
  > **username_1**: 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).  
  > **username_0**: I post that exceptions but can you help me with one more problem? I use RMIRegulatedMotor but it is not smoothly rotating the motor. Can I use RegulatedMotor with remote control?  
  > **username_1**: Hmm, looking at your stack trace, it seems to be throwing at `RemoteEV3.createRegulatedMotor` rather than on your EV3ColorSensor - this doesn't appear to be called by the RemoteEV3 constructor, but something in your `main` block?  
  > **username_0**: You mean I don't close the motor sensor properly?  
  > **username_0**: 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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-11-04. Score: 2 | Accepted answer
  
  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.
  
  
  ---
  
  