java版windwos串口通信代码

本文介绍如何使用Java实现串口通信,包括所需jar包及dll文件的下载链接、配置步骤和核心代码示例。通过定义串口参数配置实体类和串口管理类,实现了串口的打开、设置参数及发送指令等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、通信前需要的jar包括comm.jar、RXTXcomm.jar、rxtxSerial.dll,这三个包已放到云盘,请自行下载

https://pan.baidu.com/s/1yjlsD2yj_EK0zCjZdXM7Sg

2、需要把rxtxSerial.dll放到jdk安装目录jre\bin\ 下面

3、开始上代码,首先定义串口参数配置实体

//串口参数配置类

public class CommPort {
    /**
     * 串口名称,如:COM1、COM2
     */
    private String commPortName="";           
    
    /**
     * 串口所有者名称,一般为应用程序的名称
     */
    private String ownerName;                 
    
    /**
     * 波特率
     */
    private String baudRate="";               
    
    /**
     * 数据位
     */
    private String dataBit="";                
    
    /**
     * 校验位
     */
    private String partityBit="";             
    
    /**
     * 停止位
     */
    private String stopBit="";                
    
    public String getCommPortName() {
        return commPortName;
    }

    public void setCommPortName(String commPortName) {
        this.commPortName = commPortName;
    }

    public String getOwnerName() {
        return ownerName;
    }

    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    public String getBaudRate() {
        return baudRate;
    }

    public void setBaudRate(String baudRate) {
        this.baudRate = baudRate;
    }

    public String getDataBit() {
        return dataBit;
    }

    public void setDataBit(String dataBit) {
        this.dataBit = dataBit;
    }

    public String getPartityBit() {
        return partityBit;
    }

    public void setPartityBit(String partityBit) {
        this.partityBit = partityBit;
    }

    public String getStopBit() {
        return stopBit;
    }

    public void setStopBit(String stopBit) {
        this.stopBit = stopBit;
    }
    
}

4、其次串口管理类

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Enumeration;

public class CommPortManager {
    /**
     * 串口定义
     */
    private CommPort commPort;         
    
    /**
     * 串口对象
     */
    private SerialPort serialPort;     
    
    /**
     * 字节输入
     */
    private InputStream in;               
    
    /**
     * 字节输出
     */
    private OutputStream out;          
    
    /**
     * 字符输入
     */
    private BufferedReader bufReader;  
    
    /**
     * 字符输出
     */
    private BufferedWriter bufWriter;  
    
    /**
     * 数据输入
     */
    private DataInputStream dataIn;    
    
    /**
     * 数据输出
     */
    private DataOutputStream dataOut;  
    
    /**
     * 串口是否在使用
     */
    private boolean isUse;             
    
    public CommPortManager(CommPort commPort){
        this.commPort = commPort;
    }
    
    /**
     * 打开串口
     * @throws Exception
     */
    public void open() throws Exception{
        CommPortIdentifier commPortId = CommPortIdentifier.getPortIdentifier(commPort.getCommPortName());
        
        // 第一个参数:通常设置为你的应用程序的名字;第二个参数:开启端口超时的毫秒数
        serialPort = (SerialPort)commPortId.open(commPort.getOwnerName(), 5000);
        serialPort.setOutputBufferSize(2048);
        
      //  in = serialPort.getInputStream();
        out = serialPort.getOutputStream();        
        
    //    bufReader = new BufferedReader(new InputStreamReader(in, "Unicode"));
        bufWriter = new BufferedWriter(new OutputStreamWriter(out));
      //  dataIn = new DataInputStream(in);
        dataOut = new DataOutputStream(out);
       // String ordery="0x5B0x4B0x5D";
        /*dataOut.write(ordery.getBytes());
        dataOut.close();*/
        // 设置串口参数
        serialPort.setSerialPortParams(Integer.valueOf(commPort.getBaudRate()), Integer.valueOf(commPort.getDataBit()), 
                Integer.valueOf(commPort.getStopBit()), Integer.valueOf(commPort.getPartityBit())); 
        String order="[kD04][kU04]";//指令,具体看自己的驱动指令
        dataOut.write(ordery.getBytes());  
        dataOut.flush();  
        dataOut.close(); 
    }
    
    /**
     * 判断串口是否可用
     */
    public boolean commPortEnable(){    
        boolean result = false;
        Enumeration en = CommPortIdentifier.getPortIdentifiers();
        while (en.hasMoreElements()) {             
            CommPortIdentifier portId = (CommPortIdentifier) en.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL
                    && portId.getName().equalsIgnoreCase(commPort.getCommPortName())) {
                result = true;
            }
        }
        return result;
    }

    /**
     * 设置串口接收超时时间
     * @param timeout 超时时间,单位秒
     * @throws Exception
     */
    public void setReceiveTimeout(int timeout) throws Exception{
        getSerialPort().enableReceiveTimeout(timeout*1000);
    }
    
    public CommPort getCommPort() {
        return commPort;
    }

    public SerialPort getSerialPort() {
        return serialPort;
    }

    public InputStream getIn() {
        return in;
    }

    public OutputStream getOut() {
        return out;
    }

    public BufferedReader getBufReader() {
        return bufReader;
    }

    public BufferedWriter getBufWriter() {
        return bufWriter;
    }

    public DataInputStream getDataIn() {
        return dataIn;
    }

    public DataOutputStream getDataOut() {
        return dataOut;
    }

    public boolean isUse() {
        return isUse;
    }

    public void setUse(boolean isUse) {
        this.isUse = isUse;
    }
    public static void main(String[] args) throws Exception {
        CommPort prot=new CommPort();
        prot.setCommPortName("COM6");
        prot.setBaudRate("57600");
        prot.setDataBit("8");
        prot.setStopBit("1");
        prot.setPartityBit("1");
        prot.setOwnerName("USB-SERIAL CH340 (COM6)");
        CommPortManager port=new CommPortManager(prot);

      //打开端口,进行发送指令
        port.open();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值