无法从 Java 中的 COM 端口读取串行数据
Unable to read serial data from COM port in Java
我正在尝试读取我在 Linux (Ubuntu 12.10) 上开发的 Java 应用程序的串行数据。这是我的代码:
try {
portId = CommPortIdentifier.getPortIdentifier("/dev/ttyS0");
SimpleRead reader = new SimpleRead();
} catch (Exception e) {
TimeStamp=new Date().toString();
System.out.println(TimeStamp+": ttyS0 "+portId);
System.out.println(TimeStamp+": msg1 - "+e);
}
SimpleRead 构造函数和其他所需代码:
public SimpleRead() {
try {
TimeStamp=new Date().toString();
serialPort = (SerialPort) portId.open("SimpleRead", 2000);
System.out.println(TimeStamp + ": " + portId.getName() + " opened for scanner input");
} catch (PortInUseException e) {
System.out.println(e);
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
System.out.println(e);
}
System.out.println("Input Stream set");
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println(e);
}
System.out.println("Event listener added");
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setDTR(false);
serialPort.setRTS(false);
} catch (UnsupportedCommOperationException e) {
System.out.println(e);
}
System.out.println("Parameters written");
readThread = new Thread(this);
readThread.start();
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
}
@Override
public void serialEvent(SerialPortEvent event) {
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:
System.out.println("Hello");
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
System.out.println("Hello");
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
if (numBytes>=16)
break;
}
//System.out.print(new String(readBuffer));
TimeStamp = new java.util.Date().toString();
System.out.println(TimeStamp + ": scanned input received:" + new String(readBuffer));
inputStream.close();
} catch (IOException e) {
System.out.println(e);
}
break;
}
}
出于某种原因,没有从端口读取串行数据,因为 serialEvent() 方法主体中的 "Hello" 未打印在输出屏幕中。请帮帮我!
编辑 代码取自:http://www.java-samples.com/showtutorial.php?tutorialid=11
是的,在尝试读取设备之前,设备已针对给定参数进行了配置。
新编辑 我正在使用 Java SE 8 开发工具包用于 Linux x64 平台,以及 Java 通信 API 摘自 http://www.java2s.com/Code/Jar/c/Downloadcomm20jar.htm。我的 IDE 是 Netbeans 8.1。
所以我必须使用32位版本的Java 1.8来访问串口。我在 Linux 上的 IDE 不会接受 64 位机器上的 32 位库,即使被迫(通过更改 netbeans.conf
文件中的 "jdk_home" 路径)也是如此,因此我在访问串行端口时遇到问题。
我将操作系统切换到 Windows 7,尽管我仍然使用 64 位系统,因为 Windows 上的 Netbeans IDE 8.1 可以 运行在 64 位和 32 位模式下使用各自的源库。所以我为 Windows 安装了 JDK 1.8(32 位),然后 运行 我在 IDE 中的代码,在参考 Javax.comm API on 64-bit Windows 让它工作之后.至此,串口数据读取正常
我正在尝试读取我在 Linux (Ubuntu 12.10) 上开发的 Java 应用程序的串行数据。这是我的代码:
try {
portId = CommPortIdentifier.getPortIdentifier("/dev/ttyS0");
SimpleRead reader = new SimpleRead();
} catch (Exception e) {
TimeStamp=new Date().toString();
System.out.println(TimeStamp+": ttyS0 "+portId);
System.out.println(TimeStamp+": msg1 - "+e);
}
SimpleRead 构造函数和其他所需代码:
public SimpleRead() {
try {
TimeStamp=new Date().toString();
serialPort = (SerialPort) portId.open("SimpleRead", 2000);
System.out.println(TimeStamp + ": " + portId.getName() + " opened for scanner input");
} catch (PortInUseException e) {
System.out.println(e);
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
System.out.println(e);
}
System.out.println("Input Stream set");
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println(e);
}
System.out.println("Event listener added");
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setDTR(false);
serialPort.setRTS(false);
} catch (UnsupportedCommOperationException e) {
System.out.println(e);
}
System.out.println("Parameters written");
readThread = new Thread(this);
readThread.start();
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
}
@Override
public void serialEvent(SerialPortEvent event) {
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:
System.out.println("Hello");
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
System.out.println("Hello");
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
if (numBytes>=16)
break;
}
//System.out.print(new String(readBuffer));
TimeStamp = new java.util.Date().toString();
System.out.println(TimeStamp + ": scanned input received:" + new String(readBuffer));
inputStream.close();
} catch (IOException e) {
System.out.println(e);
}
break;
}
}
出于某种原因,没有从端口读取串行数据,因为 serialEvent() 方法主体中的 "Hello" 未打印在输出屏幕中。请帮帮我!
编辑 代码取自:http://www.java-samples.com/showtutorial.php?tutorialid=11 是的,在尝试读取设备之前,设备已针对给定参数进行了配置。
新编辑 我正在使用 Java SE 8 开发工具包用于 Linux x64 平台,以及 Java 通信 API 摘自 http://www.java2s.com/Code/Jar/c/Downloadcomm20jar.htm。我的 IDE 是 Netbeans 8.1。
所以我必须使用32位版本的Java 1.8来访问串口。我在 Linux 上的 IDE 不会接受 64 位机器上的 32 位库,即使被迫(通过更改 netbeans.conf
文件中的 "jdk_home" 路径)也是如此,因此我在访问串行端口时遇到问题。
我将操作系统切换到 Windows 7,尽管我仍然使用 64 位系统,因为 Windows 上的 Netbeans IDE 8.1 可以 运行在 64 位和 32 位模式下使用各自的源库。所以我为 Windows 安装了 JDK 1.8(32 位),然后 运行 我在 IDE 中的代码,在参考 Javax.comm API on 64-bit Windows 让它工作之后.至此,串口数据读取正常