/** Reads multiple data tags from PLC. (only S7 is currently supported) */ public byte[][] Read(String[] addr) { if (!connected) { return(null); } for (int a = 0; a < addr.Length; a++) { addr[a] = addr[a].ToUpperCase(); } switch (plc) { case ControllerType.S7: { S7Data[] s7 = new S7Data[addr.Length]; for (int a = 0; a < addr.Length; a++) { s7[a] = S7Packet.decodeAddress(addr[a]); } byte[] packet = S7Packet.makeReadPacket(s7); try { socket.Write(packet); } catch (Exception e) { lastException = e; return(null); } byte[] reply = new byte[1500]; int replySize = 0; try { do { int read = socket.Read(reply, replySize, 1500 - replySize); if (read == -1) { throw new Exception("bad read"); } replySize += read; } while (!S7Packet.isPacketComplete(Arrays <byte> .CopyOf(reply, replySize))); } catch (Exception e) { lastException = e; return(null); } s7 = S7Packet.decodeMultiPacket(Arrays <byte> .CopyOf(reply, replySize), addr.Length); byte[][] ret = new byte[addr.Length][]; for (int a = 0; a < addr.Length; a++) { ret[a] = s7[a].data; } return(ret); } /* * case ControllerType.MODBUS: { * ModAddr ma = ModPacket.decodeAddress(addr); * byte[] packet = ModPacket.makeReadPacket(ma); * try { * socket.Write(packet); * } catch (Exception e) { * lastException = e; * return null; * } * byte[] reply = new byte[1500]; * int replySize = 0; * try { * do { * int read = socket.Read(reply, replySize, 1500 - replySize); * if (read == -1) throw new Exception("bad read"); * replySize += read; * } while (!ModPacket.isPacketComplete(Arrays<byte>.CopyOf(reply, replySize))); * } catch (Exception e) { * lastException = e; * return null; * } * ModData data = ModPacket.decodePacket(Arrays<byte>.CopyOf(reply, replySize)); * return data.data; * } * case AB: { * byte[] packet = ABPacket.makeReadPacket(addr, ab_context); * try { * socket.Write(packet); * } catch (Exception e) { * lastException = e; * return null; * } * byte[] reply = new byte[1500]; * int replySize = 0; * try { * do { * int read = socket.Read(reply, replySize, 1500 - replySize); * if (read == -1) throw new Exception("bad read"); * replySize += read; * } while (!ABPacket.isPacketComplete(Arrays<byte>.CopyOf(reply, replySize))); * return ABPacket.decodePacket(reply); * } catch (Exception e) { * lastException = e; * return null; * } * } */ case ControllerType.JF: { JFTag[] tags = new JFTag[addr.Length]; for (int a = 0; a < addr.Length; a++) { tags[a] = JFPacket.decodeAddress(addr[a]); } byte[] packet = JFPacket.makeReadPacket(tags); try { socket.Write(packet); } catch (Exception e) { lastException = e; return(null); } byte[] reply = new byte[1500]; int replySize = 0; try { do { int read = socket.Read(reply, replySize, 1500 - replySize); if (read == -1) { throw new Exception("bad read"); } replySize += read; } while (!JFPacket.isPacketComplete(Arrays <byte> .CopyOf(reply, replySize))); } catch (Exception e) { lastException = e; return(null); } tags = JFPacket.decodeMultiPacket(Arrays <byte> .CopyOf(reply, replySize), addr.Length); byte[][] ret = new byte[addr.Length][]; for (int a = 0; a < addr.Length; a++) { ret[a] = tags[a].data; } return(ret); } } return(null); }
/** Reads data from PLC. */ public byte[] Read(String addr) { addr = addr.ToUpperCase(); lock (tlock) { if (!connected) { return(null); } switch (plc) { case ControllerType.S7: { S7Data s7 = S7Packet.decodeAddress(addr); if (s7 == null) { return(null); } byte[] packet = S7Packet.makeReadPacket(s7); try { socket.Write(packet); } catch (Exception e) { lastException = e; return(null); } byte[] reply = new byte[1500]; int replySize = 0; try { do { int read = socket.Read(reply, replySize, 1500 - replySize); if (read == -1) { throw new Exception("bad read"); } replySize += read; } while (!S7Packet.isPacketComplete(Arrays <byte> .CopyOf(reply, replySize))); } catch (Exception e) { lastException = e; return(null); } s7 = S7Packet.decodePacket(Arrays <byte> .CopyOf(reply, replySize)); return(s7.data); } case ControllerType.MB: { ModAddr ma = ModPacket.decodeAddress(addr); byte[] packet = ModPacket.makeReadPacket(ma); try { socket.Write(packet); } catch (Exception e) { lastException = e; return(null); } byte[] reply = new byte[1500]; int replySize = 0; try { do { int read = socket.Read(reply, replySize, 1500 - replySize); if (read == -1) { throw new Exception("bad read"); } replySize += read; } while (!ModPacket.isPacketComplete(Arrays <byte> .CopyOf(reply, replySize))); } catch (Exception e) { lastException = e; return(null); } ModData data = ModPacket.decodePacket(Arrays <byte> .CopyOf(reply, replySize)); return(data.data); } case ControllerType.AB: { byte[] packet = ABPacket.makeReadPacket(addr, ab_context); try { socket.Write(packet); } catch (Exception e) { lastException = e; return(null); } byte[] reply = new byte[1500]; int replySize = 0; try { do { int read = socket.Read(reply, replySize, 1500 - replySize); if (read == -1) { throw new Exception("bad read"); } replySize += read; } while (!ABPacket.isPacketComplete(Arrays <byte> .CopyOf(reply, replySize))); return(ABPacket.decodePacket(reply)); } catch (Exception e) { lastException = e; return(null); } } case ControllerType.JF: { JFTag tag = JFPacket.decodeAddress(addr); if (tag == null) { return(null); } byte[] packet = JFPacket.makeReadPacket(tag); try { socket.Write(packet); } catch (Exception e) { lastException = e; return(null); } byte[] reply = new byte[1500]; int replySize = 0; try { do { int read = socket.Read(reply, replySize, 1500 - replySize); if (read == -1) { throw new Exception("bad read"); } replySize += read; } while (!JFPacket.isPacketComplete(Arrays <byte> .CopyOf(reply, replySize))); } catch (Exception e) { lastException = e; return(null); } tag = JFPacket.decodePacket(Arrays <byte> .CopyOf(reply, replySize)); return(tag.data); } } } return(null); }