| | |
| | | package com.zy.core.thread; |
| | | |
| | | import com.zy.common.HslCommunication.Core.Types.OperateResult; |
| | | import com.zy.common.HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import com.zy.common.HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import HslCommunication.Core.Types.OperateResult; |
| | | import HslCommunication.Core.Types.OperateResultExOne; |
| | | import HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.zy.core.Slave; |
| | | import com.zy.core.ThreadHandler; |
| | | import com.zy.core.cache.MessageQueue; |
| | |
| | | switch (step) { |
| | | // 读数据 |
| | | case 1: |
| | | readWorkNo(); // 读取工作号 |
| | | readStaDest(); // 读取目标站 |
| | | readStatus(); // 读取状态 |
| | | readError(); // 读取异常信息 |
| | | read(); // 读取状态 |
| | | break; |
| | | case 2: |
| | | break; |
| | | case 3: |
| | | break; |
| | | case 4: |
| | | break; |
| | | default: |
| | | break; |
| | |
| | | public boolean connect() { |
| | | boolean result = false; |
| | | siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp()); |
| | | siemensS7Net.setRack((byte) 0); |
| | | siemensS7Net.setSlot((byte) 1); |
| | | OperateResult connect = siemensS7Net.ConnectServer(); |
| | | if(connect.IsSuccess){ |
| | | result = true; |
| | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 读取工作号 |
| | | */ |
| | | private void readWorkNo(){ |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 读取目标站 |
| | | */ |
| | | private void readStaDest(){ |
| | | |
| | | public static void main(String[] args) { |
| | | Slave slave = new Slave(); |
| | | slave.setIp("192.168.2.125"); |
| | | DevpThread devpThread = new DevpThread(slave); |
| | | boolean connect = devpThread.connect(); |
| | | System.out.println(connect); |
| | | devpThread.read(); |
| | | // 写 |
| | | StaProtocol staProtocol = devpThread.getStation().get(1); |
| | | staProtocol.setWorkNo((short) 666); |
| | | devpThread.write(staProtocol); |
| | | // 读 |
| | | devpThread.read(); |
| | | System.out.println(JSON.toJSONString(devpThread.station)); |
| | | } |
| | | |
| | | /** |
| | | * 读取状态 |
| | | */ |
| | | private void readStatus(){ |
| | | |
| | | private void read(){ |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read("DB100.0", (short) 40); |
| | | for (int i = 1; i <= 7; i++) { |
| | | StaProtocol staProtocol = station.get(i); |
| | | if (null == staProtocol) { |
| | | staProtocol = new StaProtocol(); |
| | | staProtocol.setSiteId(i); |
| | | station.put(i, staProtocol); |
| | | } |
| | | System.out.println(siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2)); |
| | | staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2)); |
| | | staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2+20)); |
| | | // staProtocol.setAutoing(siemensS7Net.getByteTransform().TransBool()); |
| | | } |
| | | // todo |
| | | // OperateResultExOne<Short> shortOperateResultExOne = siemensS7Net.ReadInt16("DB100.0"); |
| | | // System.out.println(shortOperateResultExOne.Content); |
| | | } |
| | | |
| | | /** |
| | | * 读取异常信息 |
| | | */ |
| | | private void readError(){ |
| | | |
| | | |
| | | |
| | | private void write(StaProtocol staProtocol){ |
| | | if (null == staProtocol) { |
| | | return; |
| | | } |
| | | OperateResult write = siemensS7Net.Write("DB100." + (staProtocol.getSiteId() - 1) * 2, staProtocol.getWorkNo()); |
| | | if (write.IsSuccess) { |
| | | System.out.println("写入成功"); |
| | | } else { |
| | | System.out.println("写入失败"); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void close() { |