加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java串口开发,rs232,comm.jar

(2016-11-30 11:13:49)
标签:

comm

rs232

serialport

commportidentifier

分类: java
首先,要做三步:
win32com.dll放到jre的/bin目录下。
javax.comm.properties放到jre的lib目录下。
comm.jar放到随便哪个classPath里就行了,能引用到即可。
操作类代码如下,供参考:

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;

public class ComPortUtil {

    static SerialPort serialPort null;
    static CommPortIdentifier portId null;

    
    public static boolean OpenPort(String port) {
        CommPortIdentifier portId null;
        try {
            // 打开串口,如com1,com2什么的
            portId CommPortIdentifier.getPortIdentifier(port);
        catch (NoSuchPortException e) {
            return false;
        }

        try {
            // 使用串口的程序名称,超时时间
            serialPort (SerialPort) portId.open("Hello World程序名称", 100);
        catch (PortInUseException e1) {
            return false;
        }
        if (serialPort == null) {
            return false;
        }

        try {
            // 设置波特率什么的
            serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            // 接收超时时间100毫秒
            serialPort.enableReceiveTimeout(100);
        catch (UnsupportedCommOperationException e) {
            return false;
        }
        return true;
    }

    
    public static byte[] Read() {
        if (serialPort == null) {
            return null;
        }

        byte[] data new byte[254];
        try {
            InputStream in serialPort.getInputStream();
            int length in.read(data);
            // length是读取的真实长度,可以根据它截取一下数组。
            byte[] trueData new byte[length];
            System.arraycopy(data, 0, trueData, 0, length);
            return trueData;
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    
    public static boolean Write(byte[] data) {
        if (serialPort != null) {
            try {
                if (serialPort.getOutputStream() == null) {
                    return false;
                }
                serialPort.getOutputStream().write(data);
                return true;
            catch (IOException e) {
                return false;
            }
        }
        return false;
    }

    
    public static boolean ClosePort() {
        if (serialPort != null) {
            serialPort.notifyOnDataAvailable(false);

            try {
                if (serialPort.getInputStream() == null) {
                    return false;
                }
                serialPort.getInputStream().close();

                if (serialPort.getOutputStream() == null) {
                    return false;
                }
                serialPort.getOutputStream().close();
                serialPort.removeEventListener();
                serialPort.close();
                return true;
            catch (IOException e) {
                e.printStackTrace();
                return false;
            finally {
                portId null;
                serialPort null;
            }
        }
        return false;
    }

    
    @SuppressWarnings("unchecked")
    public static List getComList() {
        List comList new ArrayList();

        // 返回枚举,包括串口和并口。
        Enumeration portList CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier port (CommPortIdentifier) portList
                    .nextElement();
            // 如果类型是串口,则添加。
            if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                comList.add(port.getName().toUpperCase());
            }
        }
        return comList;
    }

    public static void main(String[] args) {

        ComPortUtil.OpenPort("COM1");
        ComPortUtil.Write(new byte[] (byte) 0xFF, (byte) 0xFF, (byte) 0x86,
                (byte) 0xFA, 0x7C, (byte) 0xFF });
        byte[] data ComPortUtil.Read();
        for (byte data) {
            System.out.println(b);
        }
        ComPortUtil.ClosePort();
    }
}

0

阅读 收藏 喜欢 打印举报/Report
后一篇:pdm导出Excel
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有