public static void Main(string[] args) { DB1 [0] = 0xff; DB2 [0] = 0xff; DB3 [0] = 0xff; Server = new S7Server(); Server.RegisterArea(S7Server.srvAreaDB, 1, ref DB1, DB1.Length); Server.RegisterArea(S7Server.srvAreaDB, 2, ref DB2, DB2.Length); Server.RegisterArea(S7Server.srvAreaDB, 3, ref DB3, DB3.Length); TheEventCallBack = new S7Server.TSrvCallback(EventCallBack); //TheReadCallBack = new S7Server.TSrvCallback (ReadCallBack); // Server.EventMask = ~S7Server.evcDataRead; Server.SetEventsCallBack(TheEventCallBack, IntPtr.Zero); // Server.SetReadEventsCallBack (TheReadCallBack, IntPtr.Zero); int Error = Server.Start(); if (Error == 0) { Console.ReadKey(); Server.Stop(); } else { Console.WriteLine(Server.ErrorText(Error)); } }
public static void Start(short port) { Server = new S7Server(); // Share some resources with our virtual PLC Server.RegisterArea(S7Server.srvAreaDB, // We are registering a DB 1, // Its number is 1 (DB1) DB1, // Our buffer for DB1 DB1.Length); // Its size // Do the same for DB2, DB3, and DB4 Server.RegisterArea(S7Server.srvAreaDB, 2, DB2, DB2.Length); Server.RegisterArea(S7Server.srvAreaDB, 3, DB3, DB3.Length); Server.RegisterArea(S7Server.srvAreaDB, 4, DB4, DB4.Length); // Exclude read event to avoid the double report // Set the callbacks (using the static var to avoid the garbage collect) TheEventCallBack = new S7Server.TSrvCallback(EventCallback); TheReadCallBack = new S7Server.TSrvCallback(ReadEventCallback); Server.EventMask = ~S7Server.evcDataRead; Server.SetEventsCallBack(TheEventCallBack, IntPtr.Zero); Server.SetReadEventsCallBack(TheReadCallBack, IntPtr.Zero); // Uncomment next line if you don't want to see wrapped messages // (Note : Doesn't work in Mono 2.10) // Console.SetBufferSize(100, Int16.MaxValue - 1); // Start the server onto the default adapter. // To select an adapter we have to use Server->StartTo("192.168.x.y"). // Start() is the same of StartTo("0.0.0.0"); Server.SetParam(S7Consts.p_u16_LocalPort, ref port); int Error = Server.Start(); if (Error != 0) { throw new Exception($"Error starting Snap7 server: {Server.ErrorText(Error)}"); } //if (Error == 0) //{ // // Now the server is running ... wait a key to terminate // //Console.ReadKey(); // Server.Stop(); //} //else // Console.WriteLine(Server.ErrorText(Error)); // If you got a start error: // Windows - most likely you ar running the server in a pc on wich is // installed step 7 : open a command prompt and type // "net stop s7oiehsx" (Win32) or // "net stop s7oiehsx64" (Win64) // And after this test : // "net start s7oiehsx" (Win32) or // "net start s7oiehsx64" (Win64) // Unix - you need root rights :-( because the isotcp port (102) is // low and so it's considered "privileged". }
static void Main(string[] args) { Server = new S7Server(); // Share some resources with our virtual PLC Server.RegisterArea(S7Server.srvAreaDB, // We are registering a DB 1, // Its number is 1 (DB1) ref DB1, // Our buffer for DB1 DB1.Length); // Its size // Do the same for DB2 and DB3 Server.RegisterArea(S7Server.srvAreaDB, 2, ref DB2, DB2.Length); Server.RegisterArea(S7Server.srvAreaDB, 3, ref DB3, DB3.Length); // Exclude read event to avoid the double report // Set the callbacks (using the static var to avoid the garbage collect) TheEventCallBack = new S7Server.TSrvCallback(EventCallback); TheReadCallBack = new S7Server.TSrvCallback(ReadEventCallback); Server.EventMask = ~S7Server.evcDataRead; Server.SetEventsCallBack(TheEventCallBack, IntPtr.Zero); Server.SetReadEventsCallBack(TheReadCallBack, IntPtr.Zero); // Uncomment next line if you don't want to see wrapped messages // (Note : Doesn't work in Mono 2.10) Console.SetBufferSize(100, Int16.MaxValue - 1); // Start the server onto the default adapter. // To select an adapter we have to use Server->StartTo("192.168.x.y"). // Start() is the same of StartTo("0.0.0.0"); int Error=Server.Start(); if (Error == 0) { // Now the server is running ... wait a key to terminate Console.ReadKey(); Server.Stop(); } else Console.WriteLine(Server.ErrorText(Error)); // If you got a start error: // Windows - most likely you ar running the server in a pc on wich is // installed step 7 : open a command prompt and type // "net stop s7oiehsx" (Win32) or // "net stop s7oiehsx64" (Win64) // And after this test : // "net start s7oiehsx" (Win32) or // "net start s7oiehsx64" (Win64) // Unix - you need root rights :-( because the isotcp port (102) is // low and so it's considered "privileged". }
static void Main(string[] args) { Server = new S7Server(); // Set the callbacks (using the static var to avoid the garbage collect) TheEventCallBack = new S7Server.TSrvCallback(EventCallback); TheRWAreaCallBack = new S7Server.TSrvRWAreaCallback(SrvRWAreaCallback); // Filter a bit of noise Server.EventMask = 0x3ff; Server.SetEventsCallBack(TheEventCallBack, IntPtr.Zero); Server.SetRWAreaCallBack(TheRWAreaCallBack, IntPtr.Zero); // Uncomment next line if you don't want to see wrapped messages // (Note : Doesn't work in Mono 2.10) Console.SetBufferSize(100, Int16.MaxValue - 1); // Start the server onto the default adapter. // To select an adapter we have to use Server->StartTo("192.168.x.y"). // Start() is the same of StartTo("0.0.0.0"); int Error = Server.Start(); if (Error == 0) { // Now the server is running ... wait a key to terminate Console.ReadKey(); Server.Stop(); } else { Console.WriteLine(Server.ErrorText(Error)); } // If you got a start error: // Windows - most likely you ar running the server in a pc on wich is // installed step 7 : open a command prompt and type // "net stop s7oiehsx" (Win32) or // "net stop s7oiehsx64" (Win64) // And after this test : // "net start s7oiehsx" (Win32) or // "net start s7oiehsx64" (Win64) // Unix - you need root rights :-( because the isotcp port (102) is // low and so it's considered "privileged". }
private void Form1_Load(object sender, EventArgs e) { Server = new S7Server(); TheEventCallBack = new S7Server.TSrvCallback(EventCallback); Server.SetEventsCallBack(TheEventCallBack, IntPtr.Zero); int Error = Server.Start(); showMsg("Start:" + Error); var dbs = ConfigurationManager.AppSettings["dbs"]; if (!string.IsNullOrEmpty(dbs)) { foreach (var cmd in dbs.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { Reg(cmd); } } }
static void Main(string[] args) { System.Console.Out.WriteLine("S7 Server starting up"); Event = new S7Server.USrvEvent(); Srv = new S7Server(); Srv.RegisterArea(S7Server.srvAreaDB, // We are registering a DB 1, // Its number is 1 (DB1) DB1, // Our buffer for DB1 DB1.Length); // Its size TheEventCallBack = new S7Server.TSrvCallback(EventCallback); TheReadCallBack = new S7Server.TSrvCallback(ReadEventCallback); Srv.SetEventsCallBack(TheEventCallBack, IntPtr.Zero); Srv.SetReadEventsCallBack(TheReadCallBack, IntPtr.Zero); Srv.EventMask = Srv.EventMask & 1; // Mask 00000000 Srv.EventMask = Srv.EventMask | S7Server.evcDataRead; Srv.EventMask = Srv.EventMask | S7Server.evcDataWrite; int Error = Srv.Start(); if (Error == 0) { Console.WriteLine("Press ESC to stop"); do { while (!Console.KeyAvailable) { while (Srv.PickEvent(ref Event)) { Console.Out.WriteLine(Srv.EventText(ref Event)); } } } while (Console.ReadKey(true).Key != ConsoleKey.Escape); Srv.Stop(); } System.Console.Out.WriteLine("Enter any key to quit"); System.Console.ReadKey(); }