Get I/0 variables

Hello everyone. I need to develop a URCap in order to make test with some I/0. The aim is to create a Installation node with a selector of the I/0. The user can select one I/0 and set/reset for testing. The idea is to facilitate the task when it comes to testing the correct functioning of the outputs.

How can I create that? How can I manage selected I/0 and set it to a value with a button?

Thanks for your time

Take a look at this sample, it reads the collection of I/O’s and allows the user to toggle them by button push.
It sounds like what you are looking for.

Thank you for your suggestion.

Now I have more information about this type of URCap. But I have another question…
If I want to search a I/0 with a string that has been written by the user in a InputTextField, how can I implement that. In the URCap appears a loop that search the I/0 with a default name, but if I change some arguments, this script doesn’t work…

I only change the argument String to InputTextField…

private DigitalIO getDigitalIO(InputTextField defaultName){
Collection<DigitalIO> IOcollection = api.getIOs().getIOs(DigitalIO.class);
int IO_count = IOcollection.size();
if(IO_count > 0){
	Iterator<DigitalIO> IO_itr = IOcollection.iterator();
	while(IO_itr.hasNext()){
		DigitalIO thisIO = IO_itr.next();
		String thisDefaultName = thisIO.getDefaultName();
		System.out.println("Found an IO named "+thisDefaultName);
		if(thisDefaultName.equals(defaultName)){
			return thisIO;
		}
	}
}
return null;
}

When you have got the iterated IO, instead of calling String thisDefaultName = thisIO.getDefaultName(); you should get the user assigned name instead, hence String thisUserdefinedName = thisIO.getName(); and the verify if this name is the correct one or follows the correct pattern. If no name is assigned by the user, the default name will be returned.

I try it with this function, but still doesn’t work. I don’t know where is the problem, maybe in the InstallationNode you cannot access to URCap Api? In program node, the iterator for search the IO works, but in the InstallationNode no. Why, is this the reason?

The Constructor of the program node is first called when the node is inserted or a program is loaded.
The installation node constructor is called on boot, before the domain.io API is ready.
Maybe try calling this in eg. openView() instead.

1 Like

Now I have implemented the iterator like:
private DigitalIO getDigitalIO(InputTextField defaultName){
Collection IOcollection = api.getIOs().getIOs(DigitalIO.class);
int IO_count = IOcollection.size();
if(IO_count > 0){
Iterator IO_itr = IOcollection.iterator();
while(IO_itr.hasNext()){
DigitalIO thisIO = IO_itr.next();
String thisDefaultName = thisIO.getName();
System.out.println("Found an IO named "+thisDefaultName);
if(thisDefaultName.equals(defaultName)){
return thisIO;
}
}
}
return null;
}

And you mentioned before that I should call the constructor inside OpenView() but the program returs an error…

public void openView() {
	
	public EntradaSalidaIOInstallationNodeContribution(URCapAPI api, DataModel model) {
		this.api = api;
		this.model = model;
	
		// Se seleccionan las salidas digitales que se van a gestionar
		
		VariableIO = getDigitalIO(Nombrevariable);

			}
	Nombrevariable.setText(getNombrevariable());
	
	BTN_TEST1.setText("Variable a 1");
	uiTimer = new Timer(true);
	uiTimer.schedule(new TimerTask() {
		@Override
		public void run() {
			EventQueue.invokeLater(new Runnable() {
				@Override
				public void run() {
					updateLabels();
				}
			});
		}

	}, 0, 500);
	try{
		   liveImage2.setImage(ImageIO.read(getClass().getResource("/com/ur/urcap/sample/EntradaSalidaIO/impl/logo.png")));
		} catch (java.io.IOException e) {
		   e.printStackTrace();
		}
}

I think that we cannot do a Override in a variable defined final…

Thank you for your time, I apreciate that

You cannot call the Constructor inside openView.
Call the methods, that find the IO in openView instead of inside the Constructor.

private VariableIO variableIO;

public void openView(){
     this.variableIO = getDigitalIO(Nombrevariable);
}

Hi there! I’ve tried to put this function in OpenView, but there’s some other errors that I don’t understand.

I want to get the name of the IO using a InputTextField in the Installation HTML. With this name, I search in the library of the IO and grab the selected IO demanded by the InputTextField.

However, when I try to set the InputTextField to a String for whatever reason, I can’t. I copied part of the HelloWorldExample in order to get the idea of the InputTextField.

private static final String DEFAULT_VALUE = "tool_out[0]";

public String getNombrevariable() {
	if (!model.isSet(VARIABLE1_KEY)) {
		resetToDefaultValue();
	}
	return model.get(VARIABLE1_KEY, DEFAULT_VALUE);
}
// Esto es para el Popup del mensaje. En principio no es necesario para esta aplicación

private void setNombrevariable(String message) {
	if ("".equals(message)) {
		resetToDefaultValue();
	} else {
		model.set(VARIABLE1_KEY, message);
	}
}
// Pone el nombre por defecto a la variable. También lo hace para el modelo.
private void resetToDefaultValue() {
	Nombrevariable.setText(DEFAULT_VALUE);
	model.set(VARIABLE1_KEY, DEFAULT_VALUE);
}

In OpenView there’s this code:
@Override
public void openView() {

	Nombrevariable.setText(getNombrevariable());
	//Nombrevariable.setText("tool_out[0]");
	//this.VariableIO = getDigitalIO(Nombrevariable);
	this.VariableIO = getDigitalIO(getNombrevariable());
	BTN_TEST1.setText("Variable a 1");
	uiTimer = new Timer(true);
	uiTimer.schedule(new TimerTask() {
		@Override
		public void run() {
			EventQueue.invokeLater(new Runnable() {
				@Override
				public void run() {
					updateLabels();
				}
			});
		}
	}, 0, 500);
	try{
		logoimg.setImage(ImageIO.read(getClass().getResource("logo.png")));
		//logoimg.setImage(ImageIO.read(getClass().getResource("/com/ur/urcap/sample/EntradaSalidaIO/impl/logo.png")));
		} catch (java.io.IOException e) {
		   e.printStackTrace();
		}
}

Thanks for your help. I’m just a begginer with Java and URCaps, I need a lot of help. Sorry for wasting your time

Dear community,

I’m a bit new to the Swing API, so I’m having a difficulty, probably an easy thing, but I ask.

As a first approach I was getting the Digital IOs from a ProgramNodeContribution, and everything was ok:

private void initDIn() {

	ArrayList<Object> ioItems = new ArrayList<Object>();
	ioItems.addAll(apiProvider.getProgramAPI().getIOModel().getIOs(DigitalIO.class));

	for (Iterator<Object> itemIterator = ioItems.iterator(); itemIterator.hasNext();) {
		DigitalIO ioItem = (DigitalIO)itemIterator.next();
		if (ioItem.isInput() && (ioItem.getDefaultName().equals(DIN_WAYPOINT_RECORDING_STR))) {
			dInWaypointRecording = ioItem;
		}
	}
	
}

But now, I’m refactoring many things, so I need this to be in the InstallationNodeContribution code. I just get a null pointer from getIOs().

private void initDIn() {

	ArrayList<Object> ioItems = new ArrayList<Object>();
	ioItems.addAll(apiProvider.getInstallationAPI().getIOModel().getIOs(DigitalIO.class));

	for (Iterator<Object> itemIterator = ioItems.iterator(); itemIterator.hasNext();) {
		DigitalIO ioItem = (DigitalIO)itemIterator.next();
		if (ioItem.isInput() && (ioItem.getDefaultName().equals(DIN_WAYPOINT_RECORDING_STR))) {
			dInWaypointRecording = ioItem;
		}
	}
	
}

I’d love to know if I’m missing something super easy, or if this is a bug. So that I can fix my code, or just use a workaround.
I wasn’t able to find this use in any sample, is there any?
Any thoughts? Has anyone use successfully getIOs from Install node? Thanks!!!

Note: I’m using API 1.7.0 and URSim 3.10.0.76181

Your code seems right. Just tested it out inside an InstallationNode URCap and ran. Try relooking over your initialization of your apiProvider inside the NodeContribution.

Thank you @roman.reiner! Good to know it works for you. I’ll double check my initialization.