Click here to show toolbars of the Web Online Help System: show toolbars |
//***************************************************************************** // // File name: McomProfinet.cs // Location: See Matrox Example Launcher in the MIL Control Center // // // Synopsis: This program allocates a MIL application and system. // Then allocate a MIL industrial communication context to a // Profinet instance. // // Notes: This example is only available if you have the MIL Industrial Communication package, // or another relevant update installed. // Copyright (C) Matrox Electronic Systems Ltd., 1992-2016. // All Rights Reserved //***************************************************************************** using System; using System.Collections.Generic; using System.Text; using System.Threading; using Matrox.MatroxImagingLibrary; using System.Runtime.InteropServices; namespace MAppStart { class Program { static MIL_ID MilApplication = MIL.M_NULL; // Application identifier. static MIL_ID MilSystem = MIL.M_NULL; // System identifier. static MIL_ID MilCom = MIL.M_NULL; // Display identifier. /* Status register */ static byte TriggerACK; static byte ResultReady; static byte ResultValue; /* Control register */ static byte Trigger; static byte ResultACK; static byte ResultCopy; static byte value = 0; static void Main(string[] args) { byte processingResult; /* Allocate a default MIL application, system, display and image. */ MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL); MIL.McomAlloc(MilSystem, MIL.M_COM_PROTOCOL_PROFINET, "M_DEFAULT", MIL.M_DEFAULT, MIL.M_DEFAULT, ref MilCom); /* Set the data to the initial values. */ SetInitialData(); /* Wait for a key press. */ Console.Write("Press <Enter> to end.\n\n"); while (!Console.KeyAvailable) { /* Wait that the PLC set the trigger bit */ WaitForTriggerFromPLC(); /* Do the requested processing */ processingResult = DoProcessing(); /* Write the result back to the PLC*/ WriteResultToPLC(processingResult); } /* Free MIL objects*/ MIL.McomFree(MilCom); MIL.MappFreeDefault(MilApplication, MilSystem, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL); } static void WriteStatus(byte triggerACK, byte resultReady, byte resultValue) { byte[] status = new byte[2]; TriggerACK = triggerACK; ResultReady = resultReady; ResultValue = resultValue; status[0] = (byte)(((ResultReady & 0x01) << 1) | (TriggerACK & 0x01)); status[1] = ResultValue; /* Write in the input module slot id 1 as specified in PLC configuration */ MIL.McomWrite(MilCom, "1", 0, 2, status); } static void ReadControl() { byte[] control = new byte[2]; /* Read in the output module slot id 2 as specified in PLC configuration */ MIL.McomRead(MilCom, "2", 0, 2, control); Trigger = (byte)(control[0] & 0x1); ResultACK = (byte)((control[0] & 0x2) >> 1); ResultCopy = control[1]; } static void SetInitialData() { WriteStatus(0, 0, 0); } static void WaitForTriggerFromPLC() { do { /* Wait for the trigger from the PLC */ ReadControl(); Thread.Sleep(10); } while (Trigger == 0 && !Console.KeyAvailable); Console.Write("Received Trigger!\n\n"); /* Set the TriggerACK and reset ResultReady */ WriteStatus(1, 0, 0); } static byte DoProcessing() { /* Do what need to be done when PLC send the trigger. */ value++; return value; } static void WriteResultToPLC(byte result) { /* Set the result value */ WriteStatus(0, 1, result); /* Wait that the PLC acknowledge the result */ do { ReadControl(); Thread.Sleep(10); } while (ResultACK == 0 && !Console.KeyAvailable); Console.Write(String.Format("Received result ACK! Value:{0} Copy:{1}\n\n", value, ResultCopy)); /* Set the result value */ WriteStatus(TriggerACK, 0, ResultValue); } } }