Hi All,
I am writing a code to read all the available serial ports in Java. When I check the ports manually under /dev/cu it is available and even in Arduino IDE, the port is getting detected. But when use the same in the code to read serial ports I am not able to read it.
Below is the code that I am using to read the serial ports
package com.qa.tests;
import com.qa.base.BaseTest;
import com.qa.base.LoggerManager;
import com.qa.utils.JsonParser;
import gnu.io.CommPortIdentifier;
import jssc.SerialPort;
import org.json.JSONObject;
import org.testng.annotations.Test;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;
@Test
public class ArduinoControlTests extends BaseTest {
LoggerManager loggerManager = new LoggerManager();
public SerialPort connectArduino() throws Exception {
loggerManager.log().info("Connect arduino function");
Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
SerialPort serialPort = (SerialPort) portList.asIterator().next();
loggerManager.log().info(serialPort);
// for (Enumeration<?> e = CommPortIdentifier.getPortIdentifiers(); e.hasMoreElements(); )
// loggerManager.log().info(“ports”+e.nextElement());
loggerManager.log().info("list of ports " + portList);
String portName = "";
CommPortIdentifier currentPort = null;
// ports.asIterator()
loggerManager.log().info("Ports " +portList.asIterator().hasNext());
while (portList.hasMoreElements()) {
// if(ports.hasMoreElements()) {
loggerManager.log().info(“Inside while loop”);
currentPort = CommPortIdentifier.getPortIdentifier((String) portList.nextElement());
loggerManager.log().info("Current port " + currentPort);
if (currentPort.getPortType() == CommPortIdentifier.PORT_SERIAL) {
portName = currentPort.getName();
loggerManager.log().info(“Port name” + portName);
// if(portName.contains(“Arduino”)){
// break;
// }
}
}
currentPort = (CommPortIdentifier) portList;
loggerManager.log().info("Current port outside loop " + currentPort);
if (currentPort == null)
return null;
// try {
// String cmd = "sudo chmod 666 " + portName;
// Runtime.getRuntime().exec(cmd);
// SerialPort arduinoSerial = portName.open(“Arduino”, 2000);
//// SerialPort arduinoSerial = (SerialPort) currentPort;
// arduinoSerial.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// TimeUnit.SECONDS.sleep(2);
// return arduinoSerial;
// } catch (IOException | InterruptedException e) {
// throw new Exception(“Arduino board not connected”);
// }
return null;
}
@Test
public void ArduinoTest() {
try {
JSONObject Object = new JSONObject(JsonParser.parse("ArduinoConnection.json").toString());
// JSONObject Object = new JSONObject(JsonParser.parse(“devices.json”).getJSONObject(“keyControl”).toString());
loggerManager.log().info("Object " + Object);
String arduinoKeyOn = Object.getString(“ARDUINO_KEY_ON”);
loggerManager.log().info("arduino key on " + arduinoKeyOn);
String arduinoKeyOff = Object.getString(“ARDUINO_KEY_OFF”);
loggerManager.log().info("arduino key off " + arduinoKeyOff);
SerialPort sport = connectArduino();
loggerManager.log().info("Sport " + sport);
// if ( sport == null) return;
// OutputStream output = sport.getOutputStream();
// loggerManager.log().info("Arduino output " + output);
// output.write(arduinoKeyOff.getBytes());
// TimeUnit.SECONDS.sleep(1);
// output.write(arduinoKeyOn.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
wrong forum. correct -> https://forum.arduino.cc/