Ejemplo n.º 1
0
        public ExceptionCode WriteBytes(DataType DataType, int DB, int StartByteAdr, byte[] value)
        {
            byte[] bReceive = new byte[513];
            int    varCount = 0;

            try
            {
                varCount = value.Length;
                // first create the header
                int             packageSize = 35 + value.Length;
                Types.ByteArray package     = new Types.ByteArray(packageSize);

                package.Add(new byte[] { 3, 0, 0 });
                package.Add((byte)packageSize);
                package.Add(new byte[] { 2, 0xf0, 0x80, 0x32, 1, 0, 0 });
                package.Add(Types.Word.ToByteArray((ushort)(varCount - 1)));
                package.Add(new byte[] { 0, 0x0e });
                package.Add(Types.Word.ToByteArray((ushort)(varCount + 4)));
                package.Add(new byte[] { 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02 });
                package.Add(Types.Word.ToByteArray((ushort)varCount));
                package.Add(Types.Word.ToByteArray((ushort)(DB)));
                package.Add((byte)DataType);
                package.Add((byte)0);
                package.Add(Types.Word.ToByteArray((ushort)(StartByteAdr * 8)));
                package.Add(new byte[] { 0, 4 });
                package.Add(Types.Word.ToByteArray((ushort)(varCount * 8)));

                // now join the header and the data
                package.Add(value);

                mSocket.Send(package.array, package.array.Length, SocketFlags.None);

                int numReceived = mSocket.Receive(bReceive, 512, SocketFlags.None);
                if (bReceive[21] != 0xff)
                {
                    throw new Exception(ExceptionCode.WrongNumberReceivedBytes.ToString());
                }

                return(ExceptionCode.ExceptionNo);
            }
            catch
            {
                lastErrorCode   = ExceptionCode.WriteData;
                lastErrorString = "";
                return(lastErrorCode);
            }
        }
Ejemplo n.º 2
0
        public byte[] ReadBytes(DataType DataType, int DB, int StartByteAdr, int count)
        {
            byte[] bytes = new byte[count];

            try
            {
                // first create the header
                int             packageSize = 31;
                Types.ByteArray package     = new Types.ByteArray(packageSize);

                package.Add(new byte[] { 0x03, 0x00, 0x00 });
                package.Add((byte)packageSize);
                package.Add(new byte[] { 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00,
                                         0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12,
                                         0x0a, 0x10 });
                // package.Add(0x02);  // datenart
                switch (DataType)
                {
                case DataType.Timer:
                case DataType.Counter:
                    package.Add((byte)DataType);
                    break;

                default:
                    package.Add(0x02);
                    break;
                }

                package.Add(Types.Word.ToByteArray((ushort)(count)));
                package.Add(Types.Word.ToByteArray((ushort)(DB)));
                package.Add((byte)DataType);
                package.Add((byte)0);
                switch (DataType)
                {
                case DataType.Timer:
                case DataType.Counter:
                    package.Add(Types.Word.ToByteArray((ushort)(StartByteAdr)));
                    break;

                default:
                    package.Add(Types.Word.ToByteArray((ushort)((StartByteAdr) * 8)));
                    break;
                }

                mSocket.Send(package.array, package.array.Length, SocketFlags.None);

                byte[] bReceive    = new byte[512];
                int    numReceived = mSocket.Receive(bReceive, 512, SocketFlags.None);
                if (bReceive[21] != 0xff)
                {
                    throw new Exception(ExceptionCode.WrongNumberReceivedBytes.ToString());
                }

                for (int cnt = 0; cnt < count; cnt++)
                {
                    bytes[cnt] = bReceive[cnt + 25];
                }

                return(bytes);
            }
            catch
            {
                lastErrorCode   = ExceptionCode.WriteData;
                lastErrorString = "";
                return(null);
            }
        }