Exemplo n.º 1
0
        /** 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);
        }
Exemplo n.º 2
0
        /** Writes data to PLC.
         *
         * datatype is required for AB controllers.
         */
        public bool Write(String addr, byte[] data, DataType type)
        {
            addr = addr.ToUpperCase();
            lock (tlock) {
                if (!connected)
                {
                    return(false);
                }
                switch (plc)
                {
                case ControllerType.S7: {
                    S7Data s7 = S7Packet.decodeAddress(addr);
                    s7.data = data;
                    byte[] packet = S7Packet.makeWritePacket(s7);
                    try {
                        socket.Write(packet);
                    } catch (Exception e) {
                        lastException = e;
                        return(false);
                    }
                    return(true);
                }

                case ControllerType.MB: {
                    ModAddr ma = ModPacket.decodeAddress(addr);
                    ma.state = data[0] != 0;
                    byte[] packet = ModPacket.makeWritePacket(ma);
                    try {
                        socket.Write(packet);
                    } catch (Exception e) {
                        lastException = e;
                        return(false);
                    }
                    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(false);
                    }
                    return(true);
                }

                case ControllerType.AB: {
                    if (type == DataType.ANY)
                    {
                        return(false);
                    }
                    byte[] packet = ABPacket.makeWritePacket(addr, ABPacket.getType(type), data, ab_context);
                    try {
                        socket.Write(packet);
                    } catch (Exception e) {
                        lastException = e;
                        return(false);
                    }
                    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)));
                    } catch (Exception e) {
                        lastException = e;
                        return(false);
                    }
                    return(true);
                }

                case ControllerType.JF: {
                    JFTag tag = JFPacket.decodeAddress(addr);
                    tag.data = data;
                    byte[] packet = JFPacket.makeWritePacket(tag, data);
                    try {
                        socket.Write(packet);
                    } catch (Exception e) {
                        lastException = e;
                        return(false);
                    }
                    return(true);
                }
                }
            }
            return(false);
        }