如何在java中使用jssc从serialport读取数据?
我使用jssc库从串行端口读写数据如何在java中使用jssc从serialport读取数据?,java,serial-port,jssc,Java,Serial Port,Jssc,我使用jssc库从串行端口读写数据 package serial; import jssc.*; public class Serial { public static void main(String[] args) { String[] portNames = null; portNames = SerialPortList.getPortNames(); for (String string : portNames) { System.out.
package serial;
import jssc.*;
public class Serial {
public static void main(String[] args) {
String[] portNames = null;
portNames = SerialPortList.getPortNames();
for (String string : portNames) {
System.out.println(string);
}
if (portNames.length == 0) {
System.out.println("There are no serial-ports");
} else {
SerialPort serialPort = new SerialPort("com2");
try {
serialPort.openPort();
serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
PortReader portReader = new PortReader(serialPort);
serialPort.addEventListener(portReader, SerialPort.MASK_RXCHAR);
serialPort.writeString("S");
serialPort.closePort();
} catch (Exception e) {
System.out.println("There are an error on writing string to port т: " + e);
}
}
}
}
package serial;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class PortReader implements SerialPortEventListener {
SerialPort serialPort;
public PortReader(SerialPort serialPort) {
this.serialPort = serialPort;
}
@Override
public void serialEvent(SerialPortEvent event) {
System.out.println("started");
if (event.isRXCHAR() && event.getEventValue() > 0) {
try {
String receivedData = serialPort.readString(event.getEventValue());
System.out.println("Received response: " + receivedData);
} catch (SerialPortException ex) {
System.out.println("Error in receiving string from COM-port: " + ex);
}
}
}
}
我使用虚拟串行端口,该代码将数据写入串行端口。我确信数据正确写入串行端口
但我无法从串行端口读取数据。发生的情况是您正在关闭端口:
package serial;
import jssc.*;
public class Serial {
public static void main(String[] args) {
String[] portNames = null;
portNames = SerialPortList.getPortNames();
for (String string : portNames) {
System.out.println(string);
}
if (portNames.length == 0) {
System.out.println("There are no serial-ports");
} else {
SerialPort serialPort = new SerialPort("com2");
try {
serialPort.openPort();
serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
PortReader portReader = new PortReader(serialPort);
serialPort.addEventListener(portReader, SerialPort.MASK_RXCHAR);
serialPort.writeString("S");
serialPort.closePort();
} catch (Exception e) {
System.out.println("There are an error on writing string to port т: " + e);
}
}
}
}
package serial;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class PortReader implements SerialPortEventListener {
SerialPort serialPort;
public PortReader(SerialPort serialPort) {
this.serialPort = serialPort;
}
@Override
public void serialEvent(SerialPortEvent event) {
System.out.println("started");
if (event.isRXCHAR() && event.getEventValue() > 0) {
try {
String receivedData = serialPort.readString(event.getEventValue());
System.out.println("Received response: " + receivedData);
} catch (SerialPortException ex) {
System.out.println("Error in receiving string from COM-port: " + ex);
}
}
}
}
serialPort.addEventListener(portReader,serialPort.MASK_RXCHAR)
serialPort.writeString(“S”);
serialPort.closePort()//