arduino 与java通信_用java按照教程例子写了个串口通讯的工具测试,但是接受不到...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

问题如题了

能向板子发送数据,板子也是正常的有向串口通信

但是就是自己写的工具这边接收不到

arduino自带的串口监视器可以正常用

贴出代码麻烦大佬们给瞅瞅

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;

import gnu.io.SerialPortEvent;

import gnu.io.SerialPortEventListener;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

public class FindPort implements SerialPortEventListener {

String result = "结果: ";

CommPortIdentifier portIdentifier = null;

Enumeration commPortIdentifierEnumeration = null;

SerialPort serialPort = null;

InputStream inputStream = null;

OutputStream outputStream = null;

public static void main(String[] args) {

FindPort findPort = new FindPort();

findPort.openPort();

findPort.readDataFromPort();

findPort.closePort();

System.out.println(findPort.result);

}

public void openPort() {

commPortIdentifierEnumeration = CommPortIdentifier.getPortIdentifiers();

while (commPortIdentifierEnumeration.hasMoreElements()) {

portIdentifier = commPortIdentifierEnumeration.nextElement();

if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {

System.out.println("发现串口 " + portIdentifier.getName());

try {

serialPort = (SerialPort) portIdentifier.open(portIdentifier.getName(), 2000);

serialPort.addEventListener(this);

serialPort.notifyOnDataAvailable(true);

serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

outputStream = serialPort.getOutputStream();

outputStream.write("R".getBytes());

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (outputStream != null) {

outputStream.flush();

outputStream.close();

outputStream = null;

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

public void serialEvent(SerialPortEvent serialPortEvent) {

String message = "";

switch (serialPortEvent.getEventType()) {

case SerialPortEvent.BI: //通讯中断

message = "Error: BI";

case SerialPortEvent.OE: //溢位错误

message = "Error: OE";

case SerialPortEvent.FE: //帧错误

message = "Error: FE";

case SerialPortEvent.PE: //奇偶校验错误

message = "Error: PE";

case SerialPortEvent.CD: //载波检测

message = "Error: CD";

case SerialPortEvent.CTS: //清除发送

message = "Error: CTS";

case SerialPortEvent.DSR: //数据设备准备好

message = "Error: DSR";

case SerialPortEvent.RI: //响铃侦测

message = "Error: RI";

case SerialPortEvent.OUTPUT_BUFFER_EMPTY: //输出缓冲区已清空

message = "Error: OUTPUT_BUFFER_EMPTY";

System.out.println(message);

break;

case SerialPortEvent.DATA_AVAILABLE: {

readDataFromPort();

}

break;

default:

break;

}

}

public void readDataFromPort() {

byte[] bytes = new byte[1024];

try {

if (serialPort != null) {

inputStream = serialPort.getInputStream();

inputStream.read();

int len = 0;

while ((len = inputStream.read(bytes)) != -1) {

result += new String(bytes, 0, len);

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (inputStream != null) {

outputStream.flush();

outputStream.close();

outputStream = null;

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

public void closePort() {

if (serialPort != null) {

serialPort.notifyOnDataAvailable(false);

serialPort.removeEventListener();

if (inputStream != null) {

try {

inputStream.close();

inputStream = null;

} catch (IOException e) {

e.printStackTrace();

}

}

if (outputStream != null) {

try {

outputStream.flush();

outputStream.close();

outputStream = null;

} catch (Exception e) {

e.printStackTrace();

}

}

serialPort.close();

serialPort = null;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值