Home  >  Article  >  Java  >  JAVA serial port programming

JAVA serial port programming

高洛峰
高洛峰Original
2016-12-19 16:39:231729browse

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:
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