你的位置:首页 > Java教程

[Java教程]串口通讯


一. 准备工作

  1. 点击此下载java串口通讯相关工具

  2. 将RXTXcomm.jar放到  %JAVA_HOME%\jre\lib\ext\  目录下

  3. 将rxtxSerial.dll放到  %JAVA_HOME%\jre\bin\  目录下

 

二. 串口相关类

  1. 串口类:CommPort.class

 1 public class CommPort { 2   /** 3    * 串口名称,如:COM1、COM2 4   */ 5   private String commPortName="";       6    7   /** 8    * 串口所有者名称,一般为应用程序的名称 9   */10   private String ownerName;         11   12   /**13    * 波特率14   */15   private String baudRate="";        16   17   /**18    * 数据位19   */20   private String dataBit="";        21   22   /**23    * 校验位24   */25   private String partityBit="";       26   27   /**28    * 停止位29   */30   private String stopBit="";        31   32   public String getCommPortName() {33     return commPortName;34   }35 36   public void setCommPortName(String commPortName) {37     this.commPortName = commPortName;38   }39 40   public String getOwnerName() {41     return ownerName;42   }43 44   public void setOwnerName(String ownerName) {45     this.ownerName = ownerName;46   }47 48   public String getBaudRate() {49     return baudRate;50   }51 52   public void setBaudRate(String baudRate) {53     this.baudRate = baudRate;54   }55 56   public String getDataBit() {57     return dataBit;58   }59 60   public void setDataBit(String dataBit) {61     this.dataBit = dataBit;62   }63 64   public String getPartityBit() {65     return partityBit;66   }67 68   public void setPartityBit(String partityBit) {69     this.partityBit = partityBit;70   }71 72   public String getStopBit() {73     return stopBit;74   }75 76   public void setStopBit(String stopBit) {77     this.stopBit = stopBit;78   }79   80 }

View Code


  2. 串口管理员类:CommPortManager.class

 1 import gnu.io.CommPortIdentifier; 2 import gnu.io.SerialPort; 3  4 import java.io.BufferedReader; 5 import java.io.BufferedWriter; 6 import java.io.DataInputStream; 7 import java.io.DataOutputStream; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.io.OutputStream; 11 import java.io.OutputStreamWriter; 12 import java.util.Enumeration; 13  14 public class CommPortManager { 15   /** 16    * 串口定义 17   */ 18   private CommPort commPort;      19    20   /** 21    * 串口对象 22   */ 23   private SerialPort serialPort;    24    25   /** 26    * 字节输入 27   */ 28   private InputStream in;         29    30   /** 31    * 字节输出 32   */ 33   private OutputStream out;      34    35   /** 36    * 字符输入 37   */ 38   private BufferedReader bufReader;  39    40   /** 41    * 字符输出 42   */ 43   private BufferedWriter bufWriter;  44    45   /** 46    * 数据输入 47   */ 48   private DataInputStream dataIn;   49    50   /** 51    * 数据输出 52   */ 53   private DataOutputStream dataOut;  54    55   /** 56    * 串口是否在使用 57   */ 58   private boolean isUse;        59    60   public CommPortManager(CommPort commPort){ 61     this.commPort = commPort; 62   } 63    64   /** 65    * 打开串口 66    * @throws Exception 67   */ 68   public void open() throws Exception{ 69     CommPortIdentifier commPortId = CommPortIdentifier.getPortIdentifier(commPort.getCommPortName()); 70      71     // 第一个参数:通常设置为你的应用程序的名字;第二个参数:开启端口超时的毫秒数 72     serialPort = (SerialPort)commPortId.open(commPort.getOwnerName(), 5000); 73     serialPort.setOutputBufferSize(2048); 74      75     in = serialPort.getInputStream(); 76     out = serialPort.getOutputStream();     77      78     bufReader = new BufferedReader(new InputStreamReader(in, "Unicode")); 79     bufWriter = new BufferedWriter(new OutputStreamWriter(out)); 80      81     dataIn = new DataInputStream(in); 82     dataOut = new DataOutputStream(out); 83      84     // 设置串口参数 85     serialPort.setSerialPortParams(Integer.valueOf(commPort.getBaudRate()), Integer.valueOf(commPort.getDataBit()),  86         Integer.valueOf(commPort.getStopBit()), Integer.valueOf(commPort.getPartityBit())); 87   } 88    89   /** 90    * 判断串口是否可用 91   */ 92   public boolean commPortEnable(){   93     boolean result = false; 94     Enumeration en = CommPortIdentifier.getPortIdentifiers(); 95     while (en.hasMoreElements()) {        96       CommPortIdentifier portId = (CommPortIdentifier) en.nextElement(); 97       if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL 98           && portId.getName().equalsIgnoreCase(commPort.getCommPortName())) { 99         result = true;100       }101     }102     return result;103   }104 105   public CommPort getCommPort() {106     return commPort;107   }108 109   public SerialPort getSerialPort() {110     return serialPort;111   }112 113   public InputStream getIn() {114     return in;115   }116 117   public OutputStream getOut() {118     return out;119   }120 121   public BufferedReader getBufReader() {122     return bufReader;123   }124 125   public BufferedWriter getBufWriter() {126     return bufWriter;127   }128 129   public DataInputStream getDataIn() {130     return dataIn;131   }132 133   public DataOutputStream getDataOut() {134     return dataOut;135   }136 137   public boolean isUse() {138     return isUse;139   }140 141   public void setUse(boolean isUse) {142     this.isUse = isUse;143   }144 }

View Code