Последовательный порт на java

Последовательный порт на java

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 поможет в работе. Буду рад ответить на ваши вопросы.

    Источник

    Java Serial Ports

    Java Serial Ports

    Java Serial Ports are used for serial communication with windows applications. Many options are available to work with serial ports, but most are out of date.

    Even the Java API javax.comm is deprecated in new versions. This tutorial demonstrates how to work with serial ports in Java.

    Java Serial Ports Using JSerialComm API

    JSerialComm is a Java library, which provides a platform-independent way to use serial ports, and we don’t require external libraries. The JSerialComm is the best alternative to outdated RxTx and deprecated javax.comm libraries.

    JSerialComm is easy to use and has enhanced timeouts support. It also supports opening multiple ports simultaneously.

    Follow the steps to use the serial ports in Java.

    Add the jar file to your build path, and then you are ready to use JSerialComm .

    Let’s try a simple example to print the serial ports on our device. It should be mentioned here that if your device doesn’t have ports, the code will not print any port.

    package delftstack;  import com.fazecast.jSerialComm.SerialPort;  public class Serial_Port    public static void main(String[] args)   System.out.println("Hello this is a java program to check ports from delftstack");   SerialPort[] Device_Ports = SerialPort.getCommPorts();   for (SerialPort each_port: Device_Ports)   System.out.println(each_port.getSystemPortName());  >  >  > 

    It doesn’t show any ports for our system because it doesn’t have any ports, shown from the output and the device manager screenshot.

    Hello this is a java program to check ports from delftstack 

    Device Manager

    This link also provides a Java project in a zip file that can be imported. This project has a test program that shows almost all the operations of serial ports.

    JSerialComm 1

    Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

    Источник

    Читайте также:  Box whisker plot python
Оцените статью