你的位置:首页 > Java教程

[Java教程]java实现串口通讯


一. 准备工作

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

  2. 将RXTXcomm.jar放到  %JAVA_HOME%\jre\lib\ext\  目录下,工程中引入该jar包

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

View Code