Exception in "main" ...DeviceException: unable to open port

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

Exception in "main" ...DeviceException: unable to open port

Postby leJosNeedHelp » Tue Jan 28, 2014 12:05 pm

Hi Guys,

my touch sensor works now! But wenn i try to do this:

Code: Select all
for(int i=0;i<100;){
         Fahren.vfahren(); //starts Motor B&C
         if(Sensor.pressed()){ // returns true if sensor is pressed
            i++;
            Fahren.stop(); // stops Motor B&C
            Fahren.rfahrendauer(500); // drive backwards for 0,5 sec
            Fahren.drehengegenUhrzeiger(-1000); // rotate ca 90 degrees
         }      
      
      }



i got the following errors:
Exception in thread "main" lejos.hardware.DeviceException: unable to open port
at lejos.internal.ev3.EV3Port.open<EV3Port.java:70>
at lejos.hardware.sensor.AnalogSensor.<init><AnalogSensor.java:25
at lejos.hardware.sensor.AnalogSensor.<init><AnalogSensor.java:36
at lejos.hardware.sensor.EV3ToiuchSensor.<init><EV3TouchSensor.java:24
at com.roboter.dev.Sensor.pressed<Sensor.java:12>
at com.roboter.dev.EV3FirstProgram.main<EV3FirstProgram.java:42>

I have no idea what that mean...

My code for the touch sensor is the following, maybe there is the reason:
Code: Select all
package com.roboter.dev;
import lejos.hardware.sensor.*;
import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.Motor;
import lejos.hardware.port.*;
import lejos.robotics.SampleProvider;

public class Sensor {
   
   
   public static boolean pressed(){
   SampleProvider touch = new EV3TouchSensor(SensorPort.S1);
   float[] sample = new float[touch.sampleSize()];
   while(true) {
     touch.fetchSample(sample, 0);
     if (sample[0] == 1) {
       return true;
     }
   }
   }

}


Maybe somebody have an idea?
leJosNeedHelp
New User
 
Posts: 18
Joined: Tue Sep 24, 2013 7:33 am

Re: Exception in "main" ...DeviceException: unable to open p

Postby cfin » Tue Jan 28, 2014 12:29 pm

maybe it sound stupid.
But I normally get this exception, when I plugged the sensor into the wrong port. So please check that, of not already done.
cfin
New User
 
Posts: 15
Joined: Fri Oct 25, 2013 8:04 pm

Re: Exception in "main" ...DeviceException: unable to open p

Postby leJosNeedHelp » Tue Jan 28, 2014 12:53 pm

Hi!
Thanks first, for your answere.
I forgott to write that the Sensor works, but just 1 Time! Until the Touchsensor hits a wall he drives throught the room. After hitting a wall, the motor stops and he is rotating ca 90 degrees. Now he should carry on, but the error appears ...
leJosNeedHelp
New User
 
Posts: 18
Joined: Tue Sep 24, 2013 7:33 am

Re: Exception in "main" ...DeviceException: unable to open p

Postby lawrie » Tue Jan 28, 2014 2:52 pm

The EV3 version of leJOS only lets you open a sensor port once, unless you use the close() method of sensor drivers to close it.

Each time you do "new EV3TouchSensor(...)" you are opening the port.

You should only create the EV3TouchSensor class once. If you want to use a sensor port for a difference sensor, you should close it with the close() method of EV3TouchSensor.
lawrie
leJOS Team Member
 
Posts: 922
Joined: Mon Feb 05, 2007 1:27 pm

Re: Exception in "main" ...DeviceException: unable to open p

Postby leJosNeedHelp » Wed Jan 29, 2014 7:43 am

Hi !
Thank you first :-)
I tried to use the "close" Methode, but there isn't a Methode like this.
I got an error "The method close() is undefined for the type SampleProvider". So touch.close(); didn't work. I can't find a close methode in EV3TouchSensor or in SampeProvider.
leJosNeedHelp
New User
 
Posts: 18
Joined: Tue Sep 24, 2013 7:33 am

Re: Exception in "main" ...DeviceException: unable to open p

Postby gloomyandy » Wed Jan 29, 2014 8:49 am

The close method is on the port not the sample provider. To be honest you will just be better off avoiding this situation. Create the sample provider outside of the method that reads it, don't keep on opening and closing the port and creating the sample provider every time you want to read the sensor. Creating a sensor devices has considerable overhead and is not something you should be doing over and over again like this.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4042
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Exception in "main" ...DeviceException: unable to open p

Postby leJosNeedHelp » Wed Jan 29, 2014 9:37 am

Thank you! Problem fixed now, i taked the Sample Provider out of the methode just like you said and now it works fine :-)
leJosNeedHelp
New User
 
Posts: 18
Joined: Tue Sep 24, 2013 7:33 am


Return to EV3 Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff