Java教程

JAVA学习笔记20210423_串口

本文主要是介绍JAVA学习笔记20210423_串口,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

获取本地串口

尝试了两种jar包:comm和RXTXcomm
由于comm只能适配32位虚拟机,运行会报错且找不到串口:

D:\PractiseJava\SerialPort>java -classpath comm.jar; ListPorts
Error loading win32com: java.lang.UnsatisfiedLinkError: C:\Program Files\Java\jdk-15.0.2\bin\win32com.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
No serial port found

因此使用RXTXcomm包:RXTX下载

  • 拷贝 rxtxSerial.dll 到 JAVA_HOME\jre\bin目录中
  • 拷贝 rxtxParallel.dll 到 JAVA_HOME\jre\bin目录中
  • 拷贝 RXTXcomm.jar 到工作目录中
/**
* this is a serial port opration class
* @author XYM_
* @date 2021-4-23
* @version1.0
*/

import java.util.*;
import gnu.io.*;

public class ListPortsRXTX{
    public static void main(String[] args){
        CommPortIdentifier portId = null;
        Enumeration<?> allPorts = CommPortIdentifier.getPortIdentifiers();
        while(allPorts.hasMoreElements() == true){
            portId = (CommPortIdentifier)allPorts.nextElement();
            if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
                System.out.println("Serial Port: " + portId.getName());
            }
        }      
    }
}

运行结果:

D:\PractiseJava\SerialPort>java -classpath RXTXcomm.jar; ListPortsRXTX
Serial Port: COM1
Serial Port: COM2

设置参数

/**
* this is a serial port opration class
* @author XYM_
* @date 2021-4-23
* @version1.0
*/

import java.util.*;
import gnu.io.*;

public class OpenAndClose{
    public static void main(String[] args){

        // search and list all ports
        CommPortIdentifier portId = null;
        Enumeration<?> allPorts = CommPortIdentifier.getPortIdentifiers();
        while(allPorts.hasMoreElements() == true){
            portId = (CommPortIdentifier)allPorts.nextElement();
            if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
                System.out.println("Serial Port: " + portId.getName());
            }
        }

        // get the ports
        CommPortIdentifier com1 = null;
        CommPortIdentifier com2 = null;  
        try{
            com1 = CommPortIdentifier.getPortIdentifier("COM1");
            com2 = CommPortIdentifier.getPortIdentifier("COM2");
        }catch(NoSuchPortException e){
            e.printStackTrace();
        }

        // open the ports
        SerialPort serialCom1 = null;
        SerialPort serialCom2 = null;
        try{
            //firstparam: the owner of the port opened
            //secondparam: the time (ms) to wait when the port is in use
            serialCom1 = (SerialPort)com1.open("OpenAndClose",1000);
            serialCom2 = (SerialPort)com2.open("OpenAndClose",1000);
        }catch(PortInUseException e){
            e.printStackTrace();
        }

        // set the param of the ports
        try{
            serialCom1.setSerialPortParams(
                9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE
            );
            serialCom2.setSerialPortParams(
                9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE
            );
        }catch(UnsupportedCommOperationException e){
            e.printStackTrace();
        }

        // close the ports
        serialCom1.close();
        serialCom2.close();
    }
}

运行后可以在设备管理器中看到端口设置已被改写:
端口COM1属性

数据读写

ref:
串口数据的发送与监听读取
Java实现串口数据读写
使用Java实现串口通信

这篇关于JAVA学习笔记20210423_串口的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!