Home > Back-end >  Java access denied serial communication
Java access denied serial communication

Time:12-31

Recently do project will use a serial port communication, and communication, electronic scale for the first time contact, from the Internet to find some code to run, is running a few minutes (or less) is an error, error information below
Log error message:
 Java lang. NullPointerException 
The at bing. Controller. ChaunkouController. ReadFromPort (ChaunkouController. Java: 248)
The at bing. Controller. ChaunkouController $DataAvailableListener. SerialEvent (ChaunkouController. Java: 161)
At the gnu. IO. RXTXPort. Adds (RXTXPort. Java: 772)
At the gnu. IO. RXTXPort. EventLoop (Native Method)
At the gnu. IO. RXTXPort $MonitorThread. Run (RXTXPort. Java: 1641)





 private SerialPort mySerialport=null; 
Private static final ints BAUDRATE=9600;//baud rate, defaults to 9600
List List=new ArrayList<> (a);
Public static Dingdandao Dingdandao=new Dingdandao ();
XQcontroller XQcontroller=new XQcontroller ();

/* *
* detection and access to current equipment all available ports (here can include a USB ports and bluetooth)
*
* @ return returns a list containing the name of the all available ports (such as COM4, COM6, etc.)
* but will return to the list of output in order to view the
*, of course, also can through the 'device manager - port to view the available ports
*/
Public ArrayList FindPorts () {
//call the jar package getPortIdentifiers function, get the current enumeration of all the available port
Enumeration PortList.=CommPortIdentifier getPortIdentifiers ();
ArrayList PortNameList=new ArrayList (a);
//will be available port name added to the List and returns the List
While (portList. HasMoreElements ()) {
String portName=portList. NextElement (). The getName ();
PortNameList. Add (portName);
}
Return portNameList;
}

/* *
* through step on access to the port name to open a serial port and set the serial port parameters
*
* @ param portName port name
* @ param baudrate baud rate (baud rate consistent with electronic scale, generally is 9600, suggested as final macro constants in program beginning)
* @ return back to open the serial port, if not a serial port it returns null
When * @ throws PortInUseException port is already occupied an exception is thrown
*/
Public SerialPort openPort (String portName, int baudrate) throws PortInUseException {
Try {
//by port name recognition port
CommPortIdentifier portIdentifier=CommPortIdentifier. GetPortIdentifier (portName);
//open ports, and port name and a timeout (open operation timeout)
CommPort CommPort=portIdentifier. Open (portName, 2000);
//whether port is a serial port
If (commPort instanceof SerialPort) {
SerialPort SerialPort=(SerialPort commPort);
Try {
//set the baud rate of serial port parameters such as
//data bits: 8
//stop bit: 1
//check digit: None
SerialPort. SetSerialPortParams (baudrate, serialPort. DATABITS_8, serialPort STOPBITS_1, serialPort. PARITY_NONE);
} the catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
Return the serialPort;
}
{} the catch (NoSuchPortException e1)
E1. PrintStackTrace ();
}
return null;
}

/* *
* add data to the event listeners, to open the serial port communication interrupt listening
*
* @ param serialPort open serial port
* @ param listener listener
*/
Public void addListener (SerialPort SerialPort, DataAvailableListener listener) {
Try {
/* *
* add listeners to a serial port
* the addEventListener () function as the jars bring function
* function parameters of addEventListener listener must be SerialPortEventListener type
* so DataAvailableListener must implement the SerialPortEventListener interface
*/
SerialPort. AddEventListener (the listener);
//set the wake listening when data arrives at receiving threads
SerialPort. NotifyOnDataAvailable (true);
//set when wake interrupt communication interrupt thread
SerialPort. NotifyOnBreakInterrupt (true);
} the catch (TooManyListenersException e) {
e.printStackTrace();
{} catch NullPointerException (e)
e.printStackTrace();
}
}

/* *
* custom listener, realizing SerialPortEventListener interfaces defined in the jars, and overwrite the serialEvent method
*/
Public class DataAvailableListener implements SerialPortEventListener {
@ Override
@ FXML
Synchronized public void serialEvent (SerialPortEvent SerialPortEvent) {
/* *
* there are 10 types of events can be monitored
* here only two types of events for the response and handling
*/
The switch (serialPortEvent getEventType ()) {
Case SerialPortEvent. DATA_AVAILABLE://receive the data event
Byte [] data;
Try {
If (mySerialport==null) {
System. Out.println (" a serial port object is empty, listening failed!" );
} else {
//read serial data
Data=https://bbs.csdn.net/topics/readFromPort (mySerialport);

//ASCII array can be converted to the corresponding string
String text=new String (data);

//remove unnecessary characters
Text=text. ReplaceAll (" ", "");
Text=text. ReplaceAll (" \ r ", "");
Text=text. ReplaceAll (" \ n ", "");
Text=text. ReplaceAll (" \ t ", "");
If (text. The length () & gt; 0 {
//after will handle the weight of the information printed output
//textdao setText (text);
//zhongliang=text;
System. The out. Println (text);
Ckzhongliang. SetText (text);
//jiaoyizhongliang setText (text);
//weight. SetText (text);
}
}
} the catch (Exception e) {
System. The out. Println (e. oString ());
//read error display an error message after exit system

}
break;

Case SerialPortEvent. OUTPUT_BUFFER_EMPTY://2. The output buffer is empty
break;

Case SerialPortEvent. CTS://3. Removal of sent data
break;

Case SerialPortEvent. DSR://4. Ready to send data
break;

Case SerialPortEvent. RI://5. Ringing indicating
break;

Case SerialPortEvent. CD://6. Carrier detect
break;

Case SerialPortEvent. OE://7. Overflow error (overflow)
break;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related