Java RXTX串口可以发送数据,但接收不到数据。

english1255 english1255 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
刚接触串口,写了以下代码。利用软件虚拟了两个串口,运行以下代码时,可以发送数据,却不接收不到数据,多次测试,发现监听(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;
}
}
}



以下是虚拟端口软件。(从图片中可以看出发送和接收的字节数)



...全文
评论 9
收藏 已收藏
分享
返回顶部
english1255
Java 51,407 成员 86,126 内容