|
|
|
package com.zhonglai.luhui.smart.feeder.service;
|
|
|
|
|
|
|
|
import com.ruoyi.common.utils.GsonConstructor;
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.dto.*;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.dto.commd.FeederCommdDto;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.util.FeederCommd06ResponseType;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.util.FeederCommdUtil;
|
|
|
|
import com.zhonglai.luhui.smart.feeder.util.serial.SerialTool;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import purejavacomm.SerialPort;
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
...
|
...
|
@@ -29,11 +20,6 @@ import java.util.concurrent.TimeUnit; |
|
|
|
public class DeviceService {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(DeviceService.class);
|
|
|
|
|
|
|
|
// 锁对象
|
|
|
|
private final Object lock = new Object();
|
|
|
|
// 用于存储串口返回的数据,使用线程安全的队列
|
|
|
|
private BlockingQueue<ModbusDto> dataQueue = new LinkedBlockingQueue<>();
|
|
|
|
|
|
|
|
private Double backArea; //上一个大小
|
|
|
|
|
|
|
|
private Double slope; //斜率
|
|
...
|
...
|
@@ -57,7 +43,8 @@ public class DeviceService { |
|
|
|
@Autowired
|
|
|
|
private EhCacheService ehCacheService;
|
|
|
|
|
|
|
|
private SerialPort serialPort;
|
|
|
|
@Autowired
|
|
|
|
private SerialPortService serialPortService;
|
|
|
|
|
|
|
|
public void run()
|
|
|
|
{
|
|
...
|
...
|
@@ -72,22 +59,22 @@ public class DeviceService { |
|
|
|
{
|
|
|
|
if(null !=configurationParameterService.getStateData() && 1==configurationParameterService.getStateData().getRunmode())
|
|
|
|
{
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runmode,0)); //,运行模式改成手动
|
|
|
|
serialPortService.sendHexData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runmode,0)); //,运行模式改成手动
|
|
|
|
}
|
|
|
|
if(null !=configurationParameterService.getStateData() && 0==configurationParameterService.getStateData().getSwitch_status())
|
|
|
|
{
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.OnOroff,1)); //,开关是关的就先打开开关
|
|
|
|
serialPortService.sendHexData(FeederCommdUtil.controlData(FeederCommd06ResponseType.OnOroff,1)); //,开关是关的就先打开开关
|
|
|
|
}
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runspeed,nowGear));
|
|
|
|
serialPortService.sendHexData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runspeed,nowGear));
|
|
|
|
oldGear = nowGear;
|
|
|
|
}else{
|
|
|
|
if(null !=configurationParameterService.getStateData() && 0==configurationParameterService.getStateData().getRunmode())
|
|
|
|
{
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runmode,1)); //,运行模式改成自动
|
|
|
|
serialPortService.sendHexData(FeederCommdUtil.controlData(FeederCommd06ResponseType.runmode,1)); //,运行模式改成自动
|
|
|
|
}
|
|
|
|
if(null !=configurationParameterService.getStateData() && 1==configurationParameterService.getStateData().getSwitch_status())
|
|
|
|
{
|
|
|
|
send485SerialData(FeederCommdUtil.controlData(FeederCommd06ResponseType.OnOroff,0)); //,开关是关的就先打开开关
|
|
|
|
serialPortService.sendHexData(FeederCommdUtil.controlData(FeederCommd06ResponseType.OnOroff,0)); //,开关是关的就先打开开关
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -137,20 +124,6 @@ public class DeviceService { |
|
|
|
return absValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void send485SerialData(String commd)
|
|
|
|
{
|
|
|
|
if(null == serialPort)
|
|
|
|
{
|
|
|
|
openSerialPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
sendData(commd);
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error("串口指令发送错误",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据斜率计算档位
|
|
|
|
* @return
|
|
...
|
...
|
@@ -174,107 +147,4 @@ public class DeviceService { |
|
|
|
return gear;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void openSerialPort()
|
|
|
|
{
|
|
|
|
SerialPortConfig serialPortConfig = (SerialPortConfig) configurationParameterService.getConfig(ConfigurationParameter.SerialPortConfig);
|
|
|
|
try {
|
|
|
|
openSerialPort(serialPortConfig.getPortName(),serialPortConfig.getBaudrate(),serialPortConfig.getDataBits(),serialPortConfig.getStopBits(),serialPortConfig.getParity());
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取所有串口
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public ArrayList<String> getAllSerial()
|
|
|
|
{
|
|
|
|
return SerialTool.findPorts();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 打开串口
|
|
|
|
* @param portName 串口名称
|
|
|
|
* @param baudrate 波特率,用于指定每秒传输的位数。
|
|
|
|
* @param dataBits 数据位,表示每个字节的位数。常见的值为 5、6、7、8。
|
|
|
|
* @param stopBits 停止位,用于指定每个字节的停止位数。
|
|
|
|
* @param parity 校验位,用于验证数据的正确性。常见的值有 NONE(无校验)、ODD(奇校验)、EVEN(偶校验)等。
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public void openSerialPort(String portName, Integer baudrate, Integer dataBits, Integer stopBits,
|
|
|
|
Integer parity) throws Exception {
|
|
|
|
if(null != serialPort)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
serialPort = SerialTool.openPort(portName,baudrate,dataBits,stopBits,parity);
|
|
|
|
SerialTool.addListener(serialPortEvent -> {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
FeederCommdDto commdDto = new FeederCommdDto (SerialTool.readFromPort(serialPort));
|
|
|
|
dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("返回数据处理异常",e);
|
|
|
|
}
|
|
|
|
}, serialPort);
|
|
|
|
logger.info("打开串口成功");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void nowOpenSerial(String portName, Integer baudrate, Integer dataBits, Integer stopBits,
|
|
|
|
Integer parity) throws Exception {
|
|
|
|
if(null != serialPort)
|
|
|
|
{
|
|
|
|
serialPort.removeEventListener();
|
|
|
|
serialPort.close();
|
|
|
|
}
|
|
|
|
serialPort = SerialTool.openPort(portName,baudrate,dataBits,stopBits,parity);
|
|
|
|
SerialTool.addListener(serialPortEvent -> {
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
FeederCommdDto commdDto = new FeederCommdDto (SerialTool.readFromPort(serialPort));
|
|
|
|
dataQueue.offer(commdDto); // 将数据添加到队列中// 处理串口返回的数据
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("返回数据处理异常",e);
|
|
|
|
}
|
|
|
|
}, serialPort);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void openDefaultSerialPort() throws Exception {
|
|
|
|
SerialPortConfig serialPortConfig = (SerialPortConfig) configurationParameterService.getConfig(ConfigurationParameter.SerialPortConfig);
|
|
|
|
openSerialPort(serialPortConfig.getPortName(),serialPortConfig.getBaudrate(),serialPortConfig.getDataBits(),serialPortConfig.getStopBits(),serialPortConfig.getParity());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发送数据
|
|
|
|
* @param hexStr
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public ModbusDto sendData(String hexStr) throws IOException {
|
|
|
|
synchronized (lock)
|
|
|
|
{
|
|
|
|
SerialTool.sendToPort(SerialTool.HexString2Bytes(hexStr),serialPort);
|
|
|
|
try {
|
|
|
|
ModbusDto reStr = dataQueue.poll(15,TimeUnit.SECONDS);
|
|
|
|
return reStr;
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
logger.error("等待串口返回数据异常!" + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void close()
|
|
|
|
{
|
|
|
|
dataQueue.clear();
|
|
|
|
if(null != serialPort)
|
|
|
|
{
|
|
|
|
serialPort.removeEventListener();
|
|
|
|
serialPort.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
...
|
...
|
|