File List:
Comm. Jar
Javax. Comm. Properties
Win32com. dll
Copy the javax. Comm. properties and. DLL files to JRE/lib/EXT. Under JDK, and copy comm. jar to the classpath directory of LIB under the project you created.
After the environment is created, we use the sun example to test it:
1. Write text to COM1
Package com. example;
/*
* @ (#) Simplewrite. Java 1.12 98/06/25 SmI
*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Sun proprietary/confidential. Use is subject to license terms.
*
* Sun grants you ("licensee") a non-exclusive, royalty free, license
* To use, modify and redistribute this software in source and binary
* Code form, provided that I) This copyright notice and license appear
* On all copies of the software; and ii) licensee does not utilize
* Software in a manner which is disparaging to sun.
*
* This software is provided "as is," without a warranty of any kind.
* All express or implied conditions, representations and warranties,
* Including any implied warranty of merchantability, fitness for
* Particle purpose or NON-INFRINGEMENT, are hereby excluded. Sun and
* Its Licensors shall not be liable for any damages suffered
* Licensee as a result of using, modifying or distributing
* Software or its derivatives. In no event will sun or its Licensors
* Be liable for any lost revenue, profit or data, or for direct,
* Indirect, special, consequential, incidental or punitive damages,
* However caused and regardless of the theory of liability, arising
* Out of the use of or inability to use software, even if Sun has been
* Advised of the possibility of such damages.
*
* This software is not designed or intended for use in on-line control
* Of aircraft, air traffic, aircraft navigation or aircraft
* Communications; or in the design, construction, operation or
* Maintenance of any nuclear facility. Licensee represents and
* Warrants that it will not use or redistribute the software for such
* Purposes.
*/
Import java. Io. ioexception;
Import java. Io. outputstream;
Import java. util. enumeration;
Import javax. Comm. comatrix ortidentifier;
Import javax. Comm. portinuseexception;
Import javax. Comm. SerialPort;
Import javax. Comm. unsupportedcommoperationexception;
/**
* Class declaration
*
*
* @ Author
* @ Version 1.10, 08/04/00
*/
Public class simplewrite {
Static enumeration portlist;
Static commp ortidentifier portid;
Static string messagestring = "Hello, world! ";
Static SerialPort;
Static outputstream;
Static Boolean outputbufferemptyflag = false;
/**
* Method declaration
*
*
* @ Param ARGs
*
* @ See
*/
Public static void main (string [] ARGs ){
Boolean portfound = false;
String defaultport = "COM1 ";
If (ARGs. length> 0 ){
Defaultport = ARGs [0];
}
Portlist = commp ortidentifier. getportidentifiers ();
System. Out. println (portlist. hasmoreelements ());
While (portlist. hasmoreelements ()){
Portid = (comatrix ortidentifier) portlist. nextelement ();
System. Out. println ("portid:" + portid. getname ());
If (portid. getporttype () = comatrix ortidentifier. port_serial ){
If (portid. getname (). Equals (defaultport )){
System. Out. println ("found port" + defaultport );
Portfound = true;
Try {
SerialPort =
(SerialPort) portid. Open ("simplewrite", 2000 );
} Catch (portinuseexception e ){
System. Out. println ("port in use .");
Continue;
}
Try {
Outputstream = SerialPort. getoutputstream ();
} Catch (ioexception e ){}
Try {
SerialPort. setserialportparams (9600,
SerialPort. databits_8,
SerialPort. stopbits_1,
SerialPort. parity_none );
} Catch (unsupportedcommoperationexception e ){}
Try {
SerialPort. notifyonoutputempty (true );
} Catch (exception e ){
System. Out. println ("error setting event notification ");
System. Out. println (E. tostring ());
System. Exit (-1 );
}
System. Out. println (
"Writing/" "+ messagestring +"/""
+ SerialPort. getname ());
Try {
Outputstream. Write (messagestring. getbytes ());
} Catch (ioexception e ){}
Try {
Thread. Sleep (2000); // be sure data is xferred before closing
} Catch (exception e ){}
SerialPort. Close ();
System. Exit (1 );
}
}
}
If (! Portfound ){
System. Out. println ("Port" + defaultport + "Not found .");
}
}
}
Running result:
True
Portid: com4
Portid: COM1
Found port COM1
Writing "Hello, world! "To COM1
2. receive text from Port COM1
Package com. example;
/*
* @ (#) Simpleread. Java 1.12 98/06/25 SmI
*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Sun proprietary/confidential. Use is subject to license terms.
*
* Sun grants you ("licensee") a non-exclusive, royalty free, license
* To use, modify and redistribute this software in source and binary
* Code form, provided that I) This copyright notice and license appear
* On all copies of the software; and ii) licensee does not utilize
* Software in a manner which is disparaging to sun.
*
* This software is provided "as is," without a warranty of any kind.
* All express or implied conditions, representations and warranties,
* Including any implied warranty of merchantability, fitness for
* Particle purpose or NON-INFRINGEMENT, are hereby excluded. Sun and
* Its Licensors shall not be liable for any damages suffered
* Licensee as a result of using, modifying or distributing
* Software or its derivatives. In no event will sun or its Licensors
* Be liable for any lost revenue, profit or data, or for direct,
* Indirect, special, consequential, incidental or punitive damages,
* However caused and regardless of the theory of liability, arising
* Out of the use of or inability to use software, even if Sun has been
* Advised of the possibility of such damages.
*
* This software is not designed or intended for use in on-line control
* Of aircraft, air traffic, aircraft navigation or aircraft
* Communications; or in the design, construction, operation or
* Maintenance of any nuclear facility. Licensee represents and
* Warrants that it will not use or redistribute the software for such
* Purposes.
*/
Import java. Io .*;
Import java. util .*;
Import javax. Comm .*;
/**
* Class declaration
*
*
* @ Author
* @ Version 1.8, 08/03/00
*/
Public class simpleread implements runnable, serialporteventlistener {
Static commp ortidentifier portid;
Static enumeration portlist;
Inputstream;
SerialPort;
Thread readthread;
/**
* Method declaration
*
*
* @ Param ARGs
*
* @ See
*/
Public static void main (string [] ARGs ){
Boolean portfound = false;
String defaultport = "COM1 ";
If (ARGs. length> 0 ){
Defaultport = ARGs [0];
}
Portlist = commp ortidentifier. getportidentifiers ();
While (portlist. hasmoreelements ()){
Portid = (comatrix ortidentifier) portlist. nextelement ();
If (portid. getporttype () = comatrix ortidentifier. port_serial ){
If (portid. getname (). Equals (defaultport )){
System. Out. println ("found port:" + defaultport );
Portfound = true;
Simpleread reader = new simpleread ();
}
}
}
If (! Portfound ){
System. Out. println ("Port" + defaultport + "Not found .");
}
}
/**
* Constructor Declaration
*
*
* @ See
*/
Public simpleread (){
Try {
SerialPort = (SerialPort) portid. Open ("simplereadapp", 2000 );
} Catch (portinuseexception e ){}
Try {
Inputstream = SerialPort. getinputstream ();
} Catch (ioexception e ){}
Try {
SerialPort. addeventlistener (this );
} Catch (toomanylistenersexception e ){}
SerialPort. yonyondataavailable (true );
Try {
SerialPort. setserialportparams (9600, SerialPort. databits_8,
SerialPort. stopbits_1,
SerialPort. parity_none );
} Catch (unsupportedcommoperationexception e ){}
Readthread = new thread (this );
Readthread. Start ();
}
/**
* Method declaration
*
*
* @ See
*/
Public void run (){
Try {
Thread. Sleep (20000 );
} Catch (interruptedexception e ){}
}
/**
* Method declaration
*
*
* @ Param event
*
* @ See
*/
Public void serialevent (serialportevent event ){
Switch (event. geteventtype ()){
Case serialportevent. BI:
Case serialportevent. OE:
Case serialportevent. Fe:
Case serialportevent. PE:
Case serialportevent. CD:
Case serialportevent. CTS:
Case serialportevent. DSR:
Case serialportevent. Ri:
Case serialportevent. output_buffer_empty:
Break;
Case serialportevent. data_available:
Byte [] readbuffer = new byte [20];
Try {
While (inputstream. Available ()> 0 ){
Int numbytes = inputstream. Read (readbuffer );
}
System. Out. Print (new string (readbuffer ));
} Catch (ioexception e ){}
Break;
}
}
}
After the program is started:
Found port: COM1