gpt4 book ai didi

java - 无法从 Java 中的 COM 端口读取串行数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:27 25 4
gpt4 key购买 nike

我正在尝试为我在 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是的,在尝试读取之前,设备已针对给定参数进行了配置。

新编辑 我正在使用适用于 Linux x64 平台的 Java SE 8 开发工具包,以及取自 http://www.java2s.com/Code/Jar/c/Downloadcomm20jar.htm 的 Java 通信 API。 .我的 IDE 是 Netbeans 8.1。

最佳答案

原来我必须使用 32 位版本的 Java 1.8 来访问串口。即使被迫(通过更改 netbeans.conf 文件中的“jdk_home”路径),我在 Linux 上的 IDE 也不会接受 64 位机器上的 32 位库,因此我在访问时遇到了问题串口。

我将我的操作系统切换到 Windows 7,但我仍然使用 64 位系统,因为 Windows 上的 Netbeans IDE 8.1 可以在 64 位和 32 位模式下使用相应的源库运行。所以我安装了适用于 Windows 的 JDK 1.8(32 位),然后在引用 Javax.comm API on 64-bit Windows 之后在 IDE 中运行我的代码。让它工作。至此,串口数据读取正常。

关于java - 无法从 Java 中的 COM 端口读取串行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37989747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com