In the near future to do a running and Android system, and testing instruments to carry out serial communication software, toss for several days, now sorted out a complete example of serial communication, referring to the RXTX-related packages:
Class Structure:
Spcomm.java: Communication subject
Spcommtest.java: Caller
1. Spcomm.java
Import Gnu.io.CommPortIdentifier;
Import gnu.io.PortInUseException;
Import Gnu.io.SerialPort;
Import gnu.io.SerialPortEvent;
Import Gnu.io.SerialPortEventListener;
Import gnu.io.UnsupportedCommOperationException;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.Enumeration;
Import java.util.TooManyListenersException; public class Spcomm implements Runnable, Serialporteventlistener {//detection system available communication port class private static Commportidentifier p
Ortid;
private static enumeration<commportidentifier> portlist;
The input output stream is public static InputStream InputStream;
public static OutputStream OutputStream;
RS-232 the serial port public static SerialPort SerialPort;
public static Thread Commthread;
Initializes the serial port public static void Init () {portlist = Commportidentifier.getportidentifiers ();
while (Portlist.hasmoreelements ()) {Portid = (commportidentifier) portlist.nextelement (); if (portid.getporttype () = = commportidentifier.port_serial) {
if (Portid.getname (). Equals ("COM8")) {try {SerialPort = (serialPort) portid.open ("Serialport-test", 2000
);
Serialport.addeventlistener (New Spcomm ());
Serialport.notifyondataavailable (TRUE); /* Set Serial communication parameters * * Serialport.setserialportparams (19200,serialport.databits_8, serialport.stopbits_1,serialport.parity
_none);
OutputStream = Serialport.getoutputstream ();
InputStream = Serialport.getinputstream ();
catch (Portinuseexception e) {e.printstacktrace ();
catch (Toomanylistenersexception e) {e.printstacktrace ();
catch (Unsupportedcommoperationexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace (); /** * Implements the method in interface Serialporteventlistener to read data received from the string/public void serialevent (serialportevent ev
ENT) {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://get to the serial port return information int newdata = 0;
int i = 0;
do{try{NewData = Inputstream.read ();
Processing of data, omitting ... i++;
if (i = = 24) {//According to actual demand, set the end condition at the appropriate time NewData =-1;
}}catch (IOException e) {return;
} while (NewData!=-1);
Serialport.close ()//Here must use the close () method to shut down the serial port, releasing the resource break;
Default:break; Send a message to the serial port method public static void Sendmsg () {String msg = "xxxxxxxxxxxxxx";//the command to be sent try {Outputstream.wri
Te (Hexstringtobytes (msg));
catch (IOException e) {e.printstacktrace ();
} public void Run () {init ();
Sendmsg (); }
}
2. Spcommtest.java
public class Spcommtest {public
static void Main (string[] args) {
thread spcommthread = new Thread (new Spcomm ()); C2/>spcommthread.setname ("Thread-mythread");
Spcommthread.start ();
}
You can run Spcomm.java.