Code Monkey home page Code Monkey logo

iec-60870's Introduction

IEC-60870 is C# version of OpenMUC IEC-60870 library

Installation

A nuget package is available for the library. To install IEC60870 Library, run the following command in the Package Manager Console:

PM> Install-Package IEC60870

Examples (updated for version 1.2)

Client

Write your simple client application (master) like this

 var client = new ClientSAP("127.0.0.1", 2404);
 client.NewASdu += asdu => {
      // process received Asdu
      
      client.SendASdu(asdu);
  };

  client.ConnectionClosed += e =>
  {
      Console.WriteLine(e);      
  };

  client.Connect();

Server

and if you want to create server application (slave), you must use ServerSAP instead of ClientSAP

  var server = new ServerSAP("127.0.0.1", 2405); 
  server.StartListen(10);
  server.SendASdu(asdu);  
  
  server.NewASdu += asdu =>
  {
     Console.WriteLine(asdu);      
  };   

iec-60870's People

Contributors

minhdtb avatar minhdtb83 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

iec-60870's Issues

frame length not plausible

hello,

My analyzer says that the
'frame length not plausible"

is this a known problem?

using System;
using System.IO;
using System.ServiceProcess;
using System.Text;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using IEC60870.SAP;
using IEC60870.Enum;
using IEC60870.IE;
using IEC60870.IE.Base;
using IEC60870.Object;
using System.Timers;

namespace FPL104Service
{
public partial class FPL104Service : ServiceBase
{

    System.Timers.Timer timer = new System.Timers.Timer();

    MqttClient client;
    string clientId;

    string BrokerAddress = "127.0.0.1";
    
    private ServerSAP server;

    private int cnt = 1;

    public FPL104Service()
    {
        InitializeComponent();

 


    }

    protected override void OnStart(string[] args)
    {
        try
        {
            WriteToFile("Start 104 server " + DateTime.Now);
            //104 start en settings
            server = new ServerSAP("127.0.0.1", 2404);
            server.StartListen(10);
            
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            WriteToFile("Cannot start 104 rtu " + DateTime.Now);
        }


        WriteToFile("Service is started at " + DateTime.Now);
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer.Interval = 5000; //number in milisecinds  
        timer.Enabled = true;

        client = new MqttClient(BrokerAddress);

        // register a callback-function (we have to implement, see below) which is called by the library when a message was received
        client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

        // use a unique id as client id, each time we start the application
        clientId = Guid.NewGuid().ToString();

        client.Connect(clientId);

        string Topic = "FPL104/Test";

        // subscribe to the topic with QoS 2
        client.Subscribe(new string[] { Topic }, new byte[] { 2 });





        // this code runs when a message was received
        void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            string ReceivedMessage = Encoding.UTF8.GetString(e.Message);
            WriteToFile(ReceivedMessage);
            



        }

    }


    protected override void OnStop()
    {
        WriteToFile("Service is stopped at " + DateTime.Now);
        client.Disconnect();

    }

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        WriteToFile("Service is recall at " + DateTime.Now);
        InformationElement ielem_value1;
        if (cnt == 1)
        { 
            ielem_value1 = new IeShortFloat(1);
            
            cnt = 0;
        }
        else
        {
            ielem_value1 = new IeShortFloat(0);
            cnt = 1;
        }
        WriteToFile("Service is recall at " + cnt +" " + DateTime.Now);
        InformationElement ielem_quality1 = new IeQuality(false, false, false, false, false);
        InformationElement[][] ielem1 = new InformationElement[1][] { new InformationElement[2] { ielem_value1, ielem_quality1 } };
        InformationObject[] iobj1 = new InformationObject[1] { new InformationObject(1, ielem1) };
        ASdu asdu2 = new ASdu(TypeId.M_ME_NC_1, false, CauseOfTransmission.SPONTANEOUS, false, false, 1, 1, iobj1);
        //                                                                               common address,  IOA
        server.SendASdu(asdu2);


    }

another question. How do i set the iec time ?

I hope you can help me.

greets Rick

Bug in IeTime56.cs

I think there is a bug in the file IEC60870 --> IE --> IeTime56.cs. Why do you write in the methode below a "+1" for the month? When I use it, I'll get the wrong month.

public IeTime56(long timestamp, TimeZone timeZone, bool invalid)
{
...
 value[5] = (byte) (datetime.Month + 1);
...
}

send values

how to send to display IEC-60870-104 sir?

i'm running this app , just get value from display iec and i'cant send values to iec emulator .

Problems with negative values in IeNormalizedValue

In the last days my colleague and I tested the communication between my C#-Application using this library and his C++-Application using lib60870-5 (http://libiec61850.com/libiec61850/). During our tests we got problems with receiving values of type M_ME_NA_1 (normalized value / IeNormalizedValue). Every time he sends me negative values this results in exceptions.

After some debugging I was able to figure out what's the matter. I think there is a bug in IeNormalizedValue (Line 22):
Value = reader.ReadByte() | (reader.ReadByte() << 8);
The code handles two bytes (16 bit) and calculates an int (32 bit) where only 16 bits are used. The problem is that negative values in 16 bit representation are not handled correct this way - the result of negative 16 bit values is a number > 32767 in 32 bit (caused by the sign at the first bit).

I was able to fix it by replacing the line with the following code using BitConverter to create a short from the two bytes. This short has the correct value and sign an can be casted to int:

byte[] bytes = { reader.ReadByte(), reader.ReadByte()};
Value = BitConverter.ToInt16(bytes,0);

I tested it with QTester104 (https://sourceforge.net/projects/qtester104/) and lib60870-5 and it seems to work so I hope that this is correct. Please check it - hope that my code helps!

Multiple server-instances in a single application

I tried to use this library to simulate multiple battery storage power plants in a single application and provide their data via IEC 60870-5-104 protocol. The architecture / the server items of each battery should be exactly the same in order to simply integrate them into an existing plc. Each server listens to another port.

The problem is that every server-instance sends data via their own port and also via each other port defined in the other servers.

To make it easy to reproduce this behavior, I prepared a short example with two servers. server1 on port 2404 should only provide data on IOA 1 and server2 on port 2405 should only provide data on IOA 2. I receive both items on both ports.

Here’s my sample:

private ServerSAP server1;
private ServerSAP server2;
private void Start()
{
    try
    {
        server1 = new ServerSAP("127.0.0.1", 2404);
        server2 = new ServerSAP("127.0.0.1", 2405);

        server1.StartListen(100);
        server2.StartListen(101);
        
        timer.Enabled = true;
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
}

private int cnt = 0;
void TimerTick(object sender, EventArgs e)
{
    InformationElement ielem_value1 = new IeShortFloat(cnt);
    InformationElement ielem_quality1 = new IeQuality(false,false,false,false,false);
    InformationElement[][] ielem1 = new InformationElement[1][] { new InformationElement[2] { ielem_value1,ielem_quality1 } };
    InformationObject[] iobj1 = new InformationObject[1] { new InformationObject(1, ielem1) };
    ASdu asdu1 = new ASdu(TypeId.M_ME_NC_1, false, CauseOfTransmission.SPONTANEOUS, false, false, 1, 1, iobj1);
    
    InformationElement ielem_value2 = new IeShortFloat(cnt*2);
    InformationElement ielem_quality2 = new IeQuality(true,true,true,false,false);
    InformationElement[][] ielem2 = new InformationElement[1][] { new InformationElement[2] { ielem_value2,ielem_quality2 } };
    InformationObject[] iobj2 = new InformationObject[1] { new InformationObject(2, ielem2) };
    ASdu asdu2 = new ASdu(TypeId.M_ME_NC_1, false, CauseOfTransmission.SPONTANEOUS, false, false, 1, 1, iobj2);
    
    server1.SendASdu(asdu1);
    server2.SendASdu(asdu2);
    cnt++;
}

Do I use the library the wrong way or is it a bug?

Thanks in advance for any suggestion.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.