23,125
社区成员
发帖
与我相关
我的任务
分享
public SerialPort openPort() throws SerialPortParameterFailure, NotASerialPort, NoSuchPort, PortInUse {
try {
//通过端口名识别端口
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(m_comPortNumString);
//打开端口,并给端口名字和一个timeout(打开操作的超时时间)
CommPort commPort = portIdentifier.open(m_comPortNumString, 2000);
//判断是不是串口
if (commPort instanceof SerialPort) {
m_serialPort = (SerialPort) commPort;
try {
//设置一下串口的波特率等参数
m_serialPort.setSerialPortParams(m_bound, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
throw new SerialPortParameterFailure();
}
addListener(new SerialListener());
System.out.println("Open " + m_comPortNumString + " sucessfully !");
return m_serialPort;
}
else {
//不是串口
throw new NotASerialPort();
}
} catch (NoSuchPortException e1) {
throw new NoSuchPort();
} catch (PortInUseException e2) {
System.out.println("err:port In Use");
throw new PortInUse();
}
}
public void sendToPort( byte[] order) {//throws SendDataToSerialPortFailure, SerialPortOutputStreamCloseFailure {
OutputStream out = null;
try {
out = m_serialPort.getOutputStream();
out.write(order);
out.flush();
} catch (IOException e) {
//TODO throw new SendDataToSerialPortFailure();
System.out.println("err:SendDataToSerialPort Failure");
} finally {
try {
if (out != null) {
out.close();
out = null;
}
} catch (IOException e) {
//TODO throw new SerialPortOutputStreamCloseFailure();
}
}
}
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f8faa8e4733, pid=1991, tid=0x00007f8faa7db700
#
# JRE version: Java(TM) SE Runtime Environment (8.0_121-b13) (build 1.8.0_121-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [librxtxSerial.so+0x7733] Java_gnu_io_RXTXPort_nativeDrain+0xc3
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /root/out---/hs_err_pid1991.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Aborted (core dumped)
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j gnu.io.RXTXPort.nativeDrain(Z)Z+0
j gnu.io.RXTXPort$SerialOutputStream.flush()V+72
j serialPort.SerialTool.sendToPort([B)V+15
j serialPort.mainTest.heartthrobTimer()V+12
j serialPort.HeartthrobTimes.run()V+46
v ~StubRoutines::call_stub
out = m_serialPort.getOutputStream();
out.flush();