package com.XXX.utils;
import com.databus.Log; import gnu.io.*;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.*; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
public class RS485Demo extends Thread implements SerialPortEventListener {
//单例模式提供连接串口的对象 </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> RS485Demo getInstance(){ </span><span style="color: rgba(0, 0, 255, 1)">if</span> (cRead == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ </span><span style="color: rgba(0, 0, 255, 1)">synchronized</span> (RS485Demo.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (cRead == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { cRead </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RS485Demo(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 启动线程来处理收到的数据</span>
cRead.start();
} } } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> cRead; }
// 封装十六进制的打开、关闭命令
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> List<<span style="color: rgba(0, 0, 255, 1)">byte</span>[]> onOrderList =<span style="color: rgba(0, 0, 0, 1)"> Arrays.asList( </span><span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFF, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x8C, 0x3A}, <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x01, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFF, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xDD, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0xFA<span style="color: rgba(0, 0, 0, 1)">}, </span><span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x02, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFF, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x2D, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0xFA}, <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x03, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFF, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x7C, 0x3A<span style="color: rgba(0, 0, 0, 1)">}, </span><span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x04, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFF, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xCD,(<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFB}, <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x05, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFF, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x9C, 0x3B<span style="color: rgba(0, 0, 0, 1)">}, </span><span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x06, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFF, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x6C, 0x3B}, <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x07, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xFF, 0x00, 0x3D, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0xFB<span style="color: rgba(0, 0, 0, 1)">}); </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> List<<span style="color: rgba(0, 0, 255, 1)">byte</span>[]> offOrderList =<span style="color: rgba(0, 0, 0, 1)"> Arrays.asList( </span><span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x00, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xCD, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0xCA},<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x01, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x9C, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0x0A<span style="color: rgba(0, 0, 0, 1)">}, </span><span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x02, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x6C, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0x0A},<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x03, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x3D, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0xCA<span style="color: rgba(0, 0, 0, 1)">}, </span><span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x04, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x8C, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0x0B},<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x05, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0xDD, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0xCB<span style="color: rgba(0, 0, 0, 1)">}, </span><span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x06, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x2D, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0xCB},<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[]{0x01, 0x05, 0x00, 0x07, 0x00, 0x00, (<span style="color: rgba(0, 0, 255, 1)">byte</span>) 0x7C, (<span style="color: rgba(0, 0, 255, 1)">byte</span>)0x0B<span style="color: rgba(0, 0, 0, 1)">}); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 监听器,这里独立开辟一个线程监听串口数据
// 串口通信管理类
<span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> CommPortIdentifier portId; </span><span style="color: rgba(0, 0, 255, 1)">static</span> RS485Demo cRead = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">USB在主机上的通信端口名称,如:COM1、COM2等</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String COMNUM = ""<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">static</span> Enumeration<?><span style="color: rgba(0, 0, 0, 1)"> portList; InputStream inputStream; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从串口来的输入流</span> <span style="color: rgba(0, 0, 255, 1)">static</span> OutputStream outputStream;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 向串口输出的流</span> <span style="color: rgba(0, 0, 255, 1)">static</span> SerialPort serialPort; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 串口的引用 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 堵塞队列用来存放读到的数据</span> <span style="color: rgba(0, 0, 255, 1)">private</span> BlockingQueue<String> msgQueue = <span style="color: rgba(0, 0, 255, 1)">new</span> LinkedBlockingQueue<String><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * SerialPort EventListene 的方法,持续监听端口上是否有数据流 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> serialEvent(SerialPortEvent event) { </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (event.getEventType()) { </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.BI: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.OE: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.FE: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.PE: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.CD: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.CTS: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.DSR: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.RI: </span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> SerialPortEvent.OUTPUT_BUFFER_EMPTY: </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> SerialPortEvent.DATA_AVAILABLE:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当有可用数据时读取数据</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] readBuffer = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">int</span> availableBytes = 0<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { availableBytes </span>=<span style="color: rgba(0, 0, 0, 1)"> inputStream.available(); </span><span style="color: rgba(0, 0, 255, 1)">while</span> (availableBytes > 0<span style="color: rgba(0, 0, 0, 1)">) { readBuffer </span>=<span style="color: rgba(0, 0, 0, 1)"> RS485Demo.readFromPort(serialPort); String needData </span>=<span style="color: rgba(0, 0, 0, 1)"> printHexString(readBuffer); System.out.println(</span><span style="color: rgba(0, 0, 255, 1)">new</span> Date() + "真实收到的数据为:-----" +<span style="color: rgba(0, 0, 0, 1)"> needData); availableBytes </span>=<span style="color: rgba(0, 0, 0, 1)"> inputStream.available(); msgQueue.add(needData); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { } </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">: </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 从串口读取数据 * * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> serialPort 当前已建立连接的SerialPort对象 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 读取到的数据 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] readFromPort(SerialPort serialPort) { InputStream in </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes =<span style="color: rgba(0, 0, 0, 1)"> {}; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { in </span>=<span style="color: rgba(0, 0, 0, 1)"> serialPort.getInputStream(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 缓冲区大小为一个字节</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] readBuffer = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[1<span style="color: rgba(0, 0, 0, 1)">]; </span><span style="color: rgba(0, 0, 255, 1)">int</span> bytesNum =<span style="color: rgba(0, 0, 0, 1)"> in.read(readBuffer); </span><span style="color: rgba(0, 0, 255, 1)">while</span> (bytesNum > 0<span style="color: rgba(0, 0, 0, 1)">) { bytes </span>=<span style="color: rgba(0, 0, 0, 1)"> concat(bytes, readBuffer); bytesNum </span>=<span style="color: rgba(0, 0, 0, 1)"> in.read(readBuffer); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (in != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { in.close(); in </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bytes; } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 通过程序打开COM串口,设置监听器以及相关的参数 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 返回1 表示端口打开成功,返回 0表示端口打开失败 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> startComPort() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过串口通信管理类获得当前连接上的串口列表</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { Log.info(</span>"开始获取串口。。。"<span style="color: rgba(0, 0, 0, 1)">); portList </span>=<span style="color: rgba(0, 0, 0, 1)"> CommPortIdentifier.getPortIdentifiers(); Log.info(</span>"获取串口。。。" +<span style="color: rgba(0, 0, 0, 1)"> portList); Log.info(</span>"获取串口结果。。。" +<span style="color: rgba(0, 0, 0, 1)"> portList.hasMoreElements()); </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (portList.hasMoreElements()) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取相应串口对象</span>
Log.info(portList.nextElement());
portId </span>=<span style="color: rgba(0, 0, 0, 1)"> (CommPortIdentifier) portList.nextElement(); System.out.println(</span>"设备类型:--->" +<span style="color: rgba(0, 0, 0, 1)"> portId.getPortType()); System.out.println(</span>"设备名称:---->" +<span style="color: rgba(0, 0, 0, 1)"> portId.getName()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断端口类型是否为串口</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (portId.getPortType() ==<span style="color: rgba(0, 0, 0, 1)"> CommPortIdentifier.PORT_SERIAL) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断如果COM4串口存在,就打开该串口
// if (portId.getName().equals(portId.getName())) {
<span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (portId.getName().equals(COMNUM)) { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开串口名字为COM_4(名字任意),延迟为1000毫秒</span> serialPort = (SerialPort) portId.open(portId.getName(), 1000<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (PortInUseException e) { System.out.println(</span>"打开端口失败!"<span style="color: rgba(0, 0, 0, 1)">); e.printStackTrace(); </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置当前串口的输入输出流</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { inputStream </span>=<span style="color: rgba(0, 0, 0, 1)"> serialPort.getInputStream(); outputStream </span>=<span style="color: rgba(0, 0, 0, 1)"> serialPort.getOutputStream(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 给当前串口添加一个监听器,serialEvent方法监听串口返回的数据</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { serialPort.addEventListener(</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (TooManyListenersException e) { e.printStackTrace(); </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置监听器生效,即:当有数据时通知</span> serialPort.notifyOnDataAvailable(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置串口的一些读写参数</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 比特率、数据位、停止位、奇偶校验位</span> serialPort.setSerialPortParams(9600<span style="color: rgba(0, 0, 0, 1)">, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (UnsupportedCommOperationException e) { e.printStackTrace(); </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> 1<span style="color: rgba(0, 0, 0, 1)">; } } } }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e){ e.printStackTrace(); Log.info(e); </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">; } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> run() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { System.out.println(</span>"--------------任务处理线程运行了--------------"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果堵塞队列中存在数据就将其输出</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (msgQueue.size() > 0<span style="color: rgba(0, 0, 0, 1)">) { String vo </span>=<span style="color: rgba(0, 0, 0, 1)"> msgQueue.peek(); String vos[] </span>= vo.split(" ", -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据返回数据可以做相应的业务逻辑操作
// getData(vos); // sendOrder(); msgQueue.take();
} }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e){ e.printStackTrace(); } } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 16转10计算</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> getNum(String num1, String num2) { </span><span style="color: rgba(0, 0, 255, 1)">long</span> value = Long.parseLong(num1, 16) * 256 + Long.parseLong(num2, 16<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> value; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 字节数组转字符串</span> <span style="color: rgba(0, 0, 255, 1)">private</span> String printHexString(<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] b) { StringBuffer sbf </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuffer(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < b.length; i++<span style="color: rgba(0, 0, 0, 1)">) { String hex </span>= Integer.toHexString(b[i] & 0xFF<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (hex.length() == 1<span style="color: rgba(0, 0, 0, 1)">) { hex </span>= '0' +<span style="color: rgba(0, 0, 0, 1)"> hex; } sbf.append(hex.toUpperCase() </span>+ " "<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sbf.toString().trim(); } </span><span style="color: rgba(0, 128, 0, 1)">/</span><span style="color: rgba(0, 128, 0, 1)"> * 合并数组 * * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> firstArray 第一个数组 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> secondArray 第二个数组 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 合并后的数组 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] concat(<span style="color: rgba(0, 0, 255, 1)">byte</span>[] firstArray, <span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] secondArray) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (firstArray == <span style="color: rgba(0, 0, 255, 1)">null</span> || secondArray == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (firstArray != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> firstArray; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (secondArray != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> secondArray; </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[firstArray.length +<span style="color: rgba(0, 0, 0, 1)"> secondArray.length]; System.arraycopy(firstArray, </span>0, bytes, 0<span style="color: rgba(0, 0, 0, 1)">, firstArray.length); System.arraycopy(secondArray, </span>0<span style="color: rgba(0, 0, 0, 1)">, bytes, firstArray.length, secondArray.length); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bytes; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">num:偶数启动报警器,奇数关闭报警器 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">commandInfo:偶数打开,奇数关闭;channel:继电器通道;comNum:串口设备通信名称</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> startRS485(<span style="color: rgba(0, 0, 255, 1)">int</span> commandInfo,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> channel,String comNum) { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span>(cRead == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ cRead </span>=<span style="color: rgba(0, 0, 0, 1)"> getInstance(); } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!COMNUM.equals(comNum) && <span style="color: rgba(0, 0, 255, 1)">null</span> !=<span style="color: rgba(0, 0, 0, 1)"> serialPort){ serialPort.close(); COMNUM </span>=<span style="color: rgba(0, 0, 0, 1)"> comNum; } </span><span style="color: rgba(0, 0, 255, 1)">int</span> i = 1<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (serialPort == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){ COMNUM </span>=<span style="color: rgba(0, 0, 0, 1)"> comNum; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开串口通道并连接</span> i =<span style="color: rgba(0, 0, 0, 1)"> cRead.startComPort(); } </span><span style="color: rgba(0, 0, 255, 1)">if</span> (i == 1<span style="color: rgba(0, 0, 0, 1)">){ Log.info(</span>"串口连接成功"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据提供的文档给出的发送命令,发送16进制数据给仪器</span> <span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] b; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (commandInfo % 2 == 0<span style="color: rgba(0, 0, 0, 1)">) { b </span>=<span style="color: rgba(0, 0, 0, 1)"> onOrderList.get(channel); }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ b </span>=<span style="color: rgba(0, 0, 0, 1)"> offOrderList.get(channel); } System.out.println(</span>"发送的数据:" +<span style="color: rgba(0, 0, 0, 1)"> b); System.out.println(</span>"发出字节数:" +<span style="color: rgba(0, 0, 0, 1)"> b.length); outputStream.write(b); outputStream.flush(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (outputStream != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { outputStream.close(); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">每次调用完以后关闭串口通道</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">null</span> !=<span style="color: rgba(0, 0, 0, 1)"> cRead){ </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">null</span> !=<span style="color: rgba(0, 0, 0, 1)"> serialPort){ serialPort.close(); serialPort </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } cRead.interrupt(); cRead </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } } }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ Log.info(</span>"串口连接失败"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">; } }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e){ e.printStackTrace(); Log.info(</span>"串口连接失败"<span style="color: rgba(0, 0, 0, 1)">); } } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开通道1的电路,对应设备名称COM3</span> startRS485(0,1,"COM3"<span style="color: rgba(0, 0, 0, 1)">); }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/17029.html