英文:
Java Serial Communication -> Problem with EventListener
问题
以下是您要翻译的内容:
我正在尝试使用Java串行通信从串行设备中读取测量值。任务是向设备发送代表数字9的ASCII码(十进制为57),然后设备将返回当前测量到的值。首先,我想确保能够正确地发送回一些值。到目前为止,连接是正常的,当我将getListeningEvents()方法更改为检查SerialPort.LISTENING_EVENT_DATA_WRITTEN时,它可以工作并显示出"Reading possible"这个短语,但在这种情况下,这只表明数据已传输。
所以数字被正确地发送了,但我无法获得答案,程序在打印"Written"后被卡住了。
在许多示例中,我看到了SerialPort类的notifyOnDataAvailable()方法,但我在文档中找不到它了,而且我不确定是否需要使用其他方法来初始化监听器。所以我的问题是,我的程序有什么问题,特别是我的EventListener,它为什么不能接收或识别返回的值?
以下是您的代码部分:
public class ConnectionNew implements SerialPortDataListener {
private SerialPort serialPort = null;
private java.io.OutputStream output = null;
private InputStream input = null;
private SerialPort[] ports = null;
public static void main(String[] args) {
ConnectionNew connect = new ConnectionNew();
connect.connectPort();
connect.initIOStream();
connect.initListener();
connect.writeData();
}
public void connectPort() {
ports = SerialPort.getCommPorts();
System.out.println("Select a port:");
int i = 1;
for (SerialPort port : ports) {
System.out.println(i++ + ": " + port.getSystemPortName());
}
Scanner s = new Scanner(System.in);
int chosenPort = s.nextInt();
serialPort = ports[chosenPort - 1];
if (serialPort.openPort()) {
System.out.println("Port opened successfully");
} else {
System.out.println("Unable to open the port");
return;
}
}
public boolean initIOStream() {
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
System.out.println("Streams Connected");
return true;
}
public void initListener() {
serialPort.addDataListener(this);
}
public void writeData() {
try {
output.write(57);
output.flush();
System.out.println("Written");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
}
@Override
public void serialEvent(SerialPortEvent arg0) {
System.out.println("Reading possible");
}
}
英文:
i'm trying to use Java Serial Communication to read measured values from a serial device. The task is to send the ASCII Code for 9 (57 in decimal) to the device and it will return the current value that is measured. In first place, I want to make sure that some value is correctly send back. So far, the connection works and when i changed the getListeningEvents() method to check for SerialPort.LISTENING_EVENT_DATA_WRITTEN it worked and showed me my phrase "Reading possible", but in this case this only shows that data got transferred.
So the number is send correctly, but I can't get an answer and the program gets stuck after printing "Written".
In many examples i saw the method notifyOnDataAvailable() for the SerialPort class, but i can't find it in the documentation anymore and i'm not sure if i have to use any other methods to initialize the listener. So my question is, what is wrong about my program, especially my EventListener, that it can't receive or identify the returned value?
Here is my code:
public class ConnectionNew implements SerialPortDataListener {
private SerialPort serialPort = null;
private java.io.OutputStream output = null;
private InputStream input = null;
private SerialPort [] ports = null;
public static void main(String[] args) {
ConnectionNew connect = new ConnectionNew();
connect.connectPort();
connect.initIOStream();
connect.initListener();
connect.writeData();
}
public void connectPort() {
ports = SerialPort.getCommPorts();
System.out.println("Select a port: ");
int i = 1;
for (SerialPort port : ports) {
System.out.println(i++ + ": " + port.getSystemPortName());
}
Scanner s = new Scanner(System.in);
int chosenPort = s.nextInt();
serialPort = ports[chosenPort - 1];
if (serialPort.openPort()) {
System.out.println("Port opened successfully");
} else {
System.out.println("Unable to open the port");
return;
}
}
public boolean initIOStream(){
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
System.out.println("Streams Connected");
return true;
}
public void initListener() {
serialPort.addDataListener(this);
}
public void writeData(){
try {
output.write(57);
output.flush();
System.out.println("Written");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public int getListeningEvents() {
return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
}
@Override
public void serialEvent(SerialPortEvent arg0) {
System.out.println("Reading possible");
}
}
I'm very happy about any hints, thanks in advance!!
答案1
得分: 1
你需要从事件中获取数据:
public void serialEvent(SerialPortEvent e) {
byte[] data = e.getReceivedData();
// ...
}
英文:
You need to get the data from the event:
public void serialEvent(SerialPortEvent e) {
byte[] data = e.getReceivedData();
// ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论