Java library for serial communication between Java applications and Arduino boards with data visualization and export capabilities
The ArduinoException class provides comprehensive error management for Arduino communication with multilingual support. It handles specific exceptions that may occur during serial communication and provides detailed error messages in both English and Spanish.
public class ArduinoException extends Exception {
public ArduinoException(String portName, String methodName, String exceptionType);
}Creates a new Arduino-specific exception with detailed context information.
Parameters:
portName - Name of the COM port where the exception occurredmethodName - Method that triggered the exceptionexceptionType - Type of exception (use predefined constants)public static void setLanguage(boolean englishLanguage);Sets the language for error messages.
Parameters:
englishLanguage - true for English, false for Spanish (default)Usage Example:
// Set language to English
ArduinoException.setLanguage(true);
// Set language to Spanish
ArduinoException.setLanguage(false);public static String getExceptionMessage(String exceptionType);Retrieves the localized error message for a specific exception type.
Parameters:
exceptionType - Exception type constantReturns: Localized error message string
public static String getExceptionType();
public static void setExceptionType(String exceptionType);
public static String getMethodName();
public static void setMethodName(String methodName);
public static String getPortName();
public static void setPortName(String portName);Methods for accessing and setting exception context information.
public static final String TYPE_PORT_ALREADY_OPENED = "The port you are trying to open is being used by another device";
public static final String TYPE_PORT_NOT_OPENED = "Port not opened";
public static final String TYPE_NO_ARDUINO_AT_PORT = "No Arduino found connected to this port. Please check the port to which Arduino is connected";
public static final String TYPE_NO_SERIAL_PORT = "No Arduino found connected to this computer. Please connect Arduino to the PC via USB";
public static final String TYPE_PORT_USED_BY_OTHER_APP = "Unable to connect. This port is already in use by another application";public static final String TYPE_RXTX_EXCEPTION = "Cannot start the connection with Arduino twice";
public static final String TYPE_NO_ARDUINO_CONNECTION = "No connection has been established with Arduino. Please use one of the methods arduinoRX(), arduinoTX(), or arduinoRXTX()";
public static final String TYPE_KILL_ARDUINO_CONNECTION = "Cannot terminate the connection with Arduino if it was not started";
public static final String TYPE_CLOSE_PORT = "Error terminating the connection with Arduino";public static final String TYPE_SEND_DATA = "This method cannot be used if the connection to Arduino was started with the arduinoRX() method, which is only for receiving data";
public static final String TYPE_SEND_DATA_ERROR = "Error sending data";
public static final String TYPE_WRONG_SEND_DATA_CONNECTION = "The sendData() method cannot be used if the connection to Arduino was started with the arduinoRX() method, which is only for receiving data";public static final String TYPE_RECEIVE_DATA = "The receiveData() method cannot be used if the connection to Arduino was started with the ArduinoTX() method, which is only for sending data";
public static final String TYPE_NO_EVENT_LISTENER = "No EventListener has been added to the PanamaHitek_Arduino class";All exception types have corresponding Spanish translations with _ES suffix:
public static final String TYPE_PORT_ALREADY_OPENED_ES = "El puerto que intenta abrir está siendo utilizado por otro dispositivo";
public static final String TYPE_PORT_NOT_OPENED_ES = "Puerto no abierto";
public static final String TYPE_NO_ARDUINO_AT_PORT_ES = "No se ha encontrado ningún Arduino conectado a este puerto. Verifique el puerto en el que está conectado Arduino";
// ... and so on for all exception typesimport com.panamahitek.PanamaHitek_Arduino;
import com.panamahitek.ArduinoException;
public class ExceptionHandlingExample {
public void connectWithErrorHandling() {
// Set language preference
ArduinoException.setLanguage(true); // English
PanamaHitek_Arduino arduino = new PanamaHitek_Arduino();
try {
arduino.arduinoTX("COM3", 9600);
arduino.sendData("Hello Arduino!");
} catch (ArduinoException ex) {
System.err.println("Arduino Exception Details:");
System.err.println("Port: " + ArduinoException.getPortName());
System.err.println("Method: " + ArduinoException.getMethodName());
System.err.println("Type: " + ArduinoException.getExceptionType());
System.err.println("Message: " + ex.getMessage());
// Handle specific exception types
String exceptionType = ArduinoException.getExceptionType();
switch (exceptionType) {
case ArduinoException.TYPE_NO_SERIAL_PORT:
System.err.println("Please connect Arduino via USB");
break;
case ArduinoException.TYPE_PORT_ALREADY_OPENED:
System.err.println("Close other applications using this port");
break;
case ArduinoException.TYPE_NO_ARDUINO_AT_PORT:
System.err.println("Check Arduino connection and port");
break;
}
}
}
}public class MultilingualErrorHandling {
public void handleErrorsInSpanish() {
// Configure for Spanish error messages
ArduinoException.setLanguage(false);
try {
PanamaHitek_Arduino arduino = new PanamaHitek_Arduino();
arduino.arduinoRX("COM99", 9600, null); // Invalid port
} catch (ArduinoException ex) {
// Error messages will be in Spanish
System.err.println("Error en español: " + ex.getMessage());
}
}
public void handleErrorsInEnglish() {
// Configure for English error messages
ArduinoException.setLanguage(true);
try {
PanamaHitek_Arduino arduino = new PanamaHitek_Arduino();
arduino.sendData("data"); // No connection established
} catch (ArduinoException ex) {
// Error messages will be in English
System.err.println("English error: " + ex.getMessage());
}
}
}public class CustomExceptionHandling {
public void createCustomException() {
try {
// Simulate a custom error scenario
throw new ArduinoException("COM3", "customMethod",
ArduinoException.TYPE_PORT_USED_BY_OTHER_APP);
} catch (ArduinoException ex) {
// Get localized error message
String localizedMessage = ArduinoException.getExceptionMessage(
ArduinoException.TYPE_PORT_USED_BY_OTHER_APP);
System.err.println("Localized message: " + localizedMessage);
System.err.println("Full exception: " + ex.getMessage());
}
}
}public class ErrorRecoveryExample {
private PanamaHitek_Arduino arduino;
private String[] possiblePorts = {"COM3", "COM4", "COM5", "/dev/ttyUSB0", "/dev/ttyACM0"};
public boolean connectWithRetry() {
ArduinoException.setLanguage(true);
arduino = new PanamaHitek_Arduino();
for (String port : possiblePorts) {
try {
System.out.println("Trying port: " + port);
arduino.arduinoRXTX(port, 9600, null);
System.out.println("Successfully connected to: " + port);
return true;
} catch (ArduinoException ex) {
String exceptionType = ArduinoException.getExceptionType();
switch (exceptionType) {
case ArduinoException.TYPE_NO_ARDUINO_AT_PORT:
System.out.println("No Arduino at " + port + ", trying next port...");
break;
case ArduinoException.TYPE_PORT_ALREADY_OPENED:
System.err.println("Port " + port + " is in use, trying next port...");
break;
case ArduinoException.TYPE_NO_SERIAL_PORT:
System.err.println("No serial ports available");
return false;
default:
System.err.println("Unexpected error: " + ex.getMessage());
break;
}
}
}
System.err.println("Failed to connect to any port");
return false;
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-panamahitek--panama-hitek-arduino