当前位置:首页 > 行业动态 > 正文

Java串口通信,如何获取并使用源码?

Java串口通信可以使用RXTX库。以下是一个简单的示例:,,“ 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;,,public class SerialExample {, public static void main(String[] args) {, try {, CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1");, CommPort commPort = portId.open("SerialExample", 2000);, SerialPort serialPort = (SerialPort) commPort;, serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);,, InputStream in = serialPort.getInputStream();, OutputStream out = serialPort.getOutputStream();,, out.write("Hello, Serial!".getBytes());,, byte[] buffer = new byte[1024];, int len = in.read(buffer);, System.out.println(new String(buffer, 0, len));,, in.close();, out.close();, commPort.close();, } catch (Exception e) {, e.printStackTrace();, }, },},“,,这段代码使用RXTX库实现了一个简单的Java串口通信示例,通过串口发送”Hello, Serial!”字符串,并接收串口返回的数据。
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
public class SerialTest implements SerialPortEventListener {
    SerialPort serialPort;
    private static final String PORT_NAMES[] = {
            "/dev/tty.usbserialA9007UX1", // Mac OS X
            "/dev/ttyUSB0", // Linux
            "COM3", // Windows
    };
    private BufferedReader input;
    private OutputStream output;
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 9600;
    public void initialize() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            for (String portName : PORT_NAMES) {
                if (currPortId.getName().equals(portName)) {
                    portId = currPortId;
                    break;
                }
            }
        }
        if (portId == null) {
            System.out.println("Could not find COM port.");
            return;
        }
        try {
            serialPort = (SerialPort) portId.open(this.getClass().getName(),
                    TIME_OUT);
            serialPort.setSerialPortParams(DATA_RATE,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }
    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                String inputLine = input.readLine();
                System.out.println(inputLine);
            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }
    }
    public static void main(String[] args) throws Exception {
        SerialTest main = new SerialTest();
        main.initialize();
        System.out.println("Started");
        Thread.sleep(100000);
        main.close();
        System.out.println("Stopped");
    }
}

这个示例程序首先尝试找到可用的串口,然后设置串口参数(波特率、数据位、停止位和奇偶校验),它创建一个输入流和一个输出流,以便从串口读取数据和向串口写入数据,它添加一个事件监听器,当有数据可用时触发serialEvent方法,在main方法中,程序初始化串口,等待100秒后关闭串口。

Java串口通信,如何获取并使用源码?  第1张

到此,以上就是小编对于“java 串口 源码”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。

0