Home Java javaTutorial JAVA serial port programming

JAVA serial port programming

Dec 19, 2016 pm 04:39 PM

1. Environment configuration

(1) Unzip the copied file

Unzip javacomm20-win32.zip

Copy win32com.dll to the /jre/bin and /bin directories

Put comm Copy .jar to the /jre/lib/ext and /lib directories

Copy javax.comm.properties to the /jre/lib and /lib directories

(2) Set the environment variable

CLASSPATH=/jre /lib/ext /comm.jar;%classpath%

(3) Add the jar package to the Eclipse project

"Right-click the project-> ; properties-> java build path->libraries->add external jar" to find the location of comm.jar placed in /lib.

2. API Overview

Interface

CommDriver Part of the loadable device driver interface

CommPortOwnershipListener delivers ownership events of various communication ports

ParallelPortEventListener delivers parallel port events

SerialPortEventListener delivers serial ports Event

Class

CommPort communication port

CommPortIdentifier communication port management

ParallelPort Parallel communication port

ParallelPortEvent Parallel port event

SerialPort RS-232 serial communication port


SerialPortEven t exception class

NoSuchPortException when driven

PortInUseException is thrown when the program cannot find the specified port.

UnsupportedCommOperationException is thrown when the specified port is in use.

is thrown when the driver does not allow the specified operation. (1) CommPort class

describes the port supported by the underlying system abstract class. Contains some high-level IO control methods that are common to all different communication ports. SerialPort (serial port) and ParallelPort (parallel port) are its subclasses.

(2) CommPortIdentifier

is mainly used to manage and set up the serial port. It is the core class for access control of the serial port. The main methods are as follows: dAddPortName (String, int, Commdriver) add the end mouth name to the port list

AddPortownerShiPlistener (COMPORTOWNERSHIPLISHIPLISTENER) Listener (CommptownerShiplistener) Remove the monitor owned by the port

GetCurrentowner ( ) Get the object or application that currently occupies the port

getName() Get the port name

getPortIdentifier(CommPort) Get the CommPortIdentifier type object of the port opened by the parameter

getPortIdentifier(String) Get the CommPortIdentifier type object of the port named with the parameter

getPortIdentifiers() Get the list of ports in the system

getPortType() Get the type of the port

isCurrentlyOwned() Determine whether the current port is occupied

open(FileDescriptor) Open the port with the type described by the file

open(String, int ) Open the port, two parameters: program name, delay time (milliseconds)

(3) SerialPort

describes the underlying interface of the RS-232 serial communication port, which defines the minimum set of functions required for serial communication. You can directly read, write and set up the serial port.

A: Function of serial port parameters

getBaudRate() Get the baud rate getParity() Get the check type

getDataBits() Get the number of data bits getStopBits() Get the stop bits

setSerialPortParams(int, int, int, int ) Set the serial port parameters in order (baud rate, data bits, stop bits, parity check)

close() close the serial port

getOutputStream() get the output stream of OutputStream type

getInputStream() get the input stream of InputStream type

B: Events and event methods

isCD() Whether there is a carrier

isCTS() Whether it is clear to send

isDSR() Whether the data is ready

isDTR() Whether the data terminal is ready

isRI() Whether it rings Detect whether

isRTS() requires sending

addEventListener(SerialPortEventListener) and add a serial port event listener to the SerialPort object

removeEventListener() removes the serial port event listener in the SerialPort object

getEventType() gets the event type that occurred and the return value is int type

sendBreak(int) sets the time of the interruption process, the parameter is the millisecond value

setRTS(boolean ) Set or clear the RTS bit

setDTR(boolean) Set or clear the DTR bit

notifyOnBreakInterrupt(boolean) Set the interrupt event

notifyOnCarrierDetect(boolean) Set the carrier detection event

notifyOnCTS(boolean) Set the clear send event

notifyOnDataAvailable( boolean) Set the event that the serial port has data

notifyOnDSR(boolean) Set the data ready event

notifyOnFramingError(boolean) Set the error event

notifyOnOutputEmpty(boolean) Set the send buffer is empty event

notifyOnParityError(boolean) Set the occurrence Parity error event

notifyOnRingIndicator(boolean) Set ring detection event

C: Static member variable of serial port parameters

Member variable description Member variable description Member variable description

DATABITS_5 Data bits are 5 STOPBITS_2 Stop bits are 2 PARITY_ODD Odd check

DATABITS_6 Data bits are 6 STOPBITS_1 Stop bits are 1 PARITY_MARK Mark check

DATABITS_7 Data bits are 7 STOPBITS_1_5 Stops are 1.5 PARITY_NONE Space check

DATABITS_8 Data bits are 8 PARITY_EVEN Even check PARITY_SPACE No check



For more JAVA serial port programming related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles