Example #1
0
        public int WriteToggle(plcTag tag)
        {
            byte[] _tempBuffer = new byte[4];
            int    result      = -99;

            lock (PLCInterface.TimerLock)
            {
                if (Client.Connected())
                {
                    if (tag.VType == varType.BOOL)
                    {
                        if (tag.DType == dataType.DB)
                        {
                            result = Client.DBRead(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                            S7.SetBitAt(ref _tempBuffer, 0, tag.Offset.BitOffset, !S7.GetBitAt(_tempBuffer, 0, tag.Offset.BitOffset));
                            result += Client.DBWrite(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        }
                        if (tag.DType == dataType.Q)
                        {
                            result = Client.ABRead(tag.Offset.ByteOffset, 2, _tempBuffer);
                            S7.SetBitAt(ref _tempBuffer, 0, tag.Offset.BitOffset, !S7.GetBitAt(_tempBuffer, 0, tag.Offset.BitOffset));
                            result += Client.ABWrite(tag.Offset.ByteOffset, 2, _tempBuffer);
                        }
                    }
                }
                try
                {
                    if (result != 0)
                    {
                        throw new System.InvalidOperationException("write error");
                    }
                }
                catch { }
                finally
                {
                }
            }
            return(result);
        }
Example #2
0
        public int WriteTag(plcTag tag, object value)
        {
            byte[] _tempBuffer = new byte[4];
            int    result      = -99;

            lock (PLCInterface.TimerLock)
            {
                if (Client.Connected())
                {
                    switch (tag.VType)
                    {
                    case varType.BOOL:
                        result = Client.DBRead(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        S7.SetBitAt(ref _tempBuffer, 0, tag.Offset.BitOffset, (bool)value);
                        result += Client.DBWrite(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        break;

                    case varType.BYTE:
                        result = Client.DBRead(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        S7.SetByteAt(_tempBuffer, 0, (byte)value);
                        result += Client.DBWrite(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        break;

                    case varType.WORD:
                        result = Client.DBRead(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        S7.SetWordAt(_tempBuffer, 0, (ushort)value);
                        result += Client.DBWrite(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        break;

                    case varType.DWORD:
                        result = Client.DBRead(tag.DbNumber, tag.Offset.ByteOffset, 4, _tempBuffer);
                        S7.SetDWordAt(_tempBuffer, 0, (uint)value);
                        result += Client.DBWrite(tag.DbNumber, tag.Offset.ByteOffset, 4, _tempBuffer);
                        break;

                    case varType.INT:
                        result = Client.DBRead(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        S7.SetIntAt(_tempBuffer, 0, (short)value);
                        result += Client.DBWrite(tag.DbNumber, tag.Offset.ByteOffset, 2, _tempBuffer);
                        break;

                    case varType.DINT:
                        result = Client.DBRead(tag.DbNumber, tag.Offset.ByteOffset, 4, _tempBuffer);
                        S7.SetDIntAt(_tempBuffer, 0, (int)value);
                        result += Client.DBWrite(tag.DbNumber, tag.Offset.ByteOffset, 4, _tempBuffer);
                        break;

                    case varType.REAL:
                        result = Client.DBRead(tag.DbNumber, tag.Offset.ByteOffset, 4, _tempBuffer);
                        S7.SetRealAt(_tempBuffer, 0, (float)value);
                        result += Client.DBWrite(tag.DbNumber, tag.Offset.ByteOffset, 4, _tempBuffer);
                        break;
                    }
                }
                try
                {
                    if (result != 0)
                    {
                        throw new System.InvalidOperationException("write error");
                    }
                }
                catch { }
                finally
                {
                }
            }
            return(result);
        }