Java Read COM port

Source: Internet
Author: User

</pre>java read the serial port technology <p></p><p>       1, rxtx  </p> <p>   http://rxtx.qbang.org/wiki/index.php/Download</p><pre code_snippet_id= "1674392" Snippet_file_name= "blog_20160506_2_2194645" name= "code" class= "java" >package Fpx.grs.aliexpress.com;import Java.io.ioexception;import Java.io.inputstream;import Java.util.enumeration;import Java.util.toomanylistenersexception;import Gnu.io.commportidentifier;import Gnu.io.portinuseexception;import Gnu.io.serialport;import Gnu.io.serialportevent;import Gnu.io.serialporteventlistener;import    Gnu.io.unsupportedcommoperationexception;public class Simpleread implements Runnable, Serialporteventlistener {    Static Commportidentifier Portid;    Static enumeration portlist;    InputStream InputStream;    SerialPort SerialPort;    Thread Readthread;        public static void Main (string[] args) {portlist = Commportidentifier.getportidentifiers (); while (portlist. hasMoreElements ()) {Portid = (commportidentifier) portlist.nextelement ();            if (portid.getporttype () = = commportidentifier.port_serial) {if (Portid.getname (). Equals ("COM4")) { if (Portid.getname (). Equals ("/dev/term/a")) {Simpleread reader = new Simplerea                D (); }}}} public Simpleread () {try {SerialPort = (serialPort) portid.open ("Simpl        Ereadapp ", 2000);        } catch (Portinuseexception e) {System.out.println (e);}        try {InputStream = Serialport.getinputstream ();    } catch (IOException e) {System.out.println (e);}    try {Serialport.addeventlistener (this);        } catch (Toomanylistenersexception e) {System.out.println (e);}        Serialport.notifyondataavailable (TRUE); try {serialport.setserialportparams (9600, Serialport.databits_8, Serialport.stop        Bits_1,        Serialport.parity_none);        } catch (Unsupportedcommoperationexception e) {System.out.println (e);}        Readthread = new Thread (this);    Readthread.start ();        } public void Run () {try {thread.sleep (20000);    } catch (Interruptedexception e) {System.out.println (e);} } public void Serialevent (Serialportevent event) {switch (Event.geteventtype ()) {case Serialportevent.bi : Case SerialPortEvent.OE:case SerialPortEvent.FE:case SerialPortEvent.PE:case Serialportev Ent. Cd:case SerialPortEvent.CTS:case SerialPortEvent.DSR:case SerialPortEvent.RI:case Serialpo        RtEvent.OUTPUT_BUFFER_EMPTY:break;            Case serialportevent.data_available:byte[] Readbuffer = new BYTE[20]; try {while (inputstream.available () > 0) {int numbytes = Inputstream.read (Readbuffe                R); } SYSTEM.OUt.print (New String (Readbuffer));            } catch (IOException e) {System.out.println (e);}        Break }    }}


2, Jserialcomm

No need to add DLL to the system on RXTX basis

Https://github.com/Fazecast/jSerialComm/wiki

3, JSSC

Google's DLLs that don't need to rely on the system

Https://code.google.com/archive/p/java-simple-serial-connector/downloads


Java Read COM port

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.