Because the project needs to read the serial port and related operation under Linux.
The General Serial port operation (Linux) is written in C language, because the lower level, but also more secure, easier to write, but because the project is Java, it is necessary to use Java related libraries to implement.
What I'm using here is Rxtxcomm.jar.
Http://rxtx.qbang.org/wiki/index.php/Main_Page
Here are the authors on the various operating system configuration problems, because I use only in Linux, so I would like to talk about the Linux configuration problems.
First download Rxtxcomm
Install is a simple installation instructions, and then there is a configuration file under four operating systems and a jar package.
Under Linux
Here is a different configuration file for different Linux versions.
Then enter the $JAVA _home, configured as follows:
In Java_home/jre/lib, there is a configuration file, AMD64, this file may be different, and then find the configuration file corresponding to your system in Rxtxcomm. Then put the. So file inside the AMD64.
Then go into Java_home/jre/lib/ext and put the previous jar package in the folder, and the configuration is complete.
Then attach a simple example. View ports and output port information.
PackageInit;ImportGnu.io.CommPort;ImportGnu.io.CommPortIdentifier;ImportGnu.io.SerialPort;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream; Public classeee{StaticString defaultport = "/dev/ttys1";//Linux Public Static voidMain (string[] args) {listports (); Try { (Newtest ()). connect (Defaultport); } Catch(Exception e) {e.printstacktrace (); } } Publiceee () {Super(); } voidConnect (String portname)throwsException {commportidentifier portidentifier=Commportidentifier.getportidentifier (portname); if(portidentifier.iscurrentlyowned ()) {System.out.println ("Error:port is currently on use"); } Else{commport Commport= Portidentifier.open ( This. GetClass (). GetName (), 2000); if(CommportinstanceofSerialPort) {SerialPort SerialPort=(SerialPort) Commport; Serialport.setserialportparams (9600, Serialport.databits_8,serialport.stopbits_1,serialport.parity_none); InputStream in=Serialport.getinputstream (); InputStreamReader Reader=NewInputStreamReader (in); BufferedReader R=NewBufferedReader (reader); (NewThread (NewSerialreader (R)). Start (); } Else{System.out.println ("Error:only serial ports is handled by this example."); } } } /** */ Public Static classSerialreaderImplementsRunnable {BufferedReader in; PublicSerialreader (BufferedReader in) { This. in =In ; } Public voidrun () {Try{String line; while(line = In.readline ())! =NULL) {System.out.println (line); } } Catch(IOException e) {e.printstacktrace (); } } } /** */ Public Static classSerialwriterImplementsRunnable {outputstream out; PublicSerialwriter (OutputStream out) { This. Out =Out ; } Public voidrun () {Try { intc = 0; while((c = System.in.read ()) > 1 ) { This. Out.write (c); } } Catch(IOException e) {e.printstacktrace (); } } } Static voidListports () {System.out.println ("All ports!!! "); @SuppressWarnings ("Unchecked") Java.util.Enumeration<CommPortIdentifier> Portenum =commportidentifier.getportidentifiers (); while(Portenum.hasmoreelements ()) {Commportidentifier Portidentifier=portenum.nextelement (); System.out.println (Portidentifier.getname ()+ " - " +Getporttypename (Portidentifier.getporttype ())); } } StaticString Getporttypename (intPortType) { Switch(portType) { CaseCOMMPORTIDENTIFIER.PORT_I2C:return"I²c"; CaseCommportidentifier.port_parallel:return"Parallel"; CaseCommportidentifier.port_raw:return"Raw"; Casecommportidentifier.port_rs485:return"RS485"; Casecommportidentifier.port_serial:return"Serial"; default: return"Unknown type"; } } }
The configuration and use of open source RXTX library under Linux