Home > Back-end >  Java RXTX realize serial communication. Can send the order, but unable to get to a serial port retur
Java RXTX realize serial communication. Can send the order, but unable to get to a serial port retur

Time:09-19

The code is as follows:


Package com. Boco. Util.
The import gnu.io.Com mPortIdentifier;
Import the gnu. IO. NoSuchPortException;
Import the gnu. IO. PortInUseException;
Import the gnu. IO. SerialPort;
Import the gnu. IO. SerialPortEvent;
Import the gnu. IO. SerialPortEventListener;

import java.io.BufferedReader;
Import the Java. IO. ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
Import the Java. Nio. CharBuffer;
import java.util.Random;


/* *
* a serial port operation implementation class
* @ author Anlw
*
*/
Public class Port implements SerialPortEventListener {
Private CommPortIdentifier portId;
Private SerialPort SerialPort;
Private OutputStreamWriter out;
Private InputStreamReader in;
Private String COMname;

Public String getCOMname () {
Return COMname;
}

Public void setCOMname (String mname) {
COMname=mname;
}

Public CommPortIdentifier getPortId () {
Return portId;
}

Public void setPortId (CommPortIdentifier portId) {
Enclosing portId=portId;
}

Public SerialPort getSerialPort () {
Return the serialPort;
}

Public void setSerialPort (SerialPort SerialPort) {
This. SerialPort=serialPort;
}

Public OutputStreamWriter getOut () {
Return the out;
}

Public void setOut OutputStreamWriter (out) {
This. Out=out;
}

Public InputStreamReader getIn () {
Return in;
}

Public void setIn InputStreamReader (in) {
This.=in in;
}

Public Boolean isused=true;

Public Boolean isIsused () {
Return isused;
}

Public void setIsused (Boolean isused) {
Enclosing isused=isused;
}


/* *

* constructor to open the com* @ param portName
* @ return
*/
The public Port (String portName) {
Try {
PortId=CommPortIdentifier. GetPortIdentifier (portName);
If (portId==null) {
System. The out. Println (" port is null ");
}
Try {
SerialPort=(serialPort) portId. Open (getrechargeablePassword (), 20000);
} the catch (PortInUseException e) {
System.gc();
System. Out.println (" COM ports occupied, please select the correct COM port!! ");
}
//here is to initialize the COM transmission parameters, such as transmission rate: 9600
Try {
SerialPort. SetSerialPortParams (9600, serialPort. DATABITS_8, serialPort. STOPBITS_1, serialPort. PARITY_NONE);
SetCOMname (portId getName ());
SetIsused (true);
} the catch (Exception e) {
System.gc();
}
//below is used for communication and the COM input, the output stream,
Try {
Out=new OutputStreamWriter (serialPort getOutputStream ());
//serialPort setRTS (true);
In=new InputStreamReader (serialPort getInputStream ());
////add event listeners to the serial port object,
//serialPort addEventListener (this);
////set triggering event when port with the available data, this setting is indispensable,
//serialPort notifyOnDataAvailable (true);

} the catch (Exception e) {
System.gc();
}


} the catch (NoSuchPortException e) {
System.gc();
System. Out.println (" no this COM port, please select the correct COM port!! ");
}
}

//get the password
Public static String getrechargeablePassword () {
Random random=new Random();
Char [] codeSequence={' A ', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' and 'J' and 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R' and 'S' and 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'c',
'b', 'd', 'f', 'e', 'g', 'h', 'j', 'I', 'l' and 'k', 'n', 'm', 'o', 'p', 'q', 'r', 's', 't', 'u', 'w',
'v'};
String strRand="";
for (int i=0; i <18; I++) {
StrRand=strRand + String. The valueOf (codeSequence [random. NextInt (59)]);
}
Return strRand;
}

/* *

* shut down the COM* @ return Boolean
*/
Public void the close () {
Try {
in.close();
out.close();
SerialPort. Close ();
System.gc();
SetIsused (false);
} the catch (Exception e) {
}

}

/* *
* to write a string to the serial port command
* @ param string command s
* @ throws the Exception Exception
*/
Public void writeln (String s) throws the Exception {
out.write(s);
Out. Write (' \ r ');
out.flush();

}

/* *
* read COM command returns a string
* @ return result string
* @ throws the Exception
*/
Public String read () {
Int n, I;
char c;
String answer="";
for (i=0; i <100; I++) {
Try {
While (in) ready ()) {
N=in the read ();
if (n !=1) {
C=(char) n;
Answer=answer + c;

Thread.sleep (1);
} the else
break;
}
if(!" ". The equals (answer)) {
System. The out. Println (answer);
}
Thread.sleep (100);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related