| | |
| | | /** |
| | | * 读取状态 ====> 整块plc |
| | | */ |
| | | private void read(){ |
| | | private void read() throws InterruptedException { |
| | | OperateResultExOne<byte[]> result = melsecMcNet.Read("D101", (short) 30); |
| | | if (result.IsSuccess) { |
| | | for (int i = 1; i <= 8; i++) { |
| | |
| | | staProtocol.setStaNo(melsecMcNet.getByteTransform().TransInt16(result.Content, (i-1)*2+40)); // 目标站 |
| | | } |
| | | } |
| | | Thread.sleep(100); |
| | | OperateResultExOne<boolean[]> result1 = melsecMcNet.ReadBool("M800", (short) 64); |
| | | if (result1.IsSuccess) { |
| | | for (int i = 1; i <= 8; i++) { |
| | |
| | | staProtocol.setInEnable(result1.Content[(i-1)*8+2]); // 可入 |
| | | staProtocol.setOutEnable(result1.Content[(i-1)*8+3]);// 可出 |
| | | staProtocol.setEmptyMk(result1.Content[(i-1)*8+4]); // 空板信号 |
| | | |
| | | if (!staProtocol.isPakMk() && !staProtocol.isLoading()) { |
| | | staProtocol.setPakMk(true); |
| | | } |
| | | } |
| | | } |
| | | if (result.IsSuccess && result1.IsSuccess) { |
| | |
| | | /** |
| | | * 写入 ID+目标站 =====> 单站点写入 |
| | | */ |
| | | private void write(StaProtocol staProtocol){ |
| | | private void write(StaProtocol staProtocol) throws InterruptedException { |
| | | if (null == staProtocol) { |
| | | return; |
| | | } |
| | | OperateResult write = melsecMcNet.Write("D10" + staProtocol.getSiteId(), staProtocol.getWorkNo()); // 工作号 |
| | | Thread.sleep(100); |
| | | OperateResult write1 = melsecMcNet.Write("D12" + staProtocol.getSiteId(), staProtocol.getStaNo()); // 目标站 |
| | | // boolean[] status = new boolean[8]; |
| | | // status[0] = staProtocol.isAutoing(); |
| | |
| | | // status[4] = staProtocol.isEmptyMk(); |
| | | // OperateResult write2 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) + 40), status); // 状态 |
| | | if (!write.IsSuccess || !write1.IsSuccess) { |
| | | if (staProtocol.getWorkNo() == 0 && staProtocol.getStaNo() ==0) { |
| | | staProtocol.setPakMk(true); |
| | | } |
| | | OutputQueue.DEVP.offer(MessageFormat.format("【{0}】写入输送线站点数据失败。输送线plc编号={1},站点数据={2}", slave.getId(), JSON.toJSON(staProtocol))); |
| | | log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol)); |
| | | } else { |
| | |
| | | melsecMcNet.ConnectClose(); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | public static void main(String[] args) throws Exception { |
| | | DevpSlave slave = new DevpSlave(); |
| | | slave.setIp("192.168.2.125"); |
| | | slave.setIp("192.168.3.65"); |
| | | slave.setPort(6000); |
| | | DevpThread devpThread = new DevpThread(slave); |
| | | devpThread.connect(); |
| | | devpThread.read(); |
| | | OperateResult d1021 = devpThread.melsecMcNet.Write("D102", (short) 234); |
| | | if (d1021.IsSuccess) { |
| | | System.out.println("success"); |
| | | } |
| | | OperateResultExOne<Short> d102 = devpThread.melsecMcNet.ReadInt16("D102"); |
| | | System.out.println(d102.Content); |
| | | // devpThread.read(); |
| | | System.out.println("第一次读"); |
| | | // 写 |
| | | StaProtocol staProtocol = devpThread.getStation().get(1); |
| | | staProtocol.setWorkNo((short) 232); |
| | | staProtocol.setStaNo((short) 6); |
| | | staProtocol.setAutoing(true); |
| | | staProtocol.setEmptyMk(true); |
| | | staProtocol.setInEnable(true); |
| | | devpThread.write(staProtocol); |
| | | System.out.println("----------------------------------------"); |
| | | // StaProtocol staProtocol = devpThread.getStation().get(1); |
| | | // staProtocol.setWorkNo((short) 232); |
| | | // staProtocol.setStaNo((short) 6); |
| | | // staProtocol.setAutoing(true); |
| | | // staProtocol.setEmptyMk(true); |
| | | // staProtocol.setInEnable(true); |
| | | // devpThread.write(staProtocol); |
| | | // System.out.println("----------------------------------------"); |
| | | Thread.sleep(400); |
| | | // 读 |
| | | devpThread.read(); |
| | | // devpThread.read(); |
| | | System.out.println("第二次读"); |
| | | System.out.println(JSON.toJSONString(devpThread.station)); |
| | | |
| | | } |