Serial port in java linux

Java Comm Serial API How-To for Linux

Special thanks to Trent Jarvi and Kevin Hester for putting together RXTX and JCL. These two gentlemen have done an excellent job of porting the original Sun Java Comm API to the Linux OS. However, with all due respect, and not to be insulting, their installation documentation leaves much to be desired. This is the primary reason I have chosen to put this document together.

Getting Started

For the purposes of this paper, it will be assumed that you have installed a JSDK and it is located in:

And java is then located in:

If your setup is different, please adjust accordingly.

Installing RXTX

First, obtain the RXTX bins package from:

Decompress and Untar this package:

/bin/gzip —decompress rxtx-bins.1.tar.gz

At this point, you’ll have an rxtx-bins.1 directory. Next, you’ll need to copy the shared objects into your java installation:

cp rxtx-bins.1/1.4/i386-pc-linux/libParallel.so /usr/java/j2sdk1.4.0/jre/lib/i386/

cp rxtx-bins.1/1.4/i386-pc-linux/libSerial.so /usr/java/j2sdk1.4.0/jre/lib/i386/

If you are installing on an architecture other than an x86, you’ll need to adjust both the /i386-pc-linux / and the /i386/ accordingly.

Next, you’ll need to install the jcl.jar file:

cp rxtx-bins.1/1.4/jcl.jar /usr/java/j2sdk1.4.0/jre/lib/ext/

At this point, the RXTX installation is complete.

Installing Comm

The final step to getting the Java Comm API working under Linux, is to install the Comm API itself. At this point, you have all of the necessary kernel-level drivers installed. Because Linux is a form of Unix, the authors of RXTX and JCL have wisely decided to reuse sun’s solaris (unix) comm library. At this point, you must download and install this library:

Make sure you choose the Solaris/Sparc TM version. Next, you must Decompress and Untar this package:

/bin/gzip —decompress javax_comm-2_0_2-solsparc.tar.Z

/bin/tar xf javax_comm-2_0_2-solsparc.tar

Next, you’ll need to install the comm.jar file:

cp commapi/comm.jar /usr/java/j2sdk1.4.0/jre/lib/ext/

At this point, we are almost finished. We just need to create the properties file that the Comm API will use to load the drivers (.so files). To create this file, type the following command:

/bin/echo Driver=gnu.io.RXTXCommDriver > /usr/java/j2sdk1.4.0/jre/lib/javax.comm.properties

Congratulations! You have installed the Linux Comm API.

Tips

While windows uses COM and LPT designators for port identifiers, Linux is a bit different. Use the following table to identify your ports:

Port Windows port identifier What you use in Linux
Serial Port 1 COM1 /dev/ttyS0
Serial Port 2 COM2 /dev/ttyS1
Parallel Port 1 LPT1 /dev/lp0

Troubleshooting

Several people have emailed me and reported that they get the following error:

Exception in thread «main» java.lang.VerifyError: (class:
gnu/io/RXTXPort$SerialOutputStream, method: write signature: ([BII)V) Illegal
use of nonvirtual function call
at gnu.io.RXTXPort.(RXTXPort.java)
at gnu.io.RXTXCommDriver.getCommPort(RXTXCommDriver.java)
at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:547)

I personally have not experienced this problem, but if you encounter this error, you can work around it by adding -noverify to your command line:

Читайте также:  Как банихопить в css

Also, it has been suggested that recompiling the jcl with the java compiler you have chosen will resolve this issue. As I have never experienced this verification problem, I have no way to test this. It should also be noted that the purpose of this paper is to be a quick and easy way to get serial and parallel port access in Java on Linux. Most of the instructions on this page as well as the VerifyError are irrelevant if you chose to download the RXTX source and do a manual compile and installation.

Other Resources

Источник

Serial port in java linux

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 username

    Replace 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)')

    Источник

    Работаем с COM-портом из Java при помощи jSSC

    Уж много воды утекло с тех пор, как мы пользовались мышами на com-у, модемами, играли «по нему» в игры за неимением сети, перекидывали файлики и …, чего только мы с ним не делали.

    Всё, на дворе 21 век, USB 3.0 подмял по себя всё и вся, com-ы вымерли и теперь «новорожденные» программисты смотрят на этот разъём с недоумением и укоризной. Но, умерли не все, кое-кто всё-таки остался… Эти бравые парни шлют байты по трём проводам и с со снисходительной улыбкой смотрят на «новорожденных». И о том, чем эти парни пользуются выполняя свою работу я и хочу поведать в этой небольшой статье. Речь в ней пойдёт о маленькой библиотечке по имени jSSC.

    Судя по тому, как люди на Хабре отзываются о com-е видимо многие действительно считают что он уже не нужен и не понимают зачем кто-то до сих пор его использует, зачем пишут библиотеки и софт, производят оборудование с этим портом на борту. От себя могу сказать лишь одно, дабы не разводить холивар, как мне кажется, он пропал лишь для домашнего использования, а на производствах и в различных лабораториях он есть и чувствует себя просто отлично. На то есть много причин, но основная как мне кажется это простота и удобство в работе.

    Но разговор сейчас не о том, так что не будем отвлекаться. Итак, jSSC это Java библиотека для простой работы с COM-портом (Java Simple Serial Connector). Своё официальное, публичное начало она берёт в 2010 году. Именно тогда было решено поделиться ей с разработчиками на Java (распространяется под лицензией LGPL). К написанию библиотеки привёл печальный факт – отсутствие адекватных инструментов для работы с этим портом. Многие скажут, и уже говорили, мол есть javax.comm, rxtx ну и ещё есть giovynet (но его нельзя серьёзно рассматривать ни при каких обстоятельствах), но к сожалению не всё так просто. Нашей основной ОС является Windows, и использовать javax.comm 3.0 не получится, rxtx-же не подошёл из-за своей нестабильности. В результате делать было нечего, пришлось писать свою библиотеку.

    Во главу угла при разработке была поставлена простота использования, ведь каждый день приходится работать с оборудованием, и хочется, чтобы работа доставляла удовольствие. Но пусть это вас не вводит в заблуждение, простота это не серебряная пуля и не кнопка «сделать пи*дато», нужно понимать чего вы хотите получить от устройства, как оно работает и как вообще происходит взаимодействие устройств по com-у. При разработке больший уклон был сделан в сторону Windows стиля разработки под com-порт, в основном это выражается в именовании констант для установки маски ивентов, режима управления потоком, разбора ошибок и т.д.

    • SerialNativeInterface – класс, который предоставляет доступ ко всем «нативным» методам jSSC.
    • SerialPort – класс, с помощью которого мы уже будем непосредственно работать с нужным нам портом.
    • SerialPortEventListener – интерфейс, который необходимо реализовать, если мы хотим использовать ивенты.
    import jssc.SerialPort; import jssc.SerialPortEvent; import jssc.SerialPortEventListener; import jssc.SerialPortException; public class Test < private static SerialPort serialPort; public static void main(String[] args) < //Передаём в конструктор имя порта serialPort = new SerialPort("COM1"); try < //Открываем порт serialPort.openPort(); //Выставляем параметры serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //Включаем аппаратное управление потоком serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT); //Устанавливаем ивент лисенер и маску serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR); //Отправляем запрос устройству serialPort.writeString("Get data"); >catch (SerialPortException ex) < System.out.println(ex); >> private static class PortReader implements SerialPortEventListener < public void serialEvent(SerialPortEvent event) < if(event.isRXCHAR() && event.getEventValue() >0) < try < //Получаем ответ от устройства, обрабатываем данные и т.д. String data = serialPort.readString(event.getEventValue()); //И снова отправляем запрос serialPort.writeString("Get data"); >catch (SerialPortException ex) < System.out.println(ex); >> > > >
    • Управление линиями RTS, DTR
    • Получение статуса линий CTS, DSR, RING, RLSD
    • Получение количества байт в буферах
    • Очистка буферов порта
    • Отправка сигнала Break
    • Управление потоком
    • Получение списка com-портов в системе
    • Irda управление для HTPC (проект одной фирмы из США)
    • Тюнинг Mitsubishi Eclipse (привет туда же – в США)
    • Серверное ПО в центре сетевых технологий (Польша)
    • Система взвешивания посылок (на сколько я понял) для какой-то службы доставки (для этих ребят я писал мостик из Java в JavaScript и они вознаградили мои труды прислав 100 USD, кстати тоже США)
    • Различные учебные проекты (Индия, Россия и может быть ещё кто-то)
    • и т.д.

    Ну что же, разрешите откланяться, прошу прощения за немного сумбурное повествование, и надеюсь, что кому-то из вас jSSC поможет в работе. Буду рад ответить на ваши вопросы.

    Источник

Оцените статью