项目初始化
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package com.ruoyi.schedule.plcRead.ddj;
|
||||
|
||||
import HslCommunication.BasicFramework.SoftBasic;
|
||||
import HslCommunication.Core.Net.IReadWriteNet;
|
||||
import HslCommunication.Core.Types.OperateResult;
|
||||
import HslCommunication.Core.Types.OperateResultExOne;
|
||||
import HslCommunication.Profinet.Siemens.SiemensPLCS;
|
||||
import HslCommunication.Profinet.Siemens.SiemensS7Net;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.ruoyi.common.domain.HslRead;
|
||||
import com.ruoyi.common.domain.PlcConnectInfo;
|
||||
import com.ruoyi.device.ddj.domain.MsgDdj;
|
||||
import com.ruoyi.device.ssj.domain.DeviceSsj;
|
||||
import com.ruoyi.schedule.plcRead.util.ParseUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Slf4j
|
||||
public class PlcDdjRead extends Thread {
|
||||
public static ConcurrentHashMap<String, MsgDdj> deviceDdjMap;
|
||||
public volatile boolean isRun = true;
|
||||
|
||||
public SiemensS7Net siemensS7Net = null;
|
||||
public static IReadWriteNet readWriteNet = null;
|
||||
private final String ip;
|
||||
private final int port;
|
||||
private boolean success = false;
|
||||
|
||||
public PlcDdjRead(ConcurrentHashMap<String, MsgDdj> deviceDdjMap, PlcConnectInfo plcConnectInfo) {
|
||||
PlcDdjRead.deviceDdjMap = deviceDdjMap;
|
||||
this.ip = plcConnectInfo.getIp();
|
||||
this.port = plcConnectInfo.getPort();
|
||||
}
|
||||
|
||||
public static int WriteToSSJ(String address, byte[] data) {
|
||||
int a = 0;
|
||||
if (readWriteNet != null) {
|
||||
a = HslRead.WriteResultRender(readWriteNet.Write(address, data), address);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (isRun) {
|
||||
try {
|
||||
if (IsConnect()) {
|
||||
for (String key : deviceDdjMap.keySet()) {
|
||||
MsgDdj msgDdj = deviceDdjMap.get(key);
|
||||
if (msgDdj != null) {
|
||||
readPlcDdj(msgDdj);
|
||||
}
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
} else {
|
||||
reConnect();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
log.info("********IP:" + this.ip + " port:" + this.port + "<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>쳣 " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void readPlcDdj(MsgDdj msgDdj) {
|
||||
OperateResultExOne<byte[]> read2 = readWriteNet.Read(msgDdj.getPlcDbReadAddress(), Convert.toShort("38"));
|
||||
String ddsx = "0";
|
||||
if (read2.IsSuccess) {
|
||||
ddsx = SoftBasic.ByteToHexString(read2.Content);
|
||||
msgDdj.setCzfs(ParseUtil.optInt(0, ddsx));
|
||||
msgDdj.setSbzt(ParseUtil.optInt(2, ddsx));
|
||||
msgDdj.setRwh(Convert.toLong(ParseUtil.optDInt(4, ddsx)));
|
||||
msgDdj.setHczt(ParseUtil.optInt(8, ddsx));
|
||||
msgDdj.setJdbz(ParseUtil.optInt(10, ddsx));
|
||||
msgDdj.setYph(ParseUtil.optInt(12, ddsx));
|
||||
msgDdj.setYlh(ParseUtil.optInt(14, ddsx));
|
||||
msgDdj.setYch(ParseUtil.optInt(16, ddsx));
|
||||
msgDdj.setMbph(ParseUtil.optInt(18, ddsx));
|
||||
msgDdj.setMblh(ParseUtil.optInt(20, ddsx));
|
||||
msgDdj.setMbch(ParseUtil.optInt(22, ddsx));
|
||||
msgDdj.setYxxf(ParseUtil.optInt(24, ddsx));
|
||||
msgDdj.setXtxh(ParseUtil.optInt(26, ddsx));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ConnectServer() {
|
||||
siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500);
|
||||
try {
|
||||
siemensS7Net.setIpAddress(this.ip);
|
||||
siemensS7Net.setPort(this.port);
|
||||
siemensS7Net.setRack((byte) 0);
|
||||
siemensS7Net.setSlot((byte) 1);
|
||||
|
||||
OperateResult connect = siemensS7Net.ConnectServer();
|
||||
if (connect.IsSuccess) {
|
||||
setIsConnect(true);
|
||||
readWriteNet = siemensS7Net;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.info("********IP:" + this.ip + " port:" + this.port + "连接失败 " + ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setIsConnect(boolean bflag) {
|
||||
if (!success) {
|
||||
success = bflag;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean IsConnect() {
|
||||
if (success) {
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void reConnect() {
|
||||
closeServer();
|
||||
ConnectServer();
|
||||
log.info("重新连接 SSJ-PLC");
|
||||
}
|
||||
|
||||
void closeServer() {
|
||||
if (readWriteNet != null) {
|
||||
readWriteNet.ConnectClose();
|
||||
}
|
||||
setIsConnect(false);
|
||||
}
|
||||
|
||||
public static int WriteToDDJ(String address, byte[] data) {
|
||||
int a = 0;
|
||||
if (readWriteNet != null) {
|
||||
a = HslRead.WriteResultRender(readWriteNet.Write(address, data), address);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package com.ruoyi.schedule.plcRead.ssj;
|
||||
|
||||
import HslCommunication.BasicFramework.SoftBasic;
|
||||
import HslCommunication.Core.Net.IReadWriteNet;
|
||||
import HslCommunication.Core.Types.OperateResult;
|
||||
import HslCommunication.Core.Types.OperateResultExOne;
|
||||
import HslCommunication.Profinet.Siemens.SiemensPLCS;
|
||||
import HslCommunication.Profinet.Siemens.SiemensS7Net;
|
||||
import com.ruoyi.common.domain.SendData;
|
||||
import com.ruoyi.device.ddj.domain.MsgDdj;
|
||||
import com.ruoyi.device.ssj.domain.DeviceSsj;
|
||||
import com.ruoyi.common.domain.HslRead;
|
||||
import com.ruoyi.common.domain.PlcConnectInfo;
|
||||
import com.ruoyi.schedule.plcRead.util.ParseUtil;
|
||||
import com.ruoyi.schedule.plcRead.util.Tools;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Slf4j
|
||||
public class PlcSsjRead implements Runnable {
|
||||
|
||||
public static ConcurrentHashMap<String, DeviceSsj> deviceSsjMap;
|
||||
public volatile boolean isRun = true;
|
||||
|
||||
public SiemensS7Net siemensS7Net = null;
|
||||
public static IReadWriteNet readWriteNet = null;
|
||||
private final String ip;
|
||||
private final int port;
|
||||
private boolean success = false;
|
||||
|
||||
public PlcSsjRead(ConcurrentHashMap<String, DeviceSsj> deviceSsjMap, PlcConnectInfo plcConnectInfo) {
|
||||
PlcSsjRead.deviceSsjMap = deviceSsjMap;
|
||||
this.ip = plcConnectInfo.getIp();
|
||||
this.port = plcConnectInfo.getPort();
|
||||
}
|
||||
|
||||
public static int WriteToSSJ(String address, byte[] data) {
|
||||
int a = 0;
|
||||
if (readWriteNet != null) {
|
||||
a = HslRead.WriteResultRender(readWriteNet.Write(address, data), address);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (isRun) {
|
||||
try {
|
||||
if (IsConnect()) {
|
||||
int step = 20;
|
||||
int baseOffset = 0;
|
||||
for (String ssjCode : deviceSsjMap.keySet()) {
|
||||
DeviceSsj ssjDevice = deviceSsjMap.get(ssjCode);
|
||||
switch (ssjDevice.getType()) {
|
||||
case "1":
|
||||
readPlcLineSsj(ssjDevice);
|
||||
break;
|
||||
case "2":
|
||||
readPlcInOutSsj(ssjDevice);
|
||||
break;
|
||||
case "3":
|
||||
readPlcOtherSsj(ssjDevice);
|
||||
break;
|
||||
case "4":
|
||||
readPlcRoadWaySsj(ssjDevice);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
} else {
|
||||
reConnect();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
// 捕获中断异常,退出循环
|
||||
Thread.currentThread().interrupt(); // 重新设置中断标志
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
log.info("********IP:" + this.ip + " port:" + this.port + "读取数据异常 " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 其他输送机
|
||||
*
|
||||
* @param ssjDevice
|
||||
*/
|
||||
private void readPlcOtherSsj(DeviceSsj ssjDevice) {
|
||||
OperateResultExOne<byte[]> read2 = readWriteNet.Read(ssjDevice.getPlcDbReadAddress(), Short.parseShort("20"));
|
||||
String ddsx1 = "0";
|
||||
if (read2.IsSuccess) {
|
||||
ddsx1 = SoftBasic.ByteToHexString(read2.Content);
|
||||
ssjDevice.setKx(ParseUtil.optInt(0, ddsx1));
|
||||
ssjDevice.setDw(ParseUtil.optInt(2, ddsx1));
|
||||
ssjDevice.setTaskNo(ParseUtil.optDInt(6, ddsx1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 巷道输送机
|
||||
*
|
||||
* @param ssjDevice
|
||||
*/
|
||||
private void readPlcRoadWaySsj(DeviceSsj ssjDevice) {
|
||||
OperateResultExOne<byte[]> read2 = readWriteNet.Read(ssjDevice.getPlcDbReadAddress(), Short.parseShort("20"));
|
||||
String ddsx1 = "0";
|
||||
if (read2.IsSuccess) {
|
||||
ddsx1 = SoftBasic.ByteToHexString(read2.Content);
|
||||
ssjDevice.setKx(ParseUtil.optInt(0, ddsx1));
|
||||
ssjDevice.setDw(ParseUtil.optInt(2, ddsx1));
|
||||
ssjDevice.setRkqq(ParseUtil.optInt(4, ddsx1));
|
||||
ssjDevice.setTaskNo(ParseUtil.optDInt(6, ddsx1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 入出库输送机
|
||||
*
|
||||
* @param ssjDevice
|
||||
*/
|
||||
private void readPlcInOutSsj(DeviceSsj ssjDevice) {
|
||||
OperateResultExOne<byte[]> read2 = readWriteNet.Read(ssjDevice.getPlcDbReadAddress(), Short.parseShort("20"));
|
||||
String ddsx1 = "0";
|
||||
if (read2.IsSuccess) {
|
||||
ddsx1 = SoftBasic.ByteToHexString(read2.Content);
|
||||
ssjDevice.setKx(ParseUtil.optInt(0, ddsx1));
|
||||
ssjDevice.setDw(ParseUtil.optInt(2, ddsx1));
|
||||
ssjDevice.setException(ParseUtil.optInt(4, ddsx1));
|
||||
ssjDevice.setAllowPut(ParseUtil.optInt(6, ddsx1));
|
||||
ssjDevice.setTaskNo(ParseUtil.optDInt(8, ddsx1));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 产线输送机
|
||||
*
|
||||
* @param ssjDevice
|
||||
*/
|
||||
private void readPlcLineSsj(DeviceSsj ssjDevice) {
|
||||
OperateResultExOne<byte[]> read2 = readWriteNet.Read(ssjDevice.getPlcDbReadAddress(), Short.parseShort("220"));
|
||||
String ddsx1 = "0";
|
||||
if (read2.IsSuccess) {
|
||||
ddsx1 = SoftBasic.ByteToHexString(read2.Content);
|
||||
ssjDevice.setKx(ParseUtil.optInt(0, ddsx1));
|
||||
ssjDevice.setDw(ParseUtil.optInt(2, ddsx1));
|
||||
ssjDevice.setRkqq(ParseUtil.optInt(4, ddsx1));
|
||||
ssjDevice.setTraySize(ParseUtil.optInt(6, ddsx1));
|
||||
ssjDevice.setTaskNo(ParseUtil.optDInt(8, ddsx1));
|
||||
|
||||
String trayCodeReadAddress="";
|
||||
switch (ssjDevice.getDeviceCode()){
|
||||
case "G1005":
|
||||
trayCodeReadAddress = "DB502.0";
|
||||
break;
|
||||
case "L2012":
|
||||
trayCodeReadAddress = "DB503.0";
|
||||
break;
|
||||
case "L2013":
|
||||
trayCodeReadAddress = "DB504.0";
|
||||
break;
|
||||
}
|
||||
OperateResultExOne<byte[]> trayCodeRead = readWriteNet.Read(trayCodeReadAddress, Short.parseShort("220"));
|
||||
if (trayCodeRead.IsSuccess){
|
||||
byte[] content = trayCodeRead.Content;
|
||||
//去除bytes中
|
||||
ssjDevice.setTrayCode(ParseUtil.filterAndConvert(trayCodeRead.Content));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ConnectServer() {
|
||||
siemensS7Net = new SiemensS7Net(SiemensPLCS.S1500);
|
||||
try {
|
||||
siemensS7Net.setIpAddress(this.ip);
|
||||
siemensS7Net.setPort(this.port);
|
||||
siemensS7Net.setRack((byte) 0);
|
||||
siemensS7Net.setSlot((byte) 1);
|
||||
|
||||
OperateResult connect = siemensS7Net.ConnectServer();
|
||||
if (connect.IsSuccess) {
|
||||
setIsConnect(true);
|
||||
readWriteNet = siemensS7Net;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.info("********IP:" + this.ip + " port:" + this.port + "连接失败 " + ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setIsConnect(boolean bflag) {
|
||||
if (!success) {
|
||||
success = bflag;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean IsConnect() {
|
||||
if (success) {
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void reConnect() {
|
||||
closeServer();
|
||||
ConnectServer();
|
||||
log.info("重新连接 SSJ-PLC");
|
||||
}
|
||||
|
||||
void closeServer() {
|
||||
if (readWriteNet != null) {
|
||||
readWriteNet.ConnectClose();
|
||||
}
|
||||
setIsConnect(false);
|
||||
}
|
||||
|
||||
public static int writeToSsj(String address, String taskNo, int qx , int completed) {
|
||||
|
||||
int jym = Tools.getJYM(Integer.parseInt(taskNo), qx, completed);
|
||||
SendData taskNoSendData = new SendData(Tools.INT, taskNo);
|
||||
SendData qxSendData = new SendData(Tools.INT, qx);
|
||||
SendData completedSendData = new SendData(Tools.INT, completed);
|
||||
SendData jymSendData = new SendData(Tools.INT, jym);
|
||||
byte[] bytes = Tools.sendData(taskNoSendData, qxSendData, completedSendData, jymSendData);
|
||||
return PlcSsjRead.WriteToSSJ(address, bytes);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.ruoyi.schedule.plcRead.util;
|
||||
|
||||
|
||||
import HslCommunication.BasicFramework.SoftBasic;
|
||||
|
||||
/**
|
||||
* @author zn kai
|
||||
* @create 2021-06-28 14:04
|
||||
*/
|
||||
public class ParseUtil {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* short 16λתint
|
||||
* @param start
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static int optInt(int start, String data) {
|
||||
return Integer.parseInt(data.substring(start * 2, (start + 2) * 2), 16);
|
||||
}
|
||||
/**
|
||||
* int 16λתint
|
||||
* @param start
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static int optDInt(int start, String data) {
|
||||
return Integer.parseInt(data.substring(start * 2, (start + 4) * 2), 16);
|
||||
}
|
||||
/**
|
||||
* long 16λתlong
|
||||
* @param start
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public static Long optLong(int start, String data) {
|
||||
return Long.parseLong(data.substring(start * 2, (start + 8) * 2), 16);
|
||||
}
|
||||
/**
|
||||
* string 16λתstring
|
||||
* @param start
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static String optString(int start, String data) {
|
||||
byte[] bytes = SoftBasic.HexStringToBytes(data.substring(start * 2, (start + 7) * 2));
|
||||
return Tools.bytetostringtool(bytes);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 过滤ASCII数组中的无效字符,只保留可打印字符(32-126)
|
||||
* @param asciiArray 原始ASCII码数组
|
||||
* @return 转换后的字符串
|
||||
*/
|
||||
public static String filterAndConvert(byte[] asciiArray) {
|
||||
if (asciiArray == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int code : asciiArray) {
|
||||
// 只保留可打印ASCII字符(32-空格到126-~)
|
||||
if (code >= 32 && code <= 126) {
|
||||
sb.append((char) code);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,403 @@
|
||||
package com.ruoyi.schedule.plcRead.util;
|
||||
|
||||
import HslCommunication.BasicFramework.SoftBasic;
|
||||
import com.ruoyi.common.domain.SendData;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 描述:cyc16校验码
|
||||
*
|
||||
* @author zhu
|
||||
*/
|
||||
public class Tools implements Serializable {
|
||||
public static int Debug = 0x20;
|
||||
public final static int DEBUG_CONVERSIONS = 0x1000;
|
||||
public static final int DINT = 4;
|
||||
|
||||
public static final int INT = 2;
|
||||
|
||||
public static void main(String[] arg) {
|
||||
int jym = getJYM(1, 2, 3);
|
||||
System.out.println(jym);
|
||||
}
|
||||
|
||||
/*4、CRC-16校验码计算方法:
|
||||
常用查表法和计算法。计算方法一般都是:
|
||||
(1)、预置1个16位的寄存器为十六进制FFFF(即全为1),称此寄存器为CRC寄存器;
|
||||
(2)、把第一个8位二进制数据(既通讯信息帧的第一个字节)与16位的CRC寄存器的低
|
||||
8位相异或,把结果放于CRC寄存器,高八位数据不变;
|
||||
(3)、把CRC寄存器的内容右移一位(朝低位)用0填补最高位,并检查右移后的移出位;
|
||||
(4)、如果移出位为0:重复第3步(再次右移一位);如果移出位为1,CRC寄存器与多
|
||||
项式A001(1010 0000 0000 0001)进行异或;
|
||||
(5)、重复步骤3和4,直到右移8次,这样整个8位数据全部进行了处理;
|
||||
(6)、重复步骤2到步骤5,进行通讯信息帧下一个字节的处理;
|
||||
(7)、将该通讯信息帧所有字节按上述步骤计算完成后,得到的16位CRC寄存器的高、低
|
||||
字节进行交换;
|
||||
(8)、最后得到的CRC寄存器内容即为:CRC码。
|
||||
以上计算步骤中的多项式A001是8005按位颠倒后的结果。
|
||||
查表法是将移位异或的计算结果做成了一个表,就是将0~256放入一个长度为16位的寄存器中的低八位,高八位填充0,然后将该寄存器与多项式0XA001按照上述3、4步骤,直到八位全部移出,最后寄存器中的值就是表格中的数据,高八位、低八位分别单独一个表。*/
|
||||
|
||||
|
||||
/**
|
||||
* 全部校验码可用这个获取
|
||||
*
|
||||
* @param ints 传入的校验码
|
||||
* @return 返回的int
|
||||
*/
|
||||
public static int getJYM(int... ints) {
|
||||
//将整形任务号转为32位,能后转为四个字节,最后转为整形组4
|
||||
int[] ints1 = new int[ints.length * 4];
|
||||
for (int i = 0; i < ints.length; i++) {
|
||||
int[] son;
|
||||
son = binaryTints(printBits(ints[i]));
|
||||
for (int j = 0; j < 4; j++) {
|
||||
int s = j + i * 4;
|
||||
ints1[s] = son[j];
|
||||
System.out.println(Arrays.toString(ints1));
|
||||
}
|
||||
}
|
||||
int a = 0;
|
||||
for (int j : ints1) {
|
||||
a = a + j;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param hex 字符串
|
||||
* @return a 整型
|
||||
*/
|
||||
public static int[] binaryTints(String hex) {
|
||||
int[] a = new int[4];
|
||||
String hexString = change32(hex);
|
||||
a[0] = Integer.parseInt(hexString.substring(0, 8), 2);//将
|
||||
a[1] = Integer.parseInt(hexString.substring(8, 16), 2);
|
||||
a[2] = Integer.parseInt(hexString.substring(16, 24), 2);
|
||||
a[3] = Integer.parseInt(hexString.substring(24, 32), 2);
|
||||
return a;
|
||||
}
|
||||
|
||||
public static int[] binaryTints16(String hex) {
|
||||
int[] a = new int[2];
|
||||
String hexString = change16(hex);
|
||||
a[0] = Integer.parseInt(hexString.substring(0, 8), 2);//将二进制字符串转为整形
|
||||
a[1] = Integer.parseInt(hexString.substring(8, 16), 2);
|
||||
return a;
|
||||
}
|
||||
|
||||
public static int binaryTints8(String hex) {
|
||||
int a;
|
||||
String hexString = change8(hex);
|
||||
a = Integer.parseInt(hexString.substring(0, 8), 2);//将二进制字符串转为整形
|
||||
return a;
|
||||
}
|
||||
|
||||
public static int BytesToInt(int[] ss) {
|
||||
if (ss != null) {
|
||||
StringBuilder lString = new StringBuilder();
|
||||
for (int s : ss) {
|
||||
lString.append(change8(printBits(s)));
|
||||
}
|
||||
return Integer.parseInt(lString.toString(), 2);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符转为整形
|
||||
**/
|
||||
public static int showAscii(char x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将整形转为字符
|
||||
**/
|
||||
public static char showChar(int x) {
|
||||
return (char) x;
|
||||
}
|
||||
|
||||
/**
|
||||
* 托盘条码解析
|
||||
**/
|
||||
public static String byteToStringTool(byte[] aaa) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (byte b : aaa) {
|
||||
String x = Character.toString(showChar(b));
|
||||
builder.append(x);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将整形转为二进制字符串
|
||||
*/
|
||||
public static String printBits(int n) {
|
||||
return Integer.toBinaryString(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将整形转为浮点型
|
||||
*/
|
||||
public static Float printFloat(int n) {
|
||||
return Float.intBitsToFloat(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不满8位加高位加0
|
||||
**/
|
||||
public static String change8(String value) {
|
||||
if (value != null && !value.equals("")) {
|
||||
StringBuilder ss = new StringBuilder(value);
|
||||
for (int i = 0; i < 8 - value.length(); i++) {
|
||||
ss.insert(0, "0");
|
||||
}
|
||||
return ss.toString();
|
||||
} else {
|
||||
return "00000000";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 不满32位加高位加0 4个字节
|
||||
**/
|
||||
public static String change32(String value) {
|
||||
if (value != null && !value.equals("")) {
|
||||
StringBuilder ss = new StringBuilder(value);
|
||||
for (int i = 0; i < 32 - value.length(); i++) {
|
||||
ss.insert(0, "0");
|
||||
}
|
||||
return ss.toString();
|
||||
} else {
|
||||
return "00000000000000000000000000000000";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 不满16位加高位加0 4个字节
|
||||
**/
|
||||
public static String change16(String value) {
|
||||
if (value != null && !value.equals("")) {
|
||||
StringBuilder ss = new StringBuilder(value);
|
||||
for (int i = 0; i < 16 - value.length(); i++) {
|
||||
ss.insert(0, "0");
|
||||
}
|
||||
return ss.toString();
|
||||
} else {
|
||||
return "0000000000000000";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///字节转整型
|
||||
public static int get(byte a1, byte a2, byte a3, byte a4, byte a5, byte a6, byte a7, byte a8,
|
||||
byte a9, byte a10, byte a11, byte a12, byte a13, byte a14, byte a15, byte a16,
|
||||
byte a17, byte a18, byte a19, byte a20, byte a21, byte a22, byte a23, byte a24, byte a25) {
|
||||
int[] ints = new int[25];
|
||||
ints[0] = binaryTints8(printBits(a1));
|
||||
ints[1] = binaryTints8(printBits(a2));
|
||||
ints[2] = binaryTints8(printBits(a3));
|
||||
ints[3] = binaryTints8(printBits(a4));
|
||||
ints[4] = binaryTints8(printBits(a5));
|
||||
ints[5] = binaryTints8(printBits(a6));
|
||||
ints[6] = binaryTints8(printBits(a7));
|
||||
ints[7] = binaryTints8(printBits(a8));
|
||||
ints[8] = binaryTints8(printBits(a9));
|
||||
ints[9] = binaryTints8(printBits(a10));
|
||||
ints[10] = binaryTints8(printBits(a11));
|
||||
ints[11] = binaryTints8(printBits(a12));
|
||||
ints[12] = binaryTints8(printBits(a13));
|
||||
ints[13] = binaryTints8(printBits(a14));
|
||||
ints[14] = binaryTints8(printBits(a15));
|
||||
ints[15] = binaryTints8(printBits(a16));
|
||||
ints[16] = binaryTints8(printBits(a17));
|
||||
ints[17] = binaryTints8(printBits(a18));
|
||||
ints[18] = binaryTints8(printBits(a19));
|
||||
ints[19] = binaryTints8(printBits(a20));
|
||||
ints[20] = binaryTints8(printBits(a21));
|
||||
ints[21] = binaryTints8(printBits(a22));
|
||||
ints[22] = binaryTints8(printBits(a23));
|
||||
ints[23] = binaryTints8(printBits(a24));
|
||||
ints[24] = binaryTints8(printBits(a25));
|
||||
for (int anInt : ints) {
|
||||
System.out.print(anInt + " ");
|
||||
}
|
||||
System.out.println();
|
||||
return BytesToInt(ints);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//
|
||||
public static byte[] int2byte(int res) {///Java中10进制转byte[]
|
||||
byte[] targets = new byte[4];
|
||||
|
||||
targets[3] = (byte) (res & 0xff);// 最低位
|
||||
targets[2] = (byte) ((res >> 8) & 0xff);// 次低位
|
||||
targets[1] = (byte) ((res >> 16) & 0xff);// 次高位
|
||||
targets[0] = (byte) (res >>> 24);// 最高位,无符号右移。
|
||||
return targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多个byte 合并成一个byte
|
||||
*/
|
||||
public static byte[] byteMerge(byte[]... values) {
|
||||
int length_byte = 0;
|
||||
for (byte[] value : values) {
|
||||
length_byte += value.length;
|
||||
}
|
||||
byte[] all_byte = new byte[length_byte];
|
||||
int countLength = 0;
|
||||
for (byte[] b : values) {
|
||||
System.arraycopy(b, 0, all_byte, countLength, b.length);
|
||||
countLength += b.length;
|
||||
}
|
||||
return all_byte;
|
||||
}
|
||||
|
||||
/** ???????????**/
|
||||
public static String bytetostringtool(byte[] aaa) {
|
||||
String tptm = "";
|
||||
for(int i=0;i<aaa.length;i++){
|
||||
//System.out.println("??????:for:"+aaa[i]);
|
||||
String x = Character.toString(showChar(aaa[i]));
|
||||
char ddd=showChar(aaa[i]);
|
||||
tptm=tptm+x;
|
||||
//System.out.println(tptm);
|
||||
}
|
||||
if(tptm.length() == 0 || tptm.equals("")){
|
||||
return "00000000";
|
||||
}else {
|
||||
return tptm;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* int 转byte
|
||||
*/
|
||||
public static byte[] intToBytes(int value, int len) {
|
||||
byte[] b = new byte[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
b[len - i - 1] = (byte) ((value >> 8 * i) & 0xff);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//下发字节数组
|
||||
public static byte[] sendDataToDevice(long taskNo,String trayCode, int...ints){
|
||||
int length = 0;
|
||||
byte[] xfData = null;
|
||||
if (trayCode == null){
|
||||
length = 4 + (ints.length * 2);
|
||||
xfData = new byte[length];
|
||||
byte[] RWH = new byte[4];
|
||||
RWH = Tools.int2byte((int) taskNo);
|
||||
for (int i = 0; i < length; i++){
|
||||
if (i < 4){
|
||||
xfData[i] = RWH[i];
|
||||
} else {
|
||||
if (i % 2 == 0){
|
||||
byte[] param = new byte[2];
|
||||
param = intbyte(ints[(i - 4) / 2]);
|
||||
xfData[i] = param[0];
|
||||
xfData[i + 1] = param[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
length = 12 + (ints.length * 2);
|
||||
xfData = new byte[length];
|
||||
byte[] RWH = new byte[4];
|
||||
RWH = Tools.int2byte((int) taskNo);
|
||||
byte[] tCode = new byte[8];
|
||||
if ("".equals(trayCode)){
|
||||
for (int i = 0;i < tCode.length;i++){
|
||||
tCode[i] = 0;
|
||||
}
|
||||
}else {
|
||||
byte[] code = trayCode.getBytes();
|
||||
for (int i = 0; i < tCode.length; i++) {
|
||||
tCode[i] = code[i];
|
||||
}
|
||||
}
|
||||
for (int i = 0; i< length; i++){
|
||||
if (i < 4){
|
||||
xfData[i] = RWH[i];
|
||||
} else if (i > 3 && i< 12) {
|
||||
xfData[i] = tCode[i - 4];
|
||||
} else {
|
||||
if (i % 2 == 0){
|
||||
byte[] param = new byte[2];
|
||||
param = intbyte(ints[(i - 12) / 2]);
|
||||
xfData[i] = param[0];
|
||||
xfData[i + 1] = param[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return xfData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字节数组转字符船
|
||||
*/
|
||||
public static String byteToString(String substring) {
|
||||
return new String(SoftBasic.HexStringToBytes(substring), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
|
||||
public static String getDate() {
|
||||
Date t = new Date();
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return df.format(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下发到PLC的反馈字节
|
||||
*/
|
||||
public static byte[] sendData(SendData... list) {
|
||||
byte[][] values = new byte[list.length][0];
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
byte[] bytes = Tools.intToBytes(list[i].getNum2(), list[i].getNum());
|
||||
//System.out.println(bytes.length);
|
||||
values[i] = bytes;
|
||||
}
|
||||
int length_byte = 0;
|
||||
for (byte[] value : values) {
|
||||
length_byte += value.length;
|
||||
}
|
||||
byte[] all_byte = new byte[length_byte];
|
||||
int countLength = 0;
|
||||
for (byte[] b : values) {
|
||||
System.arraycopy(b, 0, all_byte, countLength, b.length);
|
||||
countLength += b.length;
|
||||
}
|
||||
return all_byte;
|
||||
}
|
||||
|
||||
//int 转2字节
|
||||
public static byte[] intbyte(int res) {///Java中10进制转byte[]
|
||||
byte[] targets = new byte[2];
|
||||
targets[1] = (byte) ((res >> 0) & 0xff);// 次高位
|
||||
targets[0] = (byte) (res >>> 8);// 最高位,无符号右移。
|
||||
return targets;
|
||||
}
|
||||
|
||||
public static int[] binarysToints(String hex){
|
||||
int[] a = new int[4];
|
||||
String hexString = change32(hex);
|
||||
a[0] = Integer.parseInt(hexString.substring(0,8),2);//将二进制字符串转为整形
|
||||
a[1] = Integer.parseInt(hexString.substring(8,16),2);
|
||||
a[2] = Integer.parseInt(hexString.substring(16,24),2);
|
||||
a[3] = Integer.parseInt(hexString.substring(24,32),2);
|
||||
return a;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user