【JAVA】读取windows的串口信息

发布于:2025-05-01 ⋅ 阅读:(22) ⋅ 点赞:(0)
  1. 首先去https://repo1.maven.org/maven2/下载rxtx的jar包,比如下载rxtx-2.1.7.jar放在项目的lib目录中
  2. 去这里https://files-cdn.cnblogs.com/files/blogs/666773/rxtx-win-x64.rar下载RXTXcomm.jar(放在项目的lib中)、rxtxParallel.dll和rxtxSerial.dll(放在项目的根目录中)
  3. 如下代码就是获取指定串口的数据
package sdf;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.IOException;
import java.io.InputStream;

public class SerialPortReader implements SerialPortEventListener {
    private SerialPort serialPort;
    private InputStream inputStream;

    public static void main(String[] args) {
        SerialPortReader reader = new SerialPortReader();
        reader.connect("COM1");  // 修改为实际的串口名称
        reader.readData();
    }

    public void connect(String portName) {
        try {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
            if (portIdentifier.isCurrentlyOwned()) {
                System.out.println("Port is currently in use.");
            } else {
            	//通过portIdentifier对象打开物理串口,"SerialPortReader"是给连接起的名称标识,2000表示打开端口的超时时间(单位:毫秒)
                serialPort = (SerialPort) portIdentifier.open("SerialPortReader", 2000);
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                inputStream = serialPort.getInputStream();
                serialPort.addEventListener(this);
                serialPort.notifyOnDataAvailable(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void readData() {
        try {
            while (true) {
                // 这里可以添加一些逻辑来处理读取到的数据
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                byte[] buffer = new byte[inputStream.available()];
                inputStream.read(buffer);
                String data = new String(buffer);
                System.out.println("Received data: " + data);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  1. 获取所有端口代码
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.util.Enumeration;

public class SerialPortReader {
    public static void main(String[] args) {
        // 列出所有可用串口
        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                System.out.println(portId.getName());
            }
        }

        // 打开指定串口
        String portName = "COM1";  // 修改为实际的串口名称
        try {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
            if (portIdentifier.isCurrentlyOwned()) {
                System.out.println("Port is currently in use.");
            } else {
                // 打开串口,参数分别为应用程序名称、超时时间
                SerialPort serialPort = (SerialPort) portIdentifier.open("SerialPortReader", 2000);
                // 设置串口参数,波特率、数据位、停止位、奇偶校验
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到