Java serial port data read/write (rxtx) in win7)

Source: Internet
Author: User

Reprinted from:

Http://blog.sina.com.cn/s/blog_5ceb51480100rdgw.html

  Recently, in actual projects, Java is required to read and write serial data. Javax. but after several tests, the serial port on the PC can never be read. So I switched the rxtx package and tested it under myeclipse8.6, the test result shows that the available port of the PC can be obtained. Javax. the development and maintenance of comm in Windows has been stopped. The old version of rxtx is supported in javax. comm-win32-2.0-based extensions, the new version of rxtx supports for javax. comm coverage support, that is, javax. comm put all the import
Javax. Comm. * changed to import GNU. Io. * to use it normally, other DLL files only need to be related, without the properties file. The supported port types are much more.

   Rxtx package: http://rxtx.qbang.org/pub/rxtx/rxtx-2.1-7-bins-r2.zip
   After downloading and decompressing the package, the configuration environment is as follows:

   Rxtx-2.1-7-bins-r2 \ windows \ i368-mingw32 \ rxtxserial In the downloaded package. put the DLL under your % java_home % \ JRE \ bin (% java_home % \ JRE \ bin \ rxtxserial. DLL) and c: \ windows \ system32 (c: \ windows \ system32 \ rxtxserial. DLL );

   Place the rxtx-2.1-7-bins-r2 \ rxtxcomm. jar in the downloaded package under % java_home % \ JRE \ Lib \ ext (% java_home % \ JRE \ Lib \ ext \ rxtxcomm. Jar ).

Note: % java_home % indicates the JDK installation path for you, for example, c: \ Program Files \ Java \ jdk1.6.0 _ 10.

   The following is a reference to rxtxcomm in myeclipse. jar, select the Project Build path, select configure build path, select libraries, and click Add exteral jars. In the displayed dialog box, locate rxtxcomm. jar, select it, click open, and then click OK.

   Now, you can write the program. First, you can get the serial port on your PC. The program is as follows:

Package test;
Import java. util. enumeration;
Import GNU. Io .*;

Public class testserial {
 @ Suppresswarnings ("unchecked ")
 Static enumeration portlist;
 Static commp ortidentifier portid;

 

 Public static void main (string [] ARGs ){
  Try {
     Portlist = commp ortidentifier. getportidentifiers (); // obtain the port on the current connection
     While (portlist. hasmoreelements ()){
      Portid = (comatrix ortidentifier) portlist. nextelement ();
      If (portid. getporttype () = comatrix ortidentifier. port_serial) {// judge if the port type is serial port
       // If (portid. getname (). Equals ("com3") {// determine if the com3 port is enabled
        System. Out. println (portid. getname ());
       }
      }
     //}
    } Catch (exception e ){
     E. printstacktrace ();
    } 
 } 
}

Test results:

Stable Library
========================================================== =
Native lib version = RXTX-2.1-7
Java lib version = RXTX-2.1-7

COM1
Com3

 

The following program is used as the server. When you receive data sent by WiFi, you can write data to the serial port:

Package test;
Import java. Io .*;
Import java.net .*;
Import GNU. Io .*;

Import java. util .*;
Public class tcpserver implements runnable {
 
 Public static final string serverip = "192.168.1.108 ";
 Public static final int serverports = 7788;
 Static commp ortidentifier portid; // serial communication management class
 @ Suppresswarnings ("unchecked ")
 Static enumeration portlist; // Enumeration of connected ports
 Public static SerialPort;   // Serial port reference

 

 

Reference: http://dev.firnow.com/course/3_program/java/javajs/20100112/189411.html

 


 Public static outputstream out;

 Public void run (){
  Try {
   System. Out. println ("connection ...");
   Serversocket = new serversocket (SERVERPORT );
   While (true ){
    Socket Client = serversocket. Accept ();
    System. Out. println ("Receiveing ...");
    Try {
     Bufferedreader BF = new bufferedreader (New inputstreamreader (client. getinputstream ()));
     String STR = BF. Readline ();
     If (Str. Equals ("0 ")){
      Out. Write (48 );
      Out. Flush ();
     } Else if (Str. Equals ("1 ")){
      Out. Write (49 );
      Out. Flush ();
     }
     System. Out. println ("Received: '" + STR + "'");
     
    } Catch (exception E)
    {
     E. tostring ();
    } Finally {
     Out. Close ();
     Client. Close ();
     System. Out. println ("done ");
    }
   }
  } Catch (exception E)
  {
   E. tostring ();
  }
 }

 
 Public staticVoid main (string [] ARGs ){
  
  Thread serverthread = new thread (New tcpserver ());
  Serverthread. Start ();
  Try {
     Portlist = commp ortidentifier. getportidentifiers (); // obtain the port on the current connection
     While (portlist. hasmoreelements ()){
      Portid = (comatrix ortidentifier) portlist. nextelement ();
      If (portid. getporttype () = comatrix ortidentifier. port_serial) {// judge if the port type is serial port
       If (portid. getname (). Equals ("COM1") {// judge if the com3 port is enabled
      
      SerialPort = (SerialPort) portid. Open ("COM1", 2000 );
      SerialPort. setserialportparams (38400, SerialPort. databits_8, SerialPort. stopbits_1, SerialPort. parity_none );
      Out = SerialPort. getoutputstream ();
      System. Out. println (portid. getname ());
       }
      }
     }
    } Catch (exception e ){
     E. printstacktrace ();
    }
  // Todo auto-generated method stub
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.