Java

Java控制台怎么设置串口通信

小亿
54
2023-12-07 00:43:17
栏目: 编程语言

在Java中,可以使用RXTXcomm库来实现串口通信。下面是一个简单的示例:

  1. 首先,你需要下载和安装RXTXcomm库。你可以在 https://github.com/rxtx/rxtx 找到最新版本的库。

  2. 导入RXTXcomm库到你的Java项目中。

  3. 创建一个新的Java类并导入所需的类和包:

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
  1. 在类中定义串口通信的方法:
public class SerialCommunication {
    private InputStream inputStream;
    private OutputStream outputStream;

    public void connect(String portName) throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned()) {
            System.out.println("Error: Port is currently in use");
        } else {
            CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

            if (commPort instanceof SerialPort) {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                inputStream = serialPort.getInputStream();
                outputStream = serialPort.getOutputStream();

                (new Thread(new SerialReader(inputStream))).start();
                (new Thread(new SerialWriter(outputStream))).start();

            } else {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }
    }

    public static class SerialReader implements Runnable {
        private InputStream inputStream;

        public SerialReader(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        public void run() {
            byte[] buffer = new byte[1024];
            int len = -1;
            try {
                while ((len = this.inputStream.read(buffer)) > -1) {
                    System.out.print(new String(buffer, 0, len));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static class SerialWriter implements Runnable {
        private OutputStream outputStream;

        public SerialWriter(OutputStream outputStream) {
            this.outputStream = outputStream;
        }

        public void run() {
            try {
                int c = 0;
                while ((c = System.in.read()) > -1) {
                    this.outputStream.write(c);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            SerialCommunication serial = new SerialCommunication();
            serial.connect("/dev/ttyUSB0"); // Replace with your serial port name
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. 运行代码,将"/dev/ttyUSB0"替换为你的串口名称,然后通过控制台输入和输出与串口进行通信。

请注意,此示例假设你的系统上已经安装并配置了正确的串口驱动程序。

0
看了该问题的人还看了