Closed HannesBulk closed 5 years ago
Hi there! Here is a simple test program that does what you requested:
import java.nio.charset.StandardCharsets;
import com.fazecast.jSerialComm.SerialPort;
public class SerialPortTest {
static public void main(String[] args) {
byte[] readBuffer = new byte[2048];
byte[] outputMessage = "Test Message to Transmit".getBytes(StandardCharsets.UTF_8);
System.out.println("\nUsing jSerialComm Library Version v" + SerialPort.getVersion());
SerialPort[] ports = SerialPort.getCommPorts();
System.out.println("\nAvailable Ports:\n");
for (int i = 0; i < ports.length; ++i)
System.out.println(" [" + (i+1) + "] " + ports[i].getSystemPortName() + ": " + ports[i].getDescriptivePortName() + " - " + ports[i].getPortDescription());
if (ports.length == 0) {
System.out.println(" No Ports Detected!");
return;
}
SerialPort serialPort = ports[ports.length - 1];
boolean openedSuccessfully = serialPort.openPort();
System.out.println("\nOpening " + serialPort.getSystemPortName() + ": " + serialPort.getDescriptivePortName() + " - " + serialPort.getPortDescription() + ": " + openedSuccessfully);
if (!openedSuccessfully)
return;
System.out.println("Setting read timeout mode to blocking with no timeout");
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
System.out.println("\nWriting message to serial port: \"" + new String(outputMessage, StandardCharsets.UTF_8) + "\"");
serialPort.writeBytes(outputMessage, outputMessage.length);
System.out.println("Writing complete!\nReading message from serial port ");
System.out.flush();
int numBytesRead = serialPort.readBytes(readBuffer, readBuffer.length);
System.out.println("(" + numBytesRead + " bytes read): \"" + new String(readBuffer, StandardCharsets.UTF_8) + "\"");
System.out.println("Reading complete!\n");
System.out.println("Closing " + serialPort.getDescriptivePortName() + ": " + serialPort.closePort());
}
}
Getting your project configuration set up to run correctly using Netbeans is outside the scope of this project, since it is not a jSerialComm-specific question. You may be able to find that information by doing a search for setting up a Netbeans project that utilizes third-party JARs containing native libraries. Thanks!
@HannesBulk based on the error messages you reported, the problem might not be jSerialComm but rather that NetBeans is configured to find the jSerialComm JAR at compile time but not at run time. You might consider trying hedgecrw's sample application from the command line instead of NetBeans to determine if NetBeans config is the problem.
If jSerialComm.jar and SerialPortTest.java are in the same directory:
javac SerialPortTest.java
java SerialPortTest --cp jSerialComm.jar
it's now working, thanks.
I want to implement JserialComm in a simple java program, but cannot get it working. Configuration: Windows 10, Java8 JDK, Netbeans 8.2. Is there is simple manual step-by-step manual with all steps needed get a simple JAVA SE program working. The program can run is a Windows CMD box, and all it must do is Display the available COM ports, open the last available COM port, send a message over the serial interface, end recieve one line of text back from the serial interface, display this in the CMD Box, and then exit the program. Once this basic program is working, I can rewrite an existing C# program, using the serial port. At this moment I cannot get the Serial port working under Java, get messages as "jSerialComm not found" or "Runtime Error". I Prefer to use a local copy of JSerialComm Software (Jar or Fazecast Directory with files ???) So i need a step by step dummy-proof manual how to implement this under Windows-10.