|
|
@ -14,6 +14,7 @@ import org.springframework.context.ApplicationContext; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
//import purejavacomm.SerialPort;
|
|
|
|
//import purejavacomm.SerialPort;
|
|
|
|
import gnu.io.SerialPort; |
|
|
|
import gnu.io.SerialPort; |
|
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer; |
|
|
|
import java.nio.ByteBuffer; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
@ -41,6 +42,7 @@ public class SerialPortSingle { |
|
|
|
DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class); |
|
|
|
DeviceInstallService deviceInstallService = context.getBean(DeviceInstallService.class); |
|
|
|
NowDataService nowDataService = context.getBean(NowDataService.class); |
|
|
|
NowDataService nowDataService = context.getBean(NowDataService.class); |
|
|
|
BuildingService buildingService = context.getBean(BuildingService.class); |
|
|
|
BuildingService buildingService = context.getBean(BuildingService.class); |
|
|
|
|
|
|
|
|
|
|
|
public String serialPortSend(DeviceCodeParamEntity deviceCodeParamEntity) { |
|
|
|
public String serialPortSend(DeviceCodeParamEntity deviceCodeParamEntity) { |
|
|
|
|
|
|
|
|
|
|
|
//查看所有串口
|
|
|
|
//查看所有串口
|
|
|
@ -51,7 +53,10 @@ public class SerialPortSingle { |
|
|
|
String rtData = ""; |
|
|
|
String rtData = ""; |
|
|
|
// System.out.println("发现全部串口:" + port);
|
|
|
|
// System.out.println("发现全部串口:" + port);
|
|
|
|
String comName = deviceCodeParamEntity.getDataCom().toUpperCase(); |
|
|
|
String comName = deviceCodeParamEntity.getDataCom().toUpperCase(); |
|
|
|
if (port.contains(comName)){ |
|
|
|
if (!port.contains(comName)) { |
|
|
|
|
|
|
|
log.info("串口:" + comName + "不存在!"); |
|
|
|
|
|
|
|
return "fail"; |
|
|
|
|
|
|
|
} |
|
|
|
try { |
|
|
|
try { |
|
|
|
try { |
|
|
|
try { |
|
|
|
baudrate = deviceCodeParamEntity.getBaudrate(); |
|
|
|
baudrate = deviceCodeParamEntity.getBaudrate(); |
|
|
@ -66,22 +71,22 @@ public class SerialPortSingle { |
|
|
|
SendOrderUtils.sendSerialPort(deviceCodeParamEntity, serialPort); |
|
|
|
SendOrderUtils.sendSerialPort(deviceCodeParamEntity, serialPort); |
|
|
|
Thread.sleep(1500); |
|
|
|
Thread.sleep(1500); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
log.error("前端设置出现异常==>", e); |
|
|
|
|
|
|
|
return "fail"; |
|
|
|
} |
|
|
|
} |
|
|
|
//对返回数据进行相关解析处理
|
|
|
|
//对返回数据进行相关解析处理
|
|
|
|
receiveStr = null; |
|
|
|
receiveStr = null; |
|
|
|
byte[] bytes = serialPortUtil.readFromPort(serialPort); //读取串口数据
|
|
|
|
byte[] bytes = serialPortUtil.readFromPort(serialPort); //读取串口数据
|
|
|
|
try { |
|
|
|
if (null == bytes) { |
|
|
|
String byteStr = new String(bytes, 0, bytes.length).trim(); |
|
|
|
serialPortUtil.closePort(serialPort); |
|
|
|
|
|
|
|
log.info("单抄串口" + serialPort + "异常,没有数据返回!关闭串口"); |
|
|
|
|
|
|
|
return "fail"; |
|
|
|
|
|
|
|
} |
|
|
|
receiveStr = receiveStr + printHexString(bytes); |
|
|
|
receiveStr = receiveStr + printHexString(bytes); |
|
|
|
//去掉空格和null
|
|
|
|
//去掉空格和null
|
|
|
|
receiveStr = receiveStr.replace("null", ""); |
|
|
|
receiveStr = receiveStr.replace("null", ""); |
|
|
|
receiveStr = receiveStr.replace(" ", ""); |
|
|
|
receiveStr = receiveStr.replace(" ", ""); |
|
|
|
log.info("串口" + serialPort + "接收数据:" + receiveStr + ",大小: " + receiveStr.length()); |
|
|
|
log.info("串口" + serialPort + "接收数据:" + receiveStr + ",大小: " + receiveStr.length()); |
|
|
|
} catch (NullPointerException e) { |
|
|
|
|
|
|
|
serialPortUtil.closePort(serialPort); |
|
|
|
|
|
|
|
log.info("单抄串口"+serialPort+"异常,没有数据返回!关闭串口"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//返回值全部变成大写
|
|
|
|
//返回值全部变成大写
|
|
|
|
String receiveData = receiveStr.toUpperCase(); |
|
|
|
String receiveData = receiveStr.toUpperCase(); |
|
|
|
//截取去掉FE
|
|
|
|
//截取去掉FE
|
|
|
@ -126,6 +131,7 @@ public class SerialPortSingle { |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
log.error(deviceCodeParamEntity.getDeviceType() + "单抄保存数据库失败!"); |
|
|
|
log.error(deviceCodeParamEntity.getDeviceType() + "单抄保存数据库失败!"); |
|
|
|
serialPortUtil.closePort(serialPort); |
|
|
|
serialPortUtil.closePort(serialPort); |
|
|
|
|
|
|
|
return "fail"; |
|
|
|
} |
|
|
|
} |
|
|
|
serialPortUtil.closePort(serialPort); |
|
|
|
serialPortUtil.closePort(serialPort); |
|
|
|
System.out.println("关闭" + serialPort); |
|
|
|
System.out.println("关闭" + serialPort); |
|
|
@ -133,15 +139,14 @@ public class SerialPortSingle { |
|
|
|
log.info("-----------------------------" + serialPort + "单抄结束!-----------------------------"); |
|
|
|
log.info("-----------------------------" + serialPort + "单抄结束!-----------------------------"); |
|
|
|
return rtData; |
|
|
|
return rtData; |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
// e.printStackTrace();
|
|
|
|
log.error("前端设置出现异常==>", e); |
|
|
|
} |
|
|
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
log.info("串口:"+comName+"不存在!"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return "fail"; |
|
|
|
return "fail"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 字节数组转16进制字符串 |
|
|
|
* 字节数组转16进制字符串 |
|
|
|
|
|
|
|
* |
|
|
|
* @param b 字节数组 |
|
|
|
* @param b 字节数组 |
|
|
|
* @return 16进制字符串 |
|
|
|
* @return 16进制字符串 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -159,6 +164,7 @@ public class SerialPortSingle { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 十六进制字符串转byte[] |
|
|
|
* 十六进制字符串转byte[] |
|
|
|
|
|
|
|
* |
|
|
|
* @param hex 十六进制字符串 |
|
|
|
* @param hex 十六进制字符串 |
|
|
|
* @return byte[] |
|
|
|
* @return byte[] |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -183,8 +189,10 @@ public class SerialPortSingle { |
|
|
|
} |
|
|
|
} |
|
|
|
return buffer.array(); |
|
|
|
return buffer.array(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 16进制转换成为string类型字符串 |
|
|
|
* 16进制转换成为string类型字符串 |
|
|
|
|
|
|
|
* |
|
|
|
* @param s 待转换字符串 |
|
|
|
* @param s 待转换字符串 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static String hexStringToString(String s) { |
|
|
|
public static String hexStringToString(String s) { |
|
|
|