Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.

LegoPort

Anthony Law edited this page May 30, 2016 · 7 revisions

Before you start with any of the devices (e.g. Motor, Sensor), you have to create yourself a LegoPort instance, pointing to your connected port.

The connected port can be specified as a parameter using these Java Fields:

  • Sensor Port 1 - LegoPort.INPUT_1
  • Sensor Port 2 - LegoPort.INPUT_2
  • Sensor Port 3 - LegoPort.INPUT_3
  • Sensor Port 4 - LegoPort.INPUT_4
  • Motor Port A - LegoPort.OUTPUT_A
  • Motor Port B - LegoPort.OUTPUT_B
  • Motor Port C - LegoPort.OUTPUT_C
  • Motor Port D - LegoPort.OUTPUT_D

To do this, we can use the following code:

import org.ev3dev.hardware.LegoPort

public static void main(String[] args){
    try {
         LegoPort port = new LegoPort(LegoPort.OUTPUT_A);
    catch (InvalidPortException e){
         e.printStackTrace();
    }
}

Yeah! We just created a LegoPort pointing to OUTPUT_A.

Other then, we can get or set different properties from this port!

import org.ev3dev.hardware.LegoPort;
import java.util.Arrays;

public static void main(String[] args){
    try {
         LegoPort port = new LegoPort(LegoPort.OUTPUT_A);
         System.out.println("Address: " + port.getAddress());
         System.out.println("Driver: " + port.getDriverName());
         System.out.println("Modes: " + Arrays.deepToString(port.getModes()));
         System.out.println("Current Mode: " + port.getMode());
         System.out.println("Status: " + port.getStatus());
         
         //Set Mode (Depends on the function, port.getModes())
         //port.setMode("");
         
         //Set device, writing the name of a driver
         //port.setDevice("");
    catch (InvalidPortException e){
         e.printStackTrace();
    }
}

##FAQ

Q: Why a InvalidPortException is thrown during the creation of the instance?

You have to specific a port that is a Integer Field or a number between 0 and 7.

Q: Why the application throws IOException wherever I get the addresses or other properties?

There are no connected device or port. So the API couldn't read the properties of the instance.

Clone this wiki locally