You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
7.2 KiB
190 lines
7.2 KiB
package com.mh.user.job;//package com.mh.quartz.job; |
|
// |
|
//import com.mh.quartz.manage.SerialPortManager; |
|
//import com.mh.quartz.utils.AnalysisReceiveOrder485; |
|
//import com.mh.quartz.utils.ExchangeStringUtil; |
|
//import com.mh.quartz.utils.GetReadOrder485; |
|
//import gnu.io.PortInUseException; |
|
//import gnu.io.SerialPort; |
|
//import lombok.extern.slf4j.Slf4j; |
|
//import org.springframework.scheduling.annotation.Scheduled; |
|
//import org.springframework.stereotype.Component; |
|
// |
|
//import java.util.Date; |
|
//import java.util.concurrent.BlockingQueue; |
|
//import java.util.concurrent.LinkedBlockingQueue; |
|
// |
|
///** |
|
// * @author ljf |
|
// * @title : |
|
// * @description : 定时采集发送水电表数据 |
|
// * @updateTime 2020-04-22 |
|
// * @throws : |
|
// */ |
|
//@Component |
|
//@Slf4j |
|
//public class SendMeterWaterJob { |
|
// |
|
// // 串口对象 |
|
// private SerialPort mSerialPort; |
|
// |
|
// AnalysisReceiveOrder485 analysisReceiveOrder485; |
|
// |
|
// // 测试 |
|
// @Scheduled(cron = "0,5 * * * * ? ") |
|
// public void test() throws InterruptedException { |
|
// Thread.sleep(10000); |
|
// log.info("延迟10s"); |
|
// } |
|
// |
|
// @Scheduled(cron = "0,20,40 * * * * ? ") |
|
// public void task1() throws PortInUseException, InterruptedException { |
|
// |
|
// // 堵塞队列用来存放读到的数据 |
|
// BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(); |
|
// log.info("每0、20、40秒执行一次!"); |
|
// if (mSerialPort == null){ |
|
// // 打开串口 |
|
// } else { |
|
// // 关闭串口 |
|
// SerialPortManager.closePort(mSerialPort); |
|
// } |
|
// |
|
// mSerialPort = SerialPortManager.openPort("COM2",1200); |
|
// |
|
// // 发送数据(采集冷量计累计流量) |
|
// String sendMeterOrderStr = "FEFEFE" + GetReadOrder485.createMeterOrder("080140001125","1"); |
|
// SerialPortManager.sendToPort(mSerialPort, ExchangeStringUtil.hexStrToBinaryStr(sendMeterOrderStr)); |
|
// |
|
// // 添加串口监听 |
|
// SerialPort finalMSerialPort = mSerialPort; |
|
// SerialPortManager.addListener(mSerialPort, () -> { |
|
// byte[] data = null; |
|
// try { |
|
// // 读取串口数据 |
|
// data = SerialPortManager.readFromPort(finalMSerialPort); |
|
// String needData = ExchangeStringUtil.printHexString(data); |
|
// msgQueue.add(needData); |
|
// } catch (Exception e) { |
|
// log.info(e.getMessage()); |
|
// } |
|
// }); |
|
// |
|
// // 创建线程解析队列信息 |
|
// Thread thread = new Thread(() -> { |
|
// try { |
|
// log.info("--------------任务处理线程运行了--------------"); |
|
// String vo = ""; |
|
// String vos[] = new String[0]; |
|
// while (true) { |
|
// // 判断数组是否完整 |
|
// // 电表 |
|
// if (vos.length >= 22) { |
|
// log.info("获取到数据长度: " + vos.length); |
|
// log.info("电表: " + new Date() + " 完整收到的数据为:-----" + vo); |
|
// // 解析接收到的报文 |
|
// analysisReceiveOrder485.analysisMeterOrder485(vo); |
|
// vos = new String[0]; |
|
// vo = ""; |
|
//// sendOrder(); |
|
// } else { |
|
// // 如果堵塞队列中存在数据就将其输出 |
|
// if (msgQueue.size() > 0) { |
|
// if (vo == "") { |
|
// vo = msgQueue.peek(); |
|
// } else { |
|
// vo = vo + " " + msgQueue.peek(); |
|
// } |
|
// vos = vo.split(" ", -1); |
|
//// getData(vos); |
|
// msgQueue.take(); |
|
// } |
|
// } |
|
// } |
|
// } catch (Exception e) { |
|
// // TODO Auto-generated catch block |
|
// e.printStackTrace(); |
|
// } |
|
// }); |
|
// |
|
// thread.start(); |
|
// |
|
// } |
|
// |
|
// @Scheduled(cron = "10,30,50 * * * * ? ") |
|
// public void task2() throws PortInUseException, InterruptedException { |
|
// // 堵塞队列用来存放读到的数据 |
|
// BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(); |
|
// log.info("每10、30、50秒执行一次!"); |
|
// if (mSerialPort == null){ |
|
// // 打开串口 |
|
// } else { |
|
// // 关闭串口 |
|
// SerialPortManager.closePort(mSerialPort); |
|
// } |
|
// mSerialPort = SerialPortManager.openPort("COM3",9600); |
|
// |
|
// // 发送数据(采集冷量计累计流量) |
|
// String sendOrderStr = GetReadOrder485.createCloudOrder("88","34"); |
|
// SerialPortManager.sendToPort(mSerialPort, ExchangeStringUtil.hexStrToBinaryStr(sendOrderStr)); |
|
// |
|
// // 添加串口监听 |
|
// SerialPort finalMSerialPort = mSerialPort; |
|
// SerialPortManager.addListener(mSerialPort, () -> { |
|
// byte[] data = null; |
|
// try { |
|
// // 读取串口数据 |
|
// data = SerialPortManager.readFromPort(finalMSerialPort); |
|
// String needData = ExchangeStringUtil.printHexString(data); |
|
// msgQueue.add(needData); |
|
// } catch (Exception e) { |
|
// log.info(e.getMessage()); |
|
// } |
|
// }); |
|
// |
|
// // 创建线程解析队列信息 |
|
// Thread thread = new Thread(() -> { |
|
// try { |
|
// log.info("--------------任务处理线程运行了--------------"); |
|
// String vo = ""; |
|
// String vos[] = new String[0]; |
|
// while (true) { |
|
// // 判断数组是否完整 |
|
// // 冷量计 |
|
// if (vos.length >= 9) { |
|
// if (vos.length == 9) { |
|
// log.info("获取到数据长度: " + vos.length); |
|
// log.info("冷量计: " + new Date() + " 完整收到的数据为:-----" + vo); |
|
// // 解析接收到的报文 |
|
// analysisReceiveOrder485.analysisCloudOrder485(vo); |
|
// } |
|
// vos = new String[0]; |
|
// vo = ""; |
|
//// sendOrder(); |
|
// } else { |
|
// // 如果堵塞队列中存在数据就将其输出 |
|
// if (msgQueue.size() > 0) { |
|
// if (vo.equalsIgnoreCase("")){ |
|
// vo = msgQueue.peek(); |
|
// } else { |
|
// vo = vo + " " + msgQueue.peek(); |
|
// } |
|
// vos = vo.split(" ", -1); |
|
//// getData(vos); |
|
// msgQueue.take(); |
|
// } |
|
// } |
|
// |
|
// |
|
// } |
|
// } catch (Exception e) { |
|
// // TODO Auto-generated catch block |
|
// e.printStackTrace(); |
|
// } |
|
// }); |
|
// |
|
// thread.start(); |
|
// |
|
// } |
|
// |
|
//}
|
|
|