Beispiel #1
0
        public ErrorCode 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(ErrorCode.WrongNumberReceivedBytes.ToString());
                }

                return(ErrorCode.NoError);
            }
            catch
            {
                lastErrorCode   = ErrorCode.WriteData;
                lastErrorString = "";
                return(lastErrorCode);
            }
        }
Beispiel #2
0
        public byte[] ReadBytes(DataType DataType, int DB, int StartByteAdr, int count)
        {
            //Console.WriteLine("I " + DateTime.Now.ToString("HH:mm:ss.fff") + " : " + Thread.CurrentThread.ManagedThreadId.ToString());

            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(ErrorCode.WrongNumberReceivedBytes.ToString());
                }
                if (bReceive[16] - 4 != count)
                {
                    throw new Exception("Receive Error numReceived->" + numReceived);
                }

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

                //Console.WriteLine("O " + DateTime.Now.ToString("HH:mm:ss.fff") + " : " + Thread.CurrentThread.ManagedThreadId.ToString());
                return(bytes);
            }
            catch (Exception ex)
            {
                Close();
                lastErrorCode   = ErrorCode.WriteData;
                lastErrorString = "";
                return(null);
            }
        }