Following other Java topics and, as a completely new Java user, I was lost doing even the most simple thing, such as how to make a functioning project from a totally new installation of Netbeans IDE and Java JDK.
For anyone else equally ill informed, this is a step by step tutorial written as I was working out how to make the project work.
this is using MacOS, Netbeans 12.4 with jdk-16.0.2 and jSerialComm-2.7.0.jar to create a working test project that runs from terminal and sends/receives a message using the serial port, there should be no reason for the same procedure to be fully operational on linux/win too, I hope it might be useful to someone:-
How to use âjSerialComm-2.7.0.jarâ library with Netbeans 12.4 to make a functioning test project that can be run in Terminal on a Mac (and I expect without much change on any other platform).
the âjSerialComm-2.7.0.jarâ file is downloaded here:-
whichever download type you use it must be opened somewhere you will be able to reference in the IDE later on.
the Java Library used was âjdk-16.0.2.jdkâ from this link:-
this setup information adds the Netbeans part NOT shown in this link:-
https://www.gitmemory.com/issue/Fazecast/jSerialComm/245/539100055
these are the steps taken to get the project working, there should be no difference on ALL platforms, the IDE was a fresh install and had no changes to any of the default installed settings of any type.
it is possible to use other options but this was the tested method for this project.
1 open Netbeans, 12.4 (current at the time of writing)
2 go to FILE>NEW PROJECT, this opens a window called âNew Projectâ
3 in âCategories:â click âJava with Antâ, do not open the disclosure triangle
4 in âProjects:â click âJava Applicationâ, click âNextâ button
5 the âName and Locationâ window opens, set the PROJECT NAME> jSerialTest, the LOCATION and FOLDER will be filled with default locations, unless necessary, do not change these
6 ignore âUse Dedicated Folder for Storing Librariesâ it must be unticked
7 tick âCreate Main Classâ, this is filled automatically with âjserialtest.JSerialTestâ, click âFinishâ button
8 the IDE creates all the project files needed for the framework of the test application and opens up the âjSerialTest.javaâ tab in the main IDE window
9 click the âProjectsâ âLibrariesâ disclosure triangle, it should show one entry âJDK 16 (Default)â, if this is not the case then you may not have the JDK installed (which is beyond this scope to fix)
10 click the âLibrariesâ folder to highlight it, then open menu with right click, select âAdd JAR/Folderâ, this opens âAdd JAR/Folderâ window, now search for the location of the uncompressed file âjSerialComm-2.7.0.jarâ, click the file when located, the information for the file is added to the text fields, accept the default settings and click âOpenâ, the file is added to the âProjectsâ âLibrariesâ folder in the IDE
11 next look at the âjSerialTest.javaâ tab in the main IDE window, make space immediately ABOVE the code line âpublic class JSerialTestâ, copy these two lines into that space:-
import java.nio.charset.StandardCharsets;
import com.fazecast.jSerialComm.SerialPort;
12 ignore any error flags at the moment, look at the line â// TODO code application logic hereâ, highlight this line with the thought that this line will be completely replaced by pasting the following code in its place:-
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());
13 that is all the code required to test the serial port, it may be formatted in an odd way as it is just text, but the code should work fine.
next we will build it and then test it, you may notice the IDE has added two folders âTest Packagesâ and âTest Librariesâ these can be ignored
14 now go to MENU>RUN>CLEAN AND BUILD PROJECT, or press the hammer+sweeping brush icon
15 the bottom of the IDE opens a tab called âOutputâ, after a short while it will print some lines of text, ending with âBUILD SUCCESSFUL (total time xx seconds)â, if not then you have done something wrong, restart and follow the instructions exactly
16 in the âOutputâ tab there is a line âTo run this application from the command line without Ant, try:â followed immediately below is the text:-
java -jar â/Users/doj/NetBeansProjects/jSerialTest/dist/jSerialTest.jarâ
17 this is the command that is used in âTerminalâ to run the application, open âTerminalâ on MacOS and paste the line from step 16 into it, then press ENTER
18 after a short pause while the ports are scanned you should get something like this:-
Using jSerialComm Library Version v2.7.0
Available Ports:
[1] cu.usbserial-FTB9GCLI: TTL232R-3V3 - TTL232R-3V3
[2] tty.usbserial-FTB9GCLI: TTL232R-3V3 (Dial-In) - TTL232R-3V3 (Dial-In)
[3] cu.Bluetooth-Incoming-Port: Bluetooth-Incoming-Port - Bluetooth-Incoming-Port
[4] tty.Bluetooth-Incoming-Port: Bluetooth-Incoming-Port (Dial-In) - Bluetooth-Incoming-Port (Dial-In)
Opening tty.Bluetooth-Incoming-Port: Bluetooth-Incoming-Port (Dial-In) - Bluetooth-Incoming-Port (Dial-In): true
Setting read timeout mode to blocking with no timeout
Writing message to serial port: âTest Message to Transmitâ
Writing complete!
Reading message from serial port
on a Mac you will always have at least the Bluetooth port available, but if you are on another platform without any serial ports it will tell you none are available with a message âNo Ports Detected!â.
so what my output tells us is that there are 2 actual ports on the system, for some reason MacOS has separate RX and TX references where on windows you might see COM1, COM3 etc.
the app is set in code âSerialPort serialPort = ports[ports.length - 1];â to connect to the bluetooth serial port and the message tells us it has sent a message âTest Message to Transmitâ to its TX buffer and is now sat, endlessly, waiting for a reply.
To test this I exited âTerminalâ to stop the app running and set up my MacBook Pro laptop with a terminal program (CoolTerm from this great place http://www.freeware.the-meiers.org).
on both laptop and iMac I opened âSystem Preferencesâ and then âBluetoothâ, this showed the âDevicesâ visible and after a short while both computers showed each other as connectable which is what I did.
next to the laptop, run CoolTerm, select âOptionsâ (Serial Port) âPort:â âBluetooth -Incoming-Portâ, âBaudrate:â 115200, click âOKâ, the port is now connected and open.
now go to the other computer, paste the text from step 16 into âTerminalâ to run the app and, hey presto, the message âText Message to Transmitâ will appear in the CoolTerm window on the laptop.
the java app will now show âReading message from serial portâ in the terminal window and it will be waiting forever for the reply.
on the laptop in CoolTerm go to the menu bar CONNECTION>SEND STRING and a small window opens up, type something in the âSend Stringâ window and click âSendâ, that text SHOULD now appear in the other computers âTerminalâ window looking something like this:-
(11 bytes read): âhelloooooooâ
Reading complete!
Closing MarksMacBookPro-Bluetoo (Dial-In): true [sic]
doj@Marks-iMac ~ %
the app closes down and releases the terminal back to the keyboard and the Bluetooth connection to the other computer is closed.
That is it, a complete test proving the system is working properly.
obviously the Bluetooth is not essential, it was used here for convenience as it was available.
the same functionality can also be had with 2 USB to serial converters and a serial crossover cable (or a home made cable with pins 2 and 3 of the 9 pin D crossed over and pin 5 (ground) joined up.
that can then be used on 2 separate machines or even on the same machine with CoolTerm used as above, 115200 appears to be the default baudrate for this app.