Skip to content

Setup and Connection

Felix Weiß edited this page Jun 21, 2022 · 1 revision

Setup the class

Setting up the interface class is fairly easy just create a new instance:

//attaching a logger
Logger.LogLevel = LogLevel.Verbose;
Logger.OnNewLogMessage((date, msg) => {
    Console.WriteLine($"{date.ToString("HH:mm:ss")} {msg}");
});

//setting up a new PLC interface and register collection
MewtocolInterface plc = new MewtocolInterface("192.168.115.5");

Initiate a new connection

After that you cann call the ConnectAsync() method

await plc.ConnectAsync(
    //PLC connected
    (plcinf) => {
        if(plcinf.OperationMode.RunMode)
            Console.WriteLine("PLC is in RUN");
    },
    //Connection failed
    () => {
        Console.WriteLine("PLC failed to connect");
    }
);

Note that you cann either use the callbacks inside the ConnectAsync() method or attach to an event like:

plc.Connected += (plcinf) => {
    if(plcinf.OperationMode.RunMode)
        Console.WriteLine("PLC is in RUN");
};

await plc.ConnectAsync();

Change between RUN/PROG mode

await plc.ConnectAsync(
    //PLC connected
    (plcinf) => {
        if(!plcinf.OperationMode.RunMode) {
            await plc.SetOperationMode(OPMode.Run);
            Console.WriteLine("PLC is in RUN");
        }
    }
);

Clone this wiki locally