ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JAVA学习笔记20210423_串口

2021-04-23 11:32:28  阅读:227  来源: 互联网

标签:CommPortIdentifier JAVA allPorts SerialPort 20210423 串口 portId null


获取本地串口

尝试了两种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实现串口通信

标签:CommPortIdentifier,JAVA,allPorts,SerialPort,20210423,串口,portId,null
来源: https://blog.csdn.net/weixin_45308301/article/details/116015848

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有