Java RXTX串口可以发送数据,但接收不到数据。
刚接触串口,写了以下代码。利用软件虚拟了两个串口,运行以下代码时,可以发送数据,却不接收不到数据,多次测试,发现监听(serialEvent)一直进不去,求各位大大指教。
以下是虚拟端口软件。(从图片中可以看出发送和接收的字节数)

package com.red.test;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TooManyListenersException;
public class Demo implements SerialPortEventListener{
protected static CommPortIdentifier portid = null; //通讯端口标识符
protected static SerialPort comPort = null; //串行端口
protected int BAUD = 9600; //波特率
protected int DATABITS = SerialPort.DATABITS_8;; //数据位
protected int STOPBITS = SerialPort.STOPBITS_1; //停止位
protected int PARITY = SerialPort.PARITY_NONE; //奇偶检验
private static OutputStream oStream; //输出流
private static InputStream iStream; //输入流
StringBuilder buf = new StringBuilder(128);
public static void main(String[] args) {
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
Demo test = new Demo();
// 查看所有串口
test.listPortChoices();
//设置串口号
test.setSerialPortNumber();
}},3000,3000);
}
/**
* 读取所有串口名字
*/
private void listPortChoices() {
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
// iterate through the ports.
while (en.hasMoreElements()) {
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
}
/**
* 设置串口号
* @param Port
* @return
*/
private void setSerialPortNumber() {
String osName = null;
String osname = System.getProperty("os.name", "").toLowerCase();
if (osname.startsWith("windows")) {
// windows
osName = "COM1";
} else if (osname.startsWith("linux")) {
// linux
osName = "/dev/ttyS1";
}
try {
portid = CommPortIdentifier.getPortIdentifier(osName);
// portid = CommPortIdentifier.getPortIdentifier(Port);
if(portid.isCurrentlyOwned()){
System.out.println("端口在使用");
}else{
comPort = (SerialPort) portid.open(this.getClass().getName(), 1000);
}
} catch (PortInUseException e) {
System.out.println("端口被占用");
e.printStackTrace();
} catch (NoSuchPortException e) {
System.out.println("端口不存在");
e.printStackTrace();
}
try {
iStream = comPort.getInputStream(); //从COM1获取数据
oStream = comPort.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
comPort.addEventListener(this); //给当前串口增加一个监听器
comPort.notifyOnDataAvailable(true); //当有数据是通知
} catch (TooManyListenersException e) {
e.printStackTrace();
}
try {
//设置串口参数依次为(波特率,数据位,停止位,奇偶检验)
comPort.setSerialPortParams(this.BAUD, this.DATABITS, this.STOPBITS, this.PARITY);
} catch (UnsupportedCommOperationException e) {
System.out.println("端口操作命令不支持");
e.printStackTrace();
}
try {
//# testData
String testData = "1";
oStream.write(testData.getBytes());
iStream.close();
comPort.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@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:
break;
case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据,并且给串口返回数据
byte[] readBuffer = new byte[20];;
try {
char b;
while (iStream.available() > 0) {
b = (char)iStream.read(readBuffer);
buf.append(b);
}
} catch (IOException e) {
System.out.println(e.toString());
}
break;
}
}
}
以下是虚拟端口软件。(从图片中可以看出发送和接收的字节数)

...全文
相关推荐
Java串口读写程序RXTX
这些参数设置完毕后,程序可以开始监听串口接收数据,或者向指定的串口发送数据。这样的设计使得用户无需深入理解串口通信的底层细节,也能方便地进行串口操作。4.
java串口通信
如果缺失,Java程序在运行时会抛出“找不到方法”的异常。2.
java串口编程串口驱动
**串口读写**:通过Java串口库,可以设置数据接收和发送的事件处理器。例如,可以添加SerialPortEventListener监听串口事件,当接收到数据时触发dataReceived()方法。
javacomm20-win32.zip_j2me_java comm w_javacomm20 win32_javacomm2
发送数据:通过`SerialPort.getOutputStream()`获取输出流,然后使用`write()`方法发送数据到串口。5.
Java使用开源Rxtx实现串口通讯 串口开发
例如,`SerialPort.getInputStream()`返回一个输入流用于接收数据,`SerialPort.getOutputStream()`返回一个输出流用于发送数据。3.


