示例#1
0
        /// <summary>
        /// Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format.
        /// </summary>
        /// <param name="varType"></param>
        /// <param name="bytes"></param>
        /// <param name="varCount"></param>
        /// <param name="bitAdr"></param>
        /// <returns></returns>
        private object ParseBytes(VarType varType, byte[] bytes, int varCount, byte bitAdr = 0)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return(null);
            }

            switch (varType)
            {
            case VarType.Byte:
                if (varCount == 1)
                {
                    return(bytes[0]);
                }
                else
                {
                    return(bytes);
                }

            case VarType.Word:
                if (varCount == 1)
                {
                    return(Word.FromByteArray(bytes));
                }
                else
                {
                    return(Word.ToArray(bytes));
                }

            case VarType.Int:
                if (varCount == 1)
                {
                    return(Int.FromByteArray(bytes));
                }
                else
                {
                    return(Int.ToArray(bytes));
                }

            case VarType.DWord:
                if (varCount == 1)
                {
                    return(DWord.FromByteArray(bytes));
                }
                else
                {
                    return(DWord.ToArray(bytes));
                }

            case VarType.DInt:
                if (varCount == 1)
                {
                    return(DInt.FromByteArray(bytes));
                }
                else
                {
                    return(DInt.ToArray(bytes));
                }

            case VarType.Real:
                if (varCount == 1)
                {
                    return(Types.Single.FromByteArray(bytes));
                }
                else
                {
                    return(Types.Single.ToArray(bytes));
                }

            case VarType.String:
                return(Types.String.FromByteArray(bytes));

            case VarType.StringEx:
                return(StringEx.FromByteArray(bytes));

            case VarType.Timer:
                if (varCount == 1)
                {
                    return(Timer.FromByteArray(bytes));
                }
                else
                {
                    return(Timer.ToArray(bytes));
                }

            case VarType.Counter:
                if (varCount == 1)
                {
                    return(Counter.FromByteArray(bytes));
                }
                else
                {
                    return(Counter.ToArray(bytes));
                }

            case VarType.Bit:
                if (varCount == 1)
                {
                    if (bitAdr > 7)
                    {
                        return(null);
                    }
                    else
                    {
                        return(Bit.FromByte(bytes[0], bitAdr));
                    }
                }
                else
                {
                    return(Bit.ToBitArray(bytes));
                }

            default:
                return(null);
            }
        }
示例#2
0
        private void SendPackage(IDVPMaster modbus, Device dv, DataBlock db)
        {
            try
            {
                SendDone.WaitOne(-1);

                switch (db.DataType)
                {
                case "Bit":

                    byte[] bitArys = null;
                    switch (db.TypeOfRead)
                    {
                    case "ReadCoilStatus":
                        bitArys = modbus.ReadCoilStatus((byte)dv.SlaveId, $"{db.Tags[0].Address}",
                                                        db.Length);
                        break;

                    case "ReadInputStatus":
                        bitArys = modbus.ReadInputStatus((byte)dv.SlaveId, $"{db.Tags[0].Address}",
                                                         db.Length);
                        break;

                    default:
                        break;
                    }
                    var BitRs = Bit.ToArray(bitArys);
                    // _RequestAndResponseMessage = new RequestAndResponseMessage("ResponseRead", BitRs);

                    if (BitRs.Length > db.Tags.Count)
                    {
                        return;
                    }
                    for (var j = 0; j < BitRs.Length; j++)
                    {
                        db.Tags[j].Value        = BitRs[j];
                        db.Tags[j].Checked      = BitRs[j];
                        db.Tags[j].Enabled      = BitRs[j];
                        db.Tags[j].Visible      = BitRs[j];
                        db.Tags[j].ValueSelect1 = BitRs[j];
                        db.Tags[j].ValueSelect2 = BitRs[j];
                        db.Tags[j].Timestamp    = DateTime.Now;
                    }
                    break;

                case "Byte":
                    var BytArys = modbus.ReadInputStatus((byte)dv.SlaveId, $"{db.Tags[0].Address}",
                                                         db.Length);
                    var BytRs = Bit.ToArray(BytArys);
                    //_RequestAndResponseMessage = new RequestAndResponseMessage("ResponseRead", BytRs);

                    if (BytRs.Length > db.Tags.Count)
                    {
                        return;
                    }
                    for (var j = 0; j < BytRs.Length; j++)
                    {
                        db.Tags[j].Value        = BytRs[j];
                        db.Tags[j].Checked      = BytRs[j];
                        db.Tags[j].Enabled      = BytRs[j];
                        db.Tags[j].Visible      = BytRs[j];
                        db.Tags[j].ValueSelect1 = BytRs[j];
                        db.Tags[j].ValueSelect2 = BytRs[j];
                        db.Tags[j].Timestamp    = DateTime.Now;
                    }
                    break;

                case "Int":
                    var IntArys = modbus.ReadHoldingRegisters((byte)dv.SlaveId,
                                                              $"{db.Tags[0].Address}", db.Length);
                    var IntRs = Int.ToArray(IntArys);
                    if (IntRs.Length > db.Tags.Count)
                    {
                        return;
                    }
                    for (var j = 0; j < IntRs.Length; j++)
                    {
                        db.Tags[j].Value     = IntRs[j];
                        db.Tags[j].Timestamp = DateTime.Now;
                    }
                    break;

                case "DInt":
                    var DIntArys = modbus.ReadHoldingRegisters((byte)dv.SlaveId,
                                                               $"{db.Tags[0].Address}", db.Length);
                    var DIntRs = Int.ToArray(DIntArys);
                    if (DIntRs.Length > db.Tags.Count)
                    {
                        return;
                    }
                    for (var j = 0; j < DIntRs.Length; j++)
                    {
                        db.Tags[j].Value     = DIntRs[j];
                        db.Tags[j].Timestamp = DateTime.Now;
                    }
                    break;

                case "Word":

                    byte[] wdArys = null;
                    switch (db.TypeOfRead)
                    {
                    case "ReadHoldingRegisters":
                        wdArys = modbus.ReadHoldingRegisters((byte)dv.SlaveId, $"{db.Tags[0].Address}", db.Length);

                        break;

                    case "ReadInputRegisters":
                        wdArys = modbus.ReadInputRegisters((byte)dv.SlaveId, $"{db.Tags[0].Address}", db.Length);

                        break;

                    default:
                        break;
                    }
                    // Array.Reverse(wdArys);

                    var wdRs = Word.ToArray(wdArys);
                    //_RequestAndResponseMessage = new RequestAndResponseMessage("ResponseRead", dresult);
                    if (wdRs.Length > db.Tags.Count)
                    {
                        return;
                    }
                    for (var j = 0; j < wdRs.Length; j++)
                    {
                        db.Tags[j].Value     = wdRs[j];
                        db.Tags[j].Timestamp = DateTime.Now;
                    }
                    break;

                case "DWord":
                    var dwArys = modbus.ReadHoldingRegisters((byte)dv.SlaveId,
                                                             $"{db.Tags[0].Address}", db.Length);
                    var dwRs = DWord.ToArray(dwArys);
                    for (var j = 0; j < dwRs.Length; j++)
                    {
                        db.Tags[j].Value     = dwRs[j];
                        db.Tags[j].Timestamp = DateTime.Now;
                    }
                    break;

                case "Real":
                    var rl1Arys = modbus.ReadHoldingRegisters((byte)dv.SlaveId,
                                                              $"{db.Tags[0].Address}", db.Length);
                    var rl1Rs = Real.ToArray(rl1Arys);
                    for (var j = 0; j < rl1Rs.Length; j++)
                    {
                        db.Tags[j].Value     = rl1Rs[j];
                        db.Tags[j].Timestamp = DateTime.Now;
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                IsConnected = false;
                EventscadaException?.Invoke(this.GetType().Name, ex.Message);
            }
        }