wcs项目初始化
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
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.device.ssj.domain.DeviceSsj;
|
||||
import com.ruoyi.schedule.plcRead.ssj.domain.HslRead;
|
||||
import com.ruoyi.schedule.plcRead.ssj.domain.PlcConnectInfo;
|
||||
import com.ruoyi.schedule.plcRead.util.ParseUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.ss.formula.functions.Offset;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Slf4j
|
||||
public class PlcRead implements Runnable {
|
||||
|
||||
public static ConcurrentHashMap<String, DeviceSsj> deviceMap;
|
||||
public volatile boolean isRun = true;
|
||||
|
||||
public SiemensS7Net siemensS7Net = null;
|
||||
public static IReadWriteNet readWriteNet = null;
|
||||
private String ip;
|
||||
private int port;
|
||||
private boolean success = false;
|
||||
|
||||
public PlcRead(ConcurrentHashMap<String, DeviceSsj> deviceMap, PlcConnectInfo plcConnectInfo) {
|
||||
this.deviceMap = deviceMap;
|
||||
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 : deviceMap.keySet()) {
|
||||
DeviceSsj ssjDevice = deviceMap.get(ssjCode);
|
||||
switch (ssjDevice.getType()) {
|
||||
case "1":
|
||||
readPlcOutSsj(ssjDevice);
|
||||
break;
|
||||
case "2":
|
||||
readPlcPickSsj(ssjDevice);
|
||||
break;
|
||||
case "3":
|
||||
readPlcInSsj(ssjDevice);
|
||||
break;
|
||||
case "4":
|
||||
readPlcOtherSsj(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.setRkqq(ParseUtil.optInt(4, ddsx1));
|
||||
ssjDevice.setTaskNo(ParseUtil.optDInt(6, ddsx1));
|
||||
ssjDevice.setTrayCode(ParseUtil.optString(10, ddsx1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 入库输送机
|
||||
*
|
||||
* @param ssjDevice
|
||||
*/
|
||||
private void readPlcInSsj(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));
|
||||
ssjDevice.setTrayCode(ParseUtil.optString(12, ddsx1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拣选口输送机
|
||||
*
|
||||
* @param ssjDevice
|
||||
*/
|
||||
private void readPlcPickSsj(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));
|
||||
ssjDevice.setTrayCode(ParseUtil.optString(10, ddsx1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 出库输送机
|
||||
*
|
||||
* @param ssjDevice
|
||||
*/
|
||||
private void readPlcOutSsj(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));
|
||||
ssjDevice.setTrayCode(ParseUtil.optString(10, ddsx1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ruoyi.schedule.plcRead.ssj.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class CustomThread<T> {
|
||||
public Thread thread;
|
||||
public T runnable;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.ruoyi.schedule.plcRead.ssj.domain;
|
||||
|
||||
import HslCommunication.BasicFramework.SoftBasic;
|
||||
import HslCommunication.Core.Net.IReadWriteNet;
|
||||
import HslCommunication.Core.Types.OperateResult;
|
||||
import HslCommunication.Core.Types.OperateResultExOne;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class HslRead {
|
||||
|
||||
private static SimpleDateFormat dateFormat = new SimpleDateFormat("[HH:mm:ss] ");
|
||||
|
||||
/**
|
||||
* 将结果信息显示出来,在界面里进行显示
|
||||
* @param result 等待显示的结果信息
|
||||
* @param x
|
||||
* @param address 读取的地址数据
|
||||
* @param textBox 显示结果的文本框
|
||||
* @param scrollPane 支持滚动条的控件
|
||||
* @param <T> 类型对象
|
||||
* @return
|
||||
* @return
|
||||
* @return
|
||||
* @return
|
||||
* @return
|
||||
*/
|
||||
public static <T> T ReadResultRender(OperateResultExOne<T> result ) {
|
||||
|
||||
T x = null ;
|
||||
if (result.IsSuccess) {
|
||||
if (result.Content.getClass().isArray()) {
|
||||
|
||||
} else {
|
||||
x= (T) result.Content;
|
||||
}
|
||||
} else {
|
||||
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
dateFormat.format(new Date()) + "] Read Failed\r\nReason:" + result.ToMessageShowString(),
|
||||
"Result",
|
||||
JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
public static int WriteResultRender(OperateResult result, String address ) { //void
|
||||
int x=0;
|
||||
if (result.IsSuccess) {
|
||||
// JOptionPane.showMessageDialog(
|
||||
// null,
|
||||
// dateFormat.format(new Date()) + "[" + address + "] Write Success",
|
||||
// "Result",
|
||||
// JOptionPane.PLAIN_MESSAGE);
|
||||
x=1;
|
||||
} else {
|
||||
// JOptionPane.showMessageDialog(
|
||||
// null,
|
||||
// dateFormat.format(new Date()) + "[" + address + "] Write Failed\r\nReason:" + result.ToMessageShowString(),
|
||||
// "Result",
|
||||
// JOptionPane.WARNING_MESSAGE);
|
||||
x=2;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
public static void BulkReadRenderResult(IReadWriteNet readWrite, JTextField addTextBox, JTextField lengthTextBox, JTextArea resultTextBox ) {
|
||||
try {
|
||||
OperateResultExOne<byte[]> read = readWrite.Read(addTextBox.getText(), Short.parseShort(lengthTextBox.getText()));
|
||||
if (read.IsSuccess) {
|
||||
resultTextBox.setText("Result:" + SoftBasic.ByteToHexString(read.Content));
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Read Failed:" + read.ToMessageShowString(), "Result", JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, "Read Failed:" + ex.getMessage(), "Result", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetPanelEnabled(JPanel panel, boolean enable) {
|
||||
Component[] components = panel.getComponents();
|
||||
|
||||
//System.out.println("Component Length:" + components.length);
|
||||
for (Component component : components) {
|
||||
if (component instanceof JPanel) {
|
||||
SetPanelEnabled((JPanel) component, enable);
|
||||
} else {
|
||||
component.setEnabled(enable);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String IpAddressInputWrong = "IpAddress input wrong";
|
||||
public static String PortInputWrong = "Port input wrong";
|
||||
public static String SlotInputWrong = "Slot input wrong";
|
||||
public static String BaudRateInputWrong = "Baud rate input wrong";
|
||||
public static String DataBitsInputWrong = "Data bit input wrong";
|
||||
public static String StopBitInputWrong = "Stop bit input wrong";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ruoyi.schedule.plcRead.ssj.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
public class PlcConnectInfo {
|
||||
|
||||
private String ip;
|
||||
private Integer port;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.ruoyi.schedule.plcRead.ssj.domain;
|
||||
|
||||
/**
|
||||
* 发送数据的实体类
|
||||
* @num 长度
|
||||
* @num2 值
|
||||
*/
|
||||
public class SendData {
|
||||
private static final int DINT = 4;
|
||||
private static final int INT = 2;
|
||||
private int num;
|
||||
private int num2;
|
||||
|
||||
public SendData(int num, int num2) {
|
||||
this.num = num;
|
||||
this.num2 = num2;
|
||||
}
|
||||
|
||||
public SendData(int num, String num2) {
|
||||
this.num = num;
|
||||
this.num2 = Integer.parseInt(num2);
|
||||
}
|
||||
|
||||
public int getNum() {
|
||||
return num;
|
||||
}
|
||||
|
||||
public void setNum(int num) {
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
public int getNum2() {
|
||||
return num2;
|
||||
}
|
||||
|
||||
public void setNum2(int num2) {
|
||||
this.num2 = num2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
package com.ruoyi.schedule.plcRead.util;
|
||||
|
||||
import HslCommunication.BasicFramework.SoftBasic;
|
||||
import com.ruoyi.schedule.plcRead.ssj.domain.SendData;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD>:cyc16У<36><D0A3><EFBFBD><EFBFBD>
|
||||
*
|
||||
* @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<><34>CRC-16У<36><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷽<EFBFBD><E3B7BD><EFBFBD><EFBFBD>
|
||||
<20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>ͼ<EFBFBD><CDBC>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD>㷽<EFBFBD><E3B7BD>һ<EFBFBD>㶼<EFBFBD>ǣ<EFBFBD>
|
||||
<20><>1<EFBFBD><31><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4>1<EFBFBD><31>16λ<36>ļĴ<C4BC><C4B4><EFBFBD>Ϊʮ<CEAA><CAAE><EFBFBD><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>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD>λΪ0<CEAA><30><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><D9B4><EFBFBD><EFBFBD><EFBFBD>һλ<D2BB><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD>λΪ1<CEAA><31>CRC<52>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
<20><>ʽA001<30><31>1010 0000 0000 0001<30><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
<20><>5<EFBFBD><35><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><33>4<EFBFBD><34>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8<EFBFBD>Σ<EFBFBD><CEA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8λ<38><CEBB><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˴<EFBFBD><CBB4><EFBFBD>
|
||||
<20><>6<EFBFBD><36><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5<EFBFBD><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨѶ<CDA8><D1B6>Ϣ֡<CFA2><D6A1>һ<EFBFBD><D2BB><EFBFBD>ֽڵĴ<DAB5><C4B4><EFBFBD>
|
||||
<20><>7<EFBFBD><37><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ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FBD><30>Ȼ<EFBFBD>üĴ<C3BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ0XA001<30><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><33>4<EFBFBD><34><EFBFBD>裬ֱ<E8A3AC><D6B1><EFBFBD><EFBFBD>λȫ<CEBB><C8AB><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>е<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>DZ<EFBFBD><C7B1><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3>߰<EFBFBD>λ<EFBFBD><CEBB><EFBFBD>Ͱ<EFBFBD>λ<EFBFBD>ֱ<D6B1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>*/
|
||||
|
||||
|
||||
/**
|
||||
* ȫ<><C8AB>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
||||
*
|
||||
* @param ints <20><><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
|
||||
* @return <20><><EFBFBD>ص<EFBFBD>int
|
||||
*/
|
||||
public static int getJYM(int... ints) {
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תΪ32λ<32><CEBB><EFBFBD>ܺ<EFBFBD>תΪ<D7AA>ĸ<EFBFBD><C4B8>ֽڣ<D6BD><DAA3><EFBFBD><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>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 <20>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
* @return a <20><><EFBFBD><EFBFBD>
|
||||
*/
|
||||
public static int[] binaryTints(String hex) {
|
||||
int[] a = new int[4];
|
||||
String hexString = change32(hex);
|
||||
a[0] = Integer.parseInt(hexString.substring(0, 8), 2);//<2F><>
|
||||
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);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD>
|
||||
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);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD>ַ<EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD>
|
||||
**/
|
||||
public static int showAscii(char x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תΪ<D7AA>ַ<EFBFBD>
|
||||
**/
|
||||
public static char showChar(int x) {
|
||||
return (char) x;
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
**/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
*/
|
||||
public static String printBits(int n) {
|
||||
return Integer.toBinaryString(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
*/
|
||||
public static Float printFloat(int n) {
|
||||
return Float.intBitsToFloat(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD>8λ<38>Ӹ<EFBFBD>λ<EFBFBD><CEBB>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";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD>32λ<32>Ӹ<EFBFBD>λ<EFBFBD><CEBB>0 4<><34><EFBFBD>ֽ<EFBFBD>
|
||||
**/
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD>16λ<36>Ӹ<EFBFBD>λ<EFBFBD><CEBB>0 4<><34><EFBFBD>ֽ<EFBFBD>
|
||||
**/
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///<2F>ֽ<EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
|
||||
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<76><61>10<31><30><EFBFBD><EFBFBD>תbyte[]
|
||||
byte[] targets = new byte[4];
|
||||
|
||||
targets[3] = (byte) (res & 0xff);// <20><><EFBFBD>λ
|
||||
targets[2] = (byte) ((res >> 8) & 0xff);// <20>ε<EFBFBD>λ
|
||||
targets[1] = (byte) ((res >> 16) & 0xff);// <20>θ<EFBFBD>λ
|
||||
targets[0] = (byte) (res >>> 24);// <20><><EFBFBD>λ,<2C><EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD>
|
||||
return targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD>byte <20>ϲ<EFBFBD><CFB2><EFBFBD>һ<EFBFBD><D2BB>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++){
|
||||
String x = Character.toString(showChar(aaa[i]));
|
||||
char ddd=showChar(aaa[i]);
|
||||
tptm=tptm+x;
|
||||
}
|
||||
if(tptm.isEmpty() || "\u0000\u0000\u0000\u0000\u0000\u0000\u0000".equals(tptm)){
|
||||
return "";
|
||||
}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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//<2F>·<EFBFBD><C2B7>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* <20>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
//int ת2<D7AA>ֽ<EFBFBD>
|
||||
public static byte[] intbyte(int res) {///Java<76><61>10<31><30><EFBFBD><EFBFBD>תbyte[]
|
||||
byte[] targets = new byte[2];
|
||||
targets[1] = (byte) ((res >> 0) & 0xff);// <20>θ<EFBFBD>λ
|
||||
targets[0] = (byte) (res >>> 8);// <20><><EFBFBD>λ,<2C><EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD>
|
||||
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);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>תΪ<D7AA><CEAA><EFBFBD><EFBFBD>
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下发到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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user