- Saved searches
- Use saved searches to filter your results more quickly
- License
- rm5248/JavaSerial
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Java linux com port
- Platform-independent serial port access for Java
- What is jSerialComm?
- How can use this library in my own project?
- serial port identification with java on ubuntu
- 2 Answers 2
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Use serial ports from Java using standard IO methods.
License
rm5248/JavaSerial
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Useful links(note that most of the information within these links is duplicated to a smaller extent within this README):
Simply put, it is a project with the aim of bringing serial port reading/writing into a Java-specific format. This means that we try to follow the Java conventions whenever possible, using InputStream s and OutputStream s to read and write data to the serial port.
How do I use it in a project?
The easiest way is to use Apache Maven, and add it as a dependency:
dependency> groupId>com.rm5248groupId> artifactId>JavaSerialartifactId> version>0.13version> dependency>
However, you may also download the JARs and add them manually to your project if you so desire. The latest versions may be downloaded here. No other dependencies are required.
Once you have added the JAR as a dependency, you may create a new serial port object:
import com.rm5248.serial.NoSuchPortException; import com.rm5248.serial.NotASerialPortException; import com.rm5248.serial.SerialPort; public class SerialTest < public static void main(String[] args) < try < //This would be COM1, COM2, etc on Windows SerialPort s = new SerialPort( "/dev/ttyUSB0" ); > catch (NoSuchPortException e) < System.err.println( "Oh no! That port doesn't exist!" ); > catch (NotASerialPortException e) < System.err.println( "Oh no! That's not a serial port!" ); > catch (IOException e) < System.err.println( "An IOException occured" ); > > >
From here, you can get the InputStream and OutputStream of the object and read/write from the port.
If you don’t need to know about the serial lines at all, open up the serial port like the following:
new SerialPort( "/dev/ttyUSB0", SerialPort.NO_CONTROL_LINE_CHANGE );
Otherwise a new thread will be created for each serial port that is opened.
As of version 0.11, you can also use the new SerialPortBuilder class to easily set settings and create a serial port:
import com.rm5248.serial.NoSuchPortException; import com.rm5248.serial.NotASerialPortException; import com.rm5248.serial.SerialPort; import com.rm5248.serial.SerialPortBuilder; public class SerialTest < public static void main(String[] args) < try < SerialPort s = new SerialPortBuilder() .setPort( "/dev/ttyUSB0" ) .setBaudRate( SerialPort.BaudRate.B4800 ) .build(); > catch (NoSuchPortException e) < System.err.println( "Oh no! That port doesn't exist!" ); > catch (NotASerialPortException e) < System.err.println( "Oh no! That's not a serial port!" ); > catch (IOException e) < System.err.println( "An IOException occured" ); > > >
JNI and Environment Variables
All of the JNI code is extracted from the JAR file and loaded at run-time, so there is no fiddling of libraries that has to be done. If you do require special JNI code for some reason, you can set the following environment variables when starting up Java:
com.rm5248.javaserial.lib.path - The directory to look in for the javaserial.[dll|so] com.rm5248.javaserial.lib.name - The name of the library to load(default:javaserial)
Set them like the following:
java -Dcom.rm5248.javaserial.lib.path=/path/to/javaserial.[dll|so|jnilib] -Dcom.rm5248.javaserial.lib.name=custom-name
Pre-compiled binaries are provided for:
Why a new serial port library?
First, let’s go through some of the prominent serial port libraries and talk about what I feel their deficiencies are:
- Java Comm API — The API that most of these libraries use. Predates the Java community process. It’s a stupid API that appears to be designed to mirror how serial ports on Windows work.
- RXTX — the old guard. Uses the Java Comm API. There are a few forks, but the main branch does not seem to receive updates frequently. The code is very complicated(>25000 lines!). The settings that it opens the serial port with on Linux are not the best for getting raw data.
- JSSC — Has an API similar to the Java Comm API. Doesn’t provide an easy way of getting raw data from the serial port. The code is relatively small though(
- PureJavaComm — I haven’t used this in the past, so I can’t comment on it.
All of the above libraries have their own strengths and weaknesses. If they use the Java Comm API, they are hobbled by the fact that it is a poorly designed API compared to the newer Java APIs. It was also made in an era before enums, so it makes improper programming easier. Advantages of JavaSerial:
- Small code size(~3800 total lines of code, Java/JNI)
- Auto-extracting JNI code means no worrying about native libraries for each platform.
- java.io compatible — uses standard InputStream and OutputStream to read/write data
- Raw serial port settings on Linux to get all the bytes all the time — uses the same settings as GTKTerm
- Open up any port you want as a serial port — no need to enumerate all ports before opening up a port
- Enum-based settings allow for clear programming
- No external dependencies
Disadvantages of JavaSerial:
- No locking of the serial port — There’s no portable way to do this, so any locking done will be on the Java side. This is a consequence of being rather simple, and as such it not a primary focus of the library.
- .
About
Use serial ports from Java using standard IO methods.
Java linux com port
View on GitHub
Platform-independent serial port access for Java
What is jSerialComm?
jSerialComm is a Java library designed to provide a platform-independent way to access standard serial ports without requiring external libraries, native code, or any other tools. It is meant as an alternative to RxTx and the (deprecated) Java Communications API, with increased ease-of-use, an enhanced support for timeouts, and the ability to open multiple ports simultaneously.
Some of the features of this library include:
- Platform-independent library deployment (automatically uses correct native library based on current architecture)
- Very lightweight and efficient implementation
- Enumerates all available serial ports on a machine
- Returns both a system port description and a friendly device description
- User-specifiable port descriptors including symbolic links
- Configurable ports according to baud rate, data bits, stop bits, and parity
- Configurable port timeouts (blocking and non-blocking) for both reading and writing
- Configurable flow control parameters for the serial port (CTS, RTS/CTS, DSR, DTR/DSR, XOn/XOff)
- Ability to read and write raw data bytes directly to the serial port
- Ability to read and write byte streams via Java’s InputStream and OutputStream interfaces
- Event-based reading and writing via callbacks
- Callback notification when:
- New data is available for reading
- All data has been successfully written
- A complete fixed-length data packet has arrived
- A delimited string-based message has been received
- Modem control lines have changed state
- Communication errors have been encountered
Additionally, this library can be used in any Java project intended for use on the following platforms:
- Windows XP and later (32-bit, 64-bit, ARM, and ARM64)
- Mac OS X Tiger (10.4) and later (32/64-bit Intel and Apple Silicon)
- All Linux distributions (32/64-bit x86, ARM, and PowerPC)
- Solaris 10 and later (32/64-bit x86 and SPARC)
- FreeBSD (32/64-bit x86 and ARM64)
- OpenBSD (32/64-bit x86)
- ARM/Intel/AMD Mobile Linux derivatives (e.g. RaspberryPi, Beaglebone, etc.)
- Android 4.1 (Jelly Bean) and later
How can use this library in my own project?
One of the most convenient features of this library is that it allows you to simply include the JAR file in your custom project, and it will automatically select and load the correct native library for your platform and architecture. As such, you can make use of this library by simply copying the jSerialComm.jar file into your project directory and linking to it as you would any other JAR file.
To access the contents of the library in your project, make sure to import com.fazecast.jSerialComm.* into your java files. You can then generate a list of all available serial ports on your system (real or virtual), by calling the following static method:
This will return an array of SerialPort objects through which you can iterate. See the Javadoc Library Reference for a complete overview of this library and its methods. Alternately, if you already know the port descriptor of the port you wish to use (e.g., «/dev/ttyS0» or «COM3»), or if you are using this library with pseudo-terminals (e.g., «/dev/pts/14»), you can create a SerialPort object using the following static method:
SerialPort.getCommPort(String portDescriptor)
Note for Linux users: Serial port access is limited to certain users and groups in Linux. To enable user access, you must open a terminal and enter the following commands before jSerialComm will be able to access the ports on your system. Don’t worry if some of the commands fail. All of these groups may not exist on every Linux distro. (Note, this process must only be done once for each user):
sudo usermod -a -G uucp username
sudo usermod -a -G dialout username
sudo usermod -a -G lock username
sudo usermod -a -G tty usernameReplace the username parameter with your current username. (If you are not sure what your username is, type whoami and it will tell you.) If you are using SUSE 11.3 or higher, replace the ‘-a -G’ flags with a single ‘-A’ flag. Log out and you should have access to the serial port after logging back in.
Additionally, if you are using an automated build system (such as Maven), you can import this library directly into your project as a dependency from the Maven Central Repository instead of copying the .jar file manually. Use one of the following dependency declarations depending on the build system you are using:
com.fazecast jSerialComm [2.0.0,3.0.0) @Grab(group='com.fazecast', module='jSerialComm', version='[2.0.0,3.0.0)')
serial port identification with java on ubuntu
I’m trying to connect a serial application on ubuntu with Java
After searching and reading resources,I add comm.jar and RXTXcomm.jar in the library.
I use the following code to identify the comports. In my system there are three ports but it is showing false in ports.hasMoreElements() method.
Kindly look into the code and help me.String wantedPortName = "/dev/ttya"; ///dev/ttyS0 و /dev/ttyS1 نیز تست شد Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); CommPortIdentifier portId = null; // will be set if port found while (portIdentifiers.hasMoreElements()) < CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL && pid.getName().equals(wantedPortName)) < portId = pid; break; >> if(portId == null)
2 Answers 2
In my case, I’m using Ubuntu, and my notebook does not have any serial or paralel ports.
So, you have to simulate this kind of port:
socat -d -d pty,raw,echo=0, pty,raw,echo=0
According to output, pay attention to «devices» created:
2014/02/05 01:04:32 socat[7411] N PTY is /dev/pts/2 2014/02/05 01:04:32 socat[7411] N PTY is /dev/pts/3 2014/02/05 01:04:32 socat[7411] N starting data transfer loop with FDs [3,3] and [5,5]
Stop socat [CTRL]+[C] and symlink it to a location that RXTX will recognise as a device, due to «tty» prefix:
sudo ln -s /dev/pts/2 /dev/ttyUSB02 sudo ln -s /dev/pts/3 /dev/ttyUSB03
socat -d -d pty,raw,echo=0 pty,raw,echo=0
Now, using following code, you will see 2 virtual ports:
Enumeration portList = CommPortIdentifier.getPortIdentifiers();//this line was false System.out.println(portList.hasMoreElements()); while(portList.hasMoreElements()) < System.out.println("Has more elements"); CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) < System.out.println(portId.getName()); >else < System.out.println(portId.getName()); >>
true Has more elements /dev/ttyUSB03 Has more elements /dev/ttyUSB02