串口例程

https://blog.csdn.net/kong_gu_you_lan/article/details/80589859  串口通信完整例程

http://fizzed.com/oss/rxtx-for-java  依赖包下载

一.串口连接成功并且串口驱动安装成功。

二.版本注意事项:

javax.comm开发中,由于win32com.dll只能工作在32位模式下,所以基本就是说串口通讯只支持jdk16~1.7~1.8的32位版本(已测试)
spring boot 2.0以上只能工作在jdk1.8以上,所以这次我是从jdk1.7-32bit转到jdk1.8-32bit
win32com.dll这个东西太老了,又没有64位的。网上说建议使用RxTx,支持Linux 32/64、Windows 32/64,这个晚点有空再研究。http://mfizz.com/oss/rxtx-for-java

资源下载:

文件清单(注意%JAVA_HOME%是jdk的路径,而非jre):

comm.jar(javax.comm的包,添加到%JAVA_HOME%/jre/lib/ext)
javax.comm.properties(javax.comm的配置,添加到%JAVA_HOME%/jre/lib)
win32com.dll(串口调试DLL,添加到JAVA_HOME%/bin)
vspd(虚拟串口调试工具,可以虚拟COM1和COM2,监听COM1并发送数据,COM2就可以收到,本质上是映射了COM1<->COM2相互通讯)
串口调试精灵,可以打开端口,设置一样的参数之后可以进行调试

三.1.简单串口读取

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPort;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class SimpleRead implements Runnable, SerialPortEventListener {

    static CommPortIdentifier commPortIdentifier;
    static Enumeration portList;
    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    public static void main(String[] args) {
        /*不带参数的getPortIdentifiers方法获得一个枚举对象,该对象又包含了系统中管理每个端口的CommPortIdentifier对象。
         *注意这里的端口不仅仅是指串口,也包括并口。这个方法还可以带参数。
         * getPortIdentifiers(CommPort)获得与已经被应用程序打开的端口相对应的CommPortIdentifier对象。
         * getPortIdentifier(String portName)获取指定端口名(比如“COM1”)的CommPortIdentifier对象。*/
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            commPortIdentifier = (CommPortIdentifier) portList.nextElement();
            if (commPortIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (commPortIdentifier.getName().equals("COM5")) {
                    SimpleRead readerRead = new SimpleRead();
                }
            }

        }
    }

    public SimpleRead() {
        System.out.println("检测到COM5了");
        try {
            /* open方法打开通讯端口,获得一个CommPort对象。
             * 它使程序独占端口。如果端口正被其他应用程序占用,将使用CommPortOwnershipListener事件机制,传递一个PORT_OWNERSHIP_REQUESTED事件。
             * 每个端口都关联一个InputStream和一个OutputStream。
             * 如果端口是用open方法打开的,那么任何的getInputStream都将返回相同的数据流对象,除非有close被调用。
             * 有两个参数,第一个为应用程序名;第二个参数是在端口打开时阻塞等待的毫秒数。*/
            serialPort = (SerialPort) commPortIdentifier.open(this.getClass()
                    .getName(), 2000);
        } catch (PortInUseException e) {
            System.out.println("端口正在使用");
        }

        try {
            /*获取端口的输入流对象*/
            inputStream = serialPort.getInputStream();
        } catch (IOException e2) {
        }

        try {
            /*注册一个SerialPortEventListener事件来监听串口事件*/
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e3) {
        }
        /*数据可用*/
        serialPort.notifyOnDataAvailable(true);
        try {
            /*设置串口初始化参数,依次是波特率,数据位,停止位和校验*/
            serialPort.setSerialPortParams(9600, serialPort.DATABITS_8,
                    serialPort.STOPBITS_1, serialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e4) {
        }
        readThread = new Thread(this);
        readThread.start();

    }
    //串口事件
    @Override
    public void serialEvent(SerialPortEvent event) {
        // TODO Auto-generated method stub
        switch (event.getEventType()) {
        case SerialPortEvent.BI:/* Break interrupt,通讯中断 */
        case SerialPortEvent.OE:/* Overrun error,溢位错误 */
        case SerialPortEvent.FE:/* Framing error,传帧错误 */
        case SerialPortEvent.PE:/* Parity error,校验错误 */
        case SerialPortEvent.CD:/* Carrier detect,载波检测 */
        case SerialPortEvent.CTS:/* Clear to send,清除发送 */
        case SerialPortEvent.DSR:/* Data set ready,数据设备就绪 */
        case SerialPortEvent.RI:/* Ring indicator,响铃指示 */
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*
                                                 * Output buffer is
                                                 * empty,输出缓冲区清空
                                                 */
            break;

        case SerialPortEvent.DATA_AVAILABLE:/*
                                             * Data available at the serial
                                             * port,端口有可用数据。读到缓冲数组,输出到终端
                                             */
            byte[] readBuffer = new byte[20];

            try {
                while (inputStream.available() > 0) {
                    //从输入流中读取一定数量的字节,存储到字节数组中,返回实际读取的字节数
                    int numBytes = inputStream.read(readBuffer);
                }
                System.out.print(new String(readBuffer));
            } catch (IOException e) {
            }
            break;
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
        }
    }

}

 2.完整串口读和写

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class SimpleRead implements Runnable, SerialPortEventListener {

    private String appName = "串口通讯测试";
    private int timeout = 2000;// open 端口时的等待时间,延迟时间(毫秒数)
    private int threadTime = 0;

    private CommPortIdentifier commPort;
    private SerialPort serialPort;
    private InputStream inputStream;
    private OutputStream outputStream;
    //当前接收COM口的数据
    public static String receiptDataString="";
    //当前已取的数组下标
    public static int nowDataIndex =0;
    //当前接收COM口的数据自动切割成StringList
    public static ArrayList<String> receiptDataList=new ArrayList<String>();

    /**
     * @方法名称 :listPort
     * @功能描述 :列出所有可用的串口
     * @返回值类型 :void
     */
    @SuppressWarnings("rawtypes")
    public void listPort() {
        CommPortIdentifier cpid;
        Enumeration en = CommPortIdentifier.getPortIdentifiers();//获得端口列表

        System.out.println("now to list all Port of this PC:" + en);

        while (en.hasMoreElements()) {
            cpid = (CommPortIdentifier) en.nextElement();
            if (cpid.getPortType() == CommPortIdentifier.PORT_SERIAL) {//判断是否串口
                System.out.println(cpid.getName() + ", " + cpid.getCurrentOwner());
            }
//          if (cpid.getPortType() == CommPortIdentifier.PORT_PARALLEL) {//判断是否并行口
//              System.out.println(cpid.getName() + ", " + cpid.getCurrentOwner());
//          }
        }
    }

    /**
     * @方法名称 :selectPort
     * @功能描述 :选择一个端口,比如:COM1
     * @返回值类型 :void
     * @param portName
     */
    @SuppressWarnings("rawtypes")
    public void selectPort(String portName) {

        this.commPort = null;
        CommPortIdentifier cpid;
        Enumeration en = CommPortIdentifier.getPortIdentifiers();

        while (en.hasMoreElements()) {
            cpid = (CommPortIdentifier) en.nextElement();
            if (cpid.getPortType() == CommPortIdentifier.PORT_SERIAL && cpid.getName().equals(portName)) {
                this.commPort = cpid;
                break;
            }
        }

        openPort();
    }

    /**
     * @方法名称 :openPort
     * @功能描述 :打开SerialPort
     * @返回值类型 :void
     */
    private void openPort() {
        if (commPort == null)
            log(String.format("无法找到串口!"));
        else {
            log("端口选择成功,当前端口:" + commPort.getName() + ",现在实例化 SerialPort:");

            try {
                serialPort = (SerialPort) commPort.open(appName, timeout);//打开端口
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                //设置参数
                log("实例 SerialPort 成功!");
            } catch (PortInUseException e) {
                throw new RuntimeException(String.format("端口'%1$s'正在使用中!", commPort.getName()));
            }catch (UnsupportedCommOperationException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @方法名称 :checkPort
     * @功能描述 :检查端口是否正确连接
     * @返回值类型 :void
     */
    public void checkPort() {
        if (commPort == null)
            throw new RuntimeException("没有选择端口,请使用 " + "selectPort(String portName) 方法选择端口");

        if (serialPort == null) {
            throw new RuntimeException("SerialPort 对象无效!");
        }
    }

    /**
     * @方法名称 :write
     * @功能描述 :向端口发送数据,请在调用此方法前 先选择端口,并确定SerialPort正常打开!
     * @返回值类型 :void
     * @param message
     */
    public void write(String message) {
        checkPort();

        try {
            outputStream = new BufferedOutputStream(serialPort.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException("获取端口的OutputStream出错:" + e.getMessage());
        }

        try {
            outputStream.write(message.getBytes());
            log("信息发送成功!");
        } catch (IOException e) {
            throw new RuntimeException("向端口发送信息时出错:" + e.getMessage());
        } finally {
            try {
                outputStream.close();
            } catch (Exception e) {
            }
        }
    }
    public void write(byte[]  message) {
        checkPort();

        try {
            outputStream = new BufferedOutputStream(serialPort.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException("获取端口的OutputStream出错:" + e.getMessage());
        }

        try {
            outputStream.write(message);
            log("信息发送成功!");
        } catch (IOException e) {
            throw new RuntimeException("向端口发送信息时出错:" + e.getMessage());
        } finally {
            try {
                outputStream.close();
            } catch (Exception e) {
            }
        }
    }
    /**
     * @方法名称 :startRead
     * @功能描述 :开始监听从端口中接收的数据
     * @返回值类型 :void
     * @param time
     *            监听程序的存活时间,单位为秒,0 则是一直监听
     */
    public void startRead(int time) {
        checkPort();

        try {
            inputStream = new BufferedInputStream(serialPort.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException("获取端口的InputStream出错:" + e.getMessage());
        }

        try {
            serialPort.addEventListener(this);//向SerialPort对象中添加串口事件监听器
        } catch (TooManyListenersException e) {
            throw new RuntimeException(e.getMessage());
        }

        serialPort.notifyOnDataAvailable(true);//设置串口有数据的事件true有效,false无效

        log(String.format("开始监听来自'%1$s'的数据--------------", commPort.getName()));
        if (time > 0) {
            this.threadTime = time * 1000;
//          this.threadTime = time* 10;
            Thread t = new Thread(this);
            t.start();
            log(String.format("监听程序将在%1$d秒后关闭。。。。", threadTime));
        }
    }
    /**
     * @方法名称 :startRead
     * @功能描述 :开始监听从端口中接收的数据
     * @返回值类型 :void
     * @param time
     *            监听程序的存活时间,单位为秒,0 则是一直监听
     */
    public void read(int time) {
        checkPort();

        try {
            inputStream = new BufferedInputStream(serialPort.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException("获取端口的InputStream出错:" + e.getMessage());
        }

        try {
            serialPort.addEventListener(this);//向SerialPort对象中添加串口事件监听器
        } catch (TooManyListenersException e) {
            throw new RuntimeException(e.getMessage());
        }

        serialPort.notifyOnDataAvailable(true);//设置串口有数据的事件true有效,false无效

        log(String.format("开始监听来自'%1$s'的数据--------------", commPort.getName()));
        if (time > 0) {
            this.threadTime = time * 1000;
//          this.threadTime = time* 10;
            Thread t = new Thread(this);
            t.start();
            log(String.format("监听程序将在%1$d秒后关闭。。。。", threadTime));
        }
    }
    /**
     * @方法名称 :close
     * @功能描述 :关闭 SerialPort
     * @返回值类型 :void
     */
    public void close() {
        serialPort.close();
        serialPort = null;
        commPort = null;
    }

    public void log(String msg) {
        System.out.println(appName + " --> " + msg);
    }

    /**
     * 数据接收的监听处理函数
     */
    @Override
    public void serialEvent(SerialPortEvent arg0) {
        switch (arg0.getEventType()) {
        case SerialPortEvent.BI:/* Break interrupt,通讯中断 */
        case SerialPortEvent.OE:/* Overrun error,溢位错误 */
        case SerialPortEvent.FE:/* Framing error,传帧错误 */
        case SerialPortEvent.PE:/* Parity error,校验错误 */
        case SerialPortEvent.CD:/* Carrier detect,载波检测 */
        case SerialPortEvent.CTS:/* Clear to send,清除发送 */
        case SerialPortEvent.DSR:/* Data set ready,数据设备就绪 */
        case SerialPortEvent.RI:/* Ring indicator,响铃指示 */
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*
                                                     * Output buffer is
                                                     * empty,输出缓冲区清空
                                                     */
            break;
        case SerialPortEvent.DATA_AVAILABLE:/*
                                             * Data available at the serial
                                             * port,端口有可用数据。读到缓冲数组,输出到终端
                                             */
            byte[] readBuffer = new byte[1024];
            String readStr = "";
            String s2 = "";

            try {

                while (inputStream.available() > 0) {
                    inputStream.read(readBuffer);
                    readStr += new String(readBuffer).trim();
                }

                s2 = new String(readBuffer).trim();
                //接收的精华再这里
                //1。readStr为当次读入的,一般设备是1位1位读,模拟的时候就很多位,但是不重要
                //2。receiptDataString是用来缓存输入字符串的
                //3。receiptDataString.length()==XX这里可以设定你要接受的长度,然后接收指定数据
                //4。超长或者不符合长度,你可以看情况抛弃数据或者清空,或者累加
                //5。接受成功的数据,放入receiptDataList供获取调用
                //6。nowDataIndex是当前数组的下标,可以参考PortController中对数据获取的方法
                log("接收端口COM->返回数据(长度为" + readStr.length() + "):数据" + s2);
                receiptDataString+=readStr;
                log("receiptDataString->长度" + receiptDataString.length() + "),数据" + receiptDataString);
                if(receiptDataString.length()==58){
                    receiptDataList.add(receiptDataString);
                    receiptDataString="";
                    log("校验通过,数据接收成功");
                }else if(receiptDataString.length()>100){
                    receiptDataString="";
                }
            } catch (IOException e) {
            }
        }
    }

    @Override
    public void run() {
        try {
            Thread.sleep(threadTime);
            serialPort.close();
            log(String.format("端口'%1$s'监听关闭了!", commPort.getName()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        SimpleRead sp = new SimpleRead();
        //sp.listPort();
        sp.selectPort("COM5");
        sp.write("210.36.16.166");
        //sp.write("2");
        //sp.startRead(120);
    }
}

 3.java基于RXTXcomm.jar的串口通信

https://blog.csdn.net/baidu_30541191/article/details/50429836

原文地址:https://www.cnblogs.com/BelieveFish/p/10239234.html