BuildInfoObj(
            Int32 asdu,
            Int32 addr,
            Double value,
            Boolean sbo               = false,
            Int32 cmdqualif           = 0,
            QualityDescriptor quality = null,
            CP56Time2a time_tag       = null,
            Double kconv1             = 1,
            Double kconv2             = 0,
            Boolean transient         = false
            )
        {
            InformationObject sc = null;

            if (time_tag == null)
            {
                time_tag = new CP56Time2a(DateTime.Now);
            }
            else
            { // has time tag, so change ASDU if necessary to embed a timetag
                var maptowithcp56time = new Dictionary <TypeID, TypeID>();
                maptowithcp56time.Add(TypeID.M_SP_NA_1, TypeID.M_SP_TB_1);
                maptowithcp56time.Add(TypeID.M_DP_NA_1, TypeID.M_DP_TB_1);
                maptowithcp56time.Add(TypeID.M_ST_NA_1, TypeID.M_ST_TB_1);
                maptowithcp56time.Add(TypeID.M_BO_NA_1, TypeID.M_BO_TB_1);
                maptowithcp56time.Add(TypeID.M_ME_NA_1, TypeID.M_ME_TD_1);
                maptowithcp56time.Add(TypeID.M_ME_NB_1, TypeID.M_ME_TE_1);
                maptowithcp56time.Add(TypeID.M_ME_NC_1, TypeID.M_ME_TF_1);
                maptowithcp56time.Add(TypeID.M_IT_NA_1, TypeID.M_IT_TB_1);
                if (maptowithcp56time.TryGetValue((TypeID)asdu, out var newasdu))
                {
                    asdu = (Int32)newasdu;
                }
            }

            Boolean bval;
            Int32   ival;
            UInt32  uival;

            switch ((TypeID)asdu)
            {
            case TypeID.M_SP_NA_1:     // 1
                bval = value != 0 ? true : false;
                if (kconv1 == -1)
                {
                    bval = !bval;
                }
                sc =
                    new SinglePointInformation(addr,
                                               bval,
                                               quality);
                break;

            case TypeID.M_SP_TB_1:     // 30
                bval = value != 0 ? true : false;
                if (kconv1 == -1)
                {
                    bval = !bval;
                }
                sc =
                    new SinglePointWithCP56Time2a(addr,
                                                  bval,
                                                  quality,
                                                  time_tag
                                                  );
                break;

            case TypeID.M_DP_NA_1:     // 3
                if (transient)
                {
                    sc =
                        new DoublePointInformation(addr,
                                                   DoublePointValue.INTERMEDIATE,
                                                   quality);
                }
                else
                if (kconv1 == -1)
                {
                    sc =
                        new DoublePointInformation(addr,
                                                   value != 0 ? DoublePointValue.OFF : DoublePointValue.ON,
                                                   quality);
                }
                else
                {
                    sc =
                        new DoublePointInformation(addr,
                                                   value != 0 ? DoublePointValue.ON : DoublePointValue.OFF,
                                                   quality);
                }
                break;

            case TypeID.M_DP_TB_1:     // 31
                if (transient)
                {
                    sc =
                        new DoublePointWithCP56Time2a(addr,
                                                      DoublePointValue.INTERMEDIATE,
                                                      quality,
                                                      time_tag);
                }
                else
                if (kconv1 == -1)
                {
                    sc =
                        new DoublePointWithCP56Time2a(addr,
                                                      value != 0 ? DoublePointValue.OFF : DoublePointValue.ON,
                                                      quality,
                                                      time_tag);
                }
                else
                {
                    sc =
                        new DoublePointWithCP56Time2a(addr,
                                                      value != 0 ? DoublePointValue.ON : DoublePointValue.OFF,
                                                      quality,
                                                      time_tag);
                }
                break;

            case TypeID.M_ST_NA_1:     // 5
                value = value * kconv1 + kconv2;
                if (value > 63)
                {
                    value            = 63;
                    quality.Overflow = true;
                }
                else
                if (value < -64)
                {
                    value            = -64;
                    quality.Overflow = true;
                }
                sc = new StepPositionInformation(addr,
                                                 System.Convert.ToInt16(value),
                                                 transient,
                                                 quality);
                break;

            case TypeID.M_ST_TB_1:     // 32
                value = value * kconv1 + kconv2;
                if (value > 63)
                {
                    value            = 63;
                    quality.Overflow = true;
                }
                else
                if (value < -64)
                {
                    value            = -64;
                    quality.Overflow = true;
                }
                sc = new StepPositionWithCP56Time2a(addr,
                                                    System.Convert.ToInt16(value),
                                                    transient,
                                                    quality,
                                                    time_tag);
                break;

            case TypeID.M_ME_NA_1:     // 9
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueNormalized(addr,
                                                 System.Convert.ToInt16(value),
                                                 quality);
                break;

            case TypeID.M_ME_ND_1:     // 21
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueNormalizedWithoutQuality(addr,
                                                               System.Convert.ToInt16(value));
                break;

            case TypeID.M_ME_TD_1:     // 34
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueNormalizedWithCP56Time2a(addr,
                                                               System.Convert.ToInt16(value),
                                                               quality,
                                                               time_tag);
                break;

            case TypeID.M_ME_NB_1:     // 11
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueScaled(addr,
                                             System.Convert.ToInt16(value),
                                             quality);
                break;

            case TypeID.M_ME_TE_1:     // 35
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value            = 32767;
                    quality.Overflow = true;
                }
                else
                if (value < -32768)
                {
                    value            = -32768;
                    quality.Overflow = true;
                }
                sc = new MeasuredValueScaledWithCP56Time2a(addr,
                                                           System.Convert.ToInt16(value),
                                                           quality,
                                                           time_tag);
                break;

            case TypeID.M_ME_NC_1:     // 13
                value = value * kconv1 + kconv2;
                sc    = new MeasuredValueShort(addr,
                                               System.Convert.ToSingle(value),
                                               quality);
                break;

            case TypeID.M_ME_TF_1:     // 36
                value = value * kconv1 + kconv2;
                sc    = new MeasuredValueShortWithCP56Time2a(addr,
                                                             System.Convert.ToSingle(value),
                                                             quality,
                                                             time_tag);
                break;

            case TypeID.M_IT_NA_1:     //15
                break;

            case TypeID.M_IT_TB_1:     // 37
                break;

            case TypeID.M_PS_NA_1:     // 20
                Log(" TODO Packed single point information with status change detection! ");
                break;

            case TypeID.M_EP_TD_1:     // 38
                break;

            case TypeID.M_EP_TE_1:     // 39
                break;

            case TypeID.M_EP_TF_1:     // 40
                break;

            case TypeID.M_BO_NA_1:     // 7
                uival = System.Convert.ToUInt32(value);
                if (kconv1 == -1)
                {
                    uival = ~uival;
                }
                sc = new Bitstring32(addr, uival, quality);
                break;

            case TypeID.M_BO_TB_1:     // 33
                uival = System.Convert.ToUInt32(value);
                if (kconv1 == -1)
                {
                    uival = ~uival;
                }
                sc = new Bitstring32WithCP56Time2a(addr, uival, quality, time_tag);
                break;

            case TypeID.C_SC_NA_1:     // 45
                bval = value != 0 ? true : false;
                if (kconv1 == -1)
                {
                    bval = !bval;
                }
                sc =
                    new SingleCommand(addr,
                                      bval,
                                      sbo,
                                      cmdqualif);
                break;

            case TypeID.C_DC_NA_1:     // 46
                if (kconv1 == -1)
                {
                    ival = value != 0 ? System.Convert.ToInt32(DoublePointValue.OFF) : System.Convert.ToInt32(DoublePointValue.ON);
                }
                else
                {
                    ival = value != 0 ? System.Convert.ToInt32(DoublePointValue.ON) : System.Convert.ToInt32(DoublePointValue.OFF);
                }
                sc =
                    new DoubleCommand(addr,
                                      ival,
                                      sbo,
                                      cmdqualif);
                break;

            case TypeID.C_RC_NA_1:     // 47
                if (kconv1 == -1)
                {
                    sc =
                        new StepCommand(addr,
                                        value >= 1
                                    ? StepCommandValue.LOWER
                                    : StepCommandValue.HIGHER,
                                        sbo,
                                        cmdqualif);
                }
                else
                {
                    sc =
                        new StepCommand(addr,
                                        value >= 1
                                    ? StepCommandValue.HIGHER
                                    : StepCommandValue.LOWER,
                                        sbo,
                                        cmdqualif);
                }
                break;

            case TypeID.C_SE_NA_1:     // 48
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new SetpointCommandNormalized(addr,
                                                  System.Convert.ToInt16(value),
                                                  new SetpointCommandQualifier(sbo, 0));
                break;

            case TypeID.C_SE_NB_1:     // 49
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new SetpointCommandScaled(addr,
                                              new ScaledValue(System.Convert.ToInt16(value)),
                                              new SetpointCommandQualifier(sbo, 0));
                break;

            case TypeID.C_SE_NC_1:     // 50
                value = value * kconv1 + kconv2;
                sc    =
                    new SetpointCommandShort(addr,
                                             System.Convert.ToSingle(value),
                                             new SetpointCommandQualifier(sbo, 0));
                break;

            case TypeID.C_BO_NA_1:     // 51
                uival = System.Convert.ToUInt32(value);
                if (kconv1 == -1)
                {
                    uival = ~uival;
                }
                sc =
                    new Bitstring32Command(addr,
                                           uival);
                break;

            case TypeID.C_SC_TA_1:     //  58
                bval = value != 0 ? true : false;
                if (kconv1 == -1)
                {
                    bval = !bval;
                }
                sc =
                    new SingleCommandWithCP56Time2a(addr,
                                                    bval,
                                                    sbo,
                                                    cmdqualif,
                                                    time_tag);
                break;

            case TypeID.C_DC_TA_1:     // 59
                if (kconv1 == -1)
                {
                    ival = value != 0 ? System.Convert.ToInt32(DoublePointValue.OFF) : System.Convert.ToInt32(DoublePointValue.ON);
                }
                else
                {
                    ival = value != 0 ? System.Convert.ToInt32(DoublePointValue.ON) : System.Convert.ToInt32(DoublePointValue.OFF);
                }
                sc =
                    new DoubleCommandWithCP56Time2a(addr,
                                                    ival,
                                                    sbo,
                                                    cmdqualif,
                                                    time_tag);
                break;

            case TypeID.C_RC_TA_1:     // 60
                if (kconv1 == -1)
                {
                    sc =
                        new StepCommandWithCP56Time2a(addr,
                                                      value >= 1
                                    ? StepCommandValue.LOWER
                                    : StepCommandValue.HIGHER,
                                                      sbo,
                                                      cmdqualif,
                                                      time_tag);
                }
                else
                {
                    sc =
                        new StepCommandWithCP56Time2a(addr,
                                                      value >= 1
                                    ? StepCommandValue.HIGHER
                                    : StepCommandValue.LOWER,
                                                      sbo,
                                                      cmdqualif,
                                                      time_tag);
                }
                break;

            case TypeID.C_SE_TA_1:     // 61
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new SetpointCommandNormalizedWithCP56Time2a(addr,
                                                                System.Convert.ToInt16(value),
                                                                new SetpointCommandQualifier(sbo, 0),
                                                                time_tag);
                break;

            case TypeID.C_SE_TB_1:     // 62
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new SetpointCommandScaledWithCP56Time2a(addr,
                                                            new ScaledValue(System.Convert.ToInt16(value)),
                                                            new SetpointCommandQualifier(sbo, 0),
                                                            time_tag);
                break;

            case TypeID.C_SE_TC_1:     // 63
                value = value * kconv1 + kconv2;
                sc    =
                    new SetpointCommandShortWithCP56Time2a(addr,
                                                           System.Convert.ToSingle(value),
                                                           new SetpointCommandQualifier(sbo, 0),
                                                           time_tag);
                break;

            case TypeID.C_BO_TA_1:     // 64
                uival = System.Convert.ToUInt32(value);
                if (kconv1 == -1)
                {
                    uival = ~uival;
                }
                sc =
                    new Bitstring32CommandWithCP56Time2a(addr,
                                                         uival,
                                                         time_tag);
                break;

            case TypeID.C_IC_NA_1:     // 100
                sc =
                    new InterrogationCommand(0,
                                             System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.C_CI_NA_1:     // 101
                sc =
                    new CounterInterrogationCommand(0,
                                                    System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.C_RD_NA_1:     // 102
                sc = new ReadCommand(addr);
                break;

            case TypeID.C_CS_NA_1:     // 103
                sc = new ClockSynchronizationCommand(0, time_tag);
                break;

            case TypeID.C_RP_NA_1:     // 105
                sc =
                    new ResetProcessCommand(addr,
                                            System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.C_TS_TA_1:     // 107
                sc =
                    new TestCommandWithCP56Time2a(System
                                                  .Convert
                                                  .ToUInt16(value),
                                                  time_tag);
                break;

            case TypeID.P_ME_NA_1:     // 110
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new ParameterNormalizedValue(System
                                                 .Convert
                                                 .ToInt32(addr),
                                                 System.Convert.ToInt32(value),
                                                 System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.P_ME_NB_1:     // 111
                value = value * kconv1 + kconv2;
                if (value > 32767)
                {
                    value = 32767;
                }
                else
                if (value < -32768)
                {
                    value = -32768;
                }
                sc =
                    new ParameterScaledValue(addr,
                                             new ScaledValue(System.Convert.ToInt16(value)),
                                             System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.P_ME_NC_1:     // 112
                value = value * kconv1 + kconv2;
                sc    =
                    new ParameterFloatValue(addr,
                                            System.Convert.ToSingle(value),
                                            System.Convert.ToByte(cmdqualif));
                break;

            case TypeID.P_AC_NA_1:     // 113
                sc =
                    new ParameterActivation(System.Convert.ToInt32(addr),
                                            System.Convert.ToByte(cmdqualif));
                break;

            default:
                break;
            }
            return(sc);
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            /* Initialize data objects */
            for (int i = 0; i < 400; i++)
            {
                spiObjects [i] = new SinglePointInformation(1000 + i, true, new QualityDescriptor());
            }

            for (int i = 0; i < 100; i++)
            {
                stepPositionObjects [i] = new StepPositionWithCP56Time2a(10000 + i, 0, false,
                                                                         new QualityDescriptor(), new CP56Time2a());
            }


            bool running = true;

            Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) {
                e.Cancel = true;
                running  = false;
            };

            Server server = new Server();

            server.DebugOutput  = false;
            server.MaxQueueSize = 100;

            server.Start();

            int waitTime = 2000;

            while (running)
            {
                Thread.Sleep(100);

                if (waitTime > 0)
                {
                    waitTime -= 100;
                }
                else
                {
                    ASDU newAsdu = null;

                    /* send SPI objects */
                    for (int i = 0; i < 400; i++)
                    {
                        spiObjects [i].Value = !(spiObjects [i].Value);

                        if (newAsdu == null)
                        {
                            newAsdu = new ASDU(server.GetApplicationLayerParameters(), CauseOfTransmission.PERIODIC, false, false, 1, 1, false);
                        }

                        if (newAsdu.AddInformationObject(spiObjects [i]) == false)
                        {
                            server.EnqueueASDU(newAsdu);
                            newAsdu = null;
                            i--;
                        }
                    }

                    if (newAsdu != null)
                    {
                        server.EnqueueASDU(newAsdu);
                    }

                    /* send step position objects */
                    newAsdu = null;

                    for (int i = 0; i < 100; i++)
                    {
                        stepPositionObjects [i].Value = (stepPositionObjects [i].Value + 1) % 63;

                        if (newAsdu == null)
                        {
                            newAsdu = new ASDU(server.GetApplicationLayerParameters(), CauseOfTransmission.PERIODIC, false, false, 1, 1, false);
                        }

                        if (newAsdu.AddInformationObject(stepPositionObjects [i]) == false)
                        {
                            server.EnqueueASDU(newAsdu);
                            newAsdu = null;
                            i--;
                        }
                    }

                    if (newAsdu != null)
                    {
                        server.EnqueueASDU(newAsdu);
                    }

                    waitTime = 1000;
                }
            }

            Console.WriteLine("Stop server");
            server.Stop();
        }