static void Main(string[] args) { // Create CIP communitation object. The first parameter is the local IP address to // listen incoming connection in default port 0xAF12 (from 'MSG' block in ladder program). // The second parameter is the PLC's IP addres using the same default port (0xAF12, don't change it. // PLC's listen only in this port) // PS: the 'Connected' checkbox in the 'MSG' instruction block property ('Connection' tab) MUST be unchecked. // Don't worry, this doesn't affect the communication performance. The socket connection remains established even // when is not sending messages. The NOP messagem was implemented (working like a life/watchdog msg). m_Cip = new CIP("0.0.0.0", "192.168.91.168"); m_Cip.OnEventTrace += Cip_OnEventTrace; m_Cip.OnConnRecReceiveMsgData += OnConnRecReceiveMsgData; // <- event to receive data from incoming connections m_Cip.OnConnStatusChanged += OnConnStatusChanged; // <- event to get connection state changes m_Cip.Open(); // Initialize variable to be send m_BytesToSend = new byte[256]; while (true) { // If the send connection is established and ready to send... if (m_Cip.SendChannelConnected() && m_Cip.ReadyToSend()) { try { // Change the message content m_BytesToSend[0]++; for (int i = 1; i < m_BytesToSend.Length; i++) { m_BytesToSend[i] = (byte)(m_BytesToSend[i - 1] + 1); } // Log. Cip_OnEventTrace(Traceable.EventType.Info, string.Format("Sending data ({0} bytes)...", m_BytesToSend.Length)); // Write data in the 'RECEIVE' tag in the PLC ('RECEIVE' is the tag's name) // You have to create a tag with this exactly name, array of SINT type (one byte), total size 300 m_Cip.SendData("RECEIVE_TAG", ElementaryDataType.SINT, m_BytesToSend); // Success log string strData = string.Join("|", m_BytesToSend.Select(a => a.ToString())); Cip_OnEventTrace(Traceable.EventType.Info, string.Format("Send msg. successful ({0} bytes)... [{1}]", m_BytesToSend.Length, strData)); } catch (Exception e) { // Error log Cip_OnEventTrace(Traceable.EventType.Exception, string.Format("Fail to send data. Error: {0}", e.Message)); } } // Delay Thread.Sleep(1000); } }
static void Main(string[] args) { try { CIP c = new CIP("192.168.0.222"); c.RegisterSession(); c.Browse(); //c.Read("Teeq"); // DINT: ok //c.Read("Local:2:I"); // UDT: byte array [TODO] //c.Read("Local:2:I.Ch0Data"); // UDT member: REAL - ok //c.Read("ints.a"); // UDT member: DINT - ok //c.Read("ints.b"); // UDT member: INT - ok //c.Read("V1.diOp"); // UDT member: BOOL - ok //c.Read("Local:2:C.Ch0Config.RangeType"); // nested UDT: ok /*while (ok) * { * c.Read("Local:2:I.Ch0Data"); * Thread.Sleep(1000); * }*/ //c.Write("Teeq", 9); // writing DINT: ok //c.Read("Teeq"); c.Write("Local:3:O.Ch6Data", 4.23f); // writing REAL: ok c.Read("Local:3:O.Ch6Data"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("bye"); Console.ReadLine(); }