I. Environment configuration (1) decompress and copy the file
Decompress javacomm20-win32.zip
Copy win32com. DLL to the <java_home>/JRE/bin and <JDK>/bin directories.
Copy comm. jar to the <java_home>/JRE/lib/EXT and <JDK>/lib directories.
Copy javax. Comm. properties to the <java_home>/JRE/lib and <JDK>/lib directories.
(2) Set Environment Variables
Classpath = <java_home>/JRE/lib/EXT/COMM. jar; % classpath %
(3) Add the jar package to the Eclipse project
Right-click the project and choose Properties> JAVA build path> libraries> Add external jar to find the location of Comm. jar in <JDK>/lib.
Ii. API overview Interface
Part of the loadable Device Driver Interface
Commp ortownershiplistener transmits ownership events of various communication ports
Parallelporteventlistener transmits parallel port events
Serialporteventlistener transmits a serial port event
Class
Comport Communication Port
Commp ortidentifier Communication Port Management
Parallelport: Parallel Communication Port
Parallelportevent parallel port event
SerialPort RS-232 serial communication port
Serialportevent exception class
Nosuchportexception thrown when the driver cannot find the specified port
Portinuseexception thrown when a specified port is in use
The unsupportedcommoperationexception driver cannot throw
(1) commp ORT class
Abstract class that describes the ports supported by the underlying system. It includes some high-level Io control methods, which are common for all different communication ports. Both SerialPort and parallelport are its subclasses.
(2) commp ortidentifier
It is mainly used to manage and set serial ports and is the core class for access control over serial ports. The main method is as follows:
Addportname (string, Int, commdriver): add the port name to the port list.
Addportownershiplistener (commp ortownershiplistener) add listener owned by the port
Removeportownershiplistener (commp ortownershiplistener) removes listener owned by the port
Getcurrentowner () obtains the object or application that currently occupies the port.
Get the port name by getname ()
Getportidentifier (comport) obtains the comportidentifier type object of the port opened by the parameter.
Getportidentifier (string) is used to obtain the commp ortidentifier type object of the port named after the parameter.
Getportidentifiers () to obtain the list of ports in the system
Getporttype () indicates the port type.
Iscurrentlyowned () checks whether the current port is occupied
Open (filedescriptor) open the port with the type described in the file
Open (string, INT) Open 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 function set required for serial communication. You can directly read, write, and set the serial port.
A: serial port parameter functions
Getbaudrate () Get the baud rate getparity () Get the test Type
Getdatabits () Get the data bits getstopbits () Get the Stop bits
Setserialportparams (INT, Int, Int, INT) sets the serial port parameters to (baud rate, data bit, stop bit, parity check) in sequence)
Close () Close the serial port
Getoutputstream () gets the output stream of the outputstream type.
Getinputstream () obtains the inputstream input stream.
B: Events and event Methods
Whether iscd () has a carrier
Iscts () Whether to clear sending
Isdsr () Data ready
Isdtr () Data Terminal ready
ISRI (): whether to enable Alarm Detection
Whether isrts () requires sending
Addeventlistener (serialporteventlistener) adds a serial event listener to the SerialPort object
Removeeventlistener () removes the serial event listener from the SerialPort object.
Geteventtype () returns the int type as the event type.
Sendbreak (INT) specifies the duration of the interrupt process. The parameter value is millisecond.
Setrts (Boolean) sets or clears the RTS bit
Setdtr (Boolean) set or clear DTR bit
Notifyonbreakinterrupt (Boolean) sets the interrupt event
Notifyoncarrierdetect (Boolean) sets the carrier listening event
Notifyoncts (Boolean) sets to clear sending events
Notifyondataavailable (Boolean) sets an event with data in the serial port
Notifyondsr (Boolean) sets data backup events
Notifyonframingerror (Boolean) is used to set an error event.
Notifyonoutputempty (Boolean) sets the sending buffer to a null event
Notifyonparityerror (Boolean) is used to set a parity check error event.
Notifyonringindicator (Boolean)
C: static member variables of serial port Parameters
Description of member variables description of member variables
Databits_5 data bit: 5 stopbits_2 stop bit: 2 parity_odd odd Test
Databits_6 data bit: 6 stopbits_1 stop bit: 1 parity_mark mark Test
Databits_7 data bit is 7 stopbits_00005 stop is 1.5 parity_none Space Test
Databits_8 data bit: 8 parity_even even test parity_space no test