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实现串口通信

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值