-
Notifications
You must be signed in to change notification settings - Fork 9
Program Structure
Java is a completely object-oriented language. So-much-so that absolutely everything in Java must be written inside a class. As a result, the code that runs when the program is started is contained in a method inside a class. Normally, both this method and class are called "Main" like so:
public class Main {
public static void main(String[] args) {
// Code goes here
}
}
When your code is executed, Java will look for the static method main()
inside the class Main
and run that.
In general, you could write your entire program within this method. However, this might not be best practice. In the case of writing a control program using JISA
, you might notice that the template code looks like this:
public class Main {
public static void run() throws Exception {
// Code goes here
}
public static void main(String[] args) {
try {
run();
} catch (Exception e) {
Util.exceptionHandler(e);
}
}
}
The reason being that the JISA library can "throw exceptions" when something goes wrong with the hardware (ie a device is disconnected part-way through a program or the hardware breaks down). This is why our run()
method says throws Exception
, because it is possible that it might throw an exception. If an exception is not caught somewhere it will crash the program. This is why in main()
our call to run()
has been surrounded with a try { } catch() { }
structure. It's basically saying "try to run the code in the first { ... }
and if something goes wrong do what it says in the second { ... }
instead". Util.exceptionHandler()
is a built-in function to JISA that automatically figures out what to do with certain types of exceptions. However, you are completely free to write your own code to deal with them instead if you so wish.
Much like with any other language, if you wish to use parts of a library you will need to import it at the top of your source code. If you are using an IDE like IntelliJ IDEA, these will be added automatically as you use them. However, if you wish, you can simply copy-and-paste the following to import everything just in-case you need it:
import JISA.*;
import JISA.Addresses.*;
import JISA.Control.*;
import JISA.Devices.*;
import JISA.Experiment.*;
import JISA.GUI.*;
import JISA.VISA.*;
This is already included for you if you use the project template.
Sometimes you will have a variable or object that you need to be able to access across multiple different functions. To do this, just declare it in the class instead of your method like so:
public class Main {
// Declaring them up here
public static SR830 lockIn;
public static K2450 smu;
public static void run() throws Exception {
connectToDevices();
getValues();
}
public static void connectToDevices() throws Exception {
// Means that you can access them in any method within the class
lockIn = new SR830(new GPIBAddress(0, 30));
smu = new K2450(new GPIBAddress(0, 15));
}
public static void getValues() throws Exception {
// Including here
double frequency = lockIn.getFrequency();
double voltage = smu.getVoltage();
}
// How wonderful
public static void main(String[] args) {
try {
run();
} catch (Exception e) {
Util.exceptionHandler(e);
}
}
}
It is best practise to define instruments, GUI elements and ResultList
objects as static class variables like this, so that they are accessible from all parts of your program.
- Getting Started
- Object Orientation
- Choosing a Language
- Using JISA in Java
- Using JISA in Python
- Using JISA in Kotlin
- Exceptions
- Functions as Objects
- Instrument Basics
- SMUs
- Thermometers (and old TCs)
- PID and Temperature Controllers
- Lock-Ins
- Power Supplies
- Pre-Amplifiers
- Writing New Drivers