Chris Graf Posted January 12, 2012 Report Share Posted January 12, 2012 Hi, I am fairly new to programming and C#. I'm trying to create my own program using Visual Studio 2010 .NET 4 platform. I have read the pdf file for Communication Driver for Dot Net and I'm having a little trouble trying to set my PC up as an Ethernet Listener. Currently I am using a button click method to read/ write all the MI's in the PLC. I tried using the examples that came with Unitronics Communication Driver but the program wouldn't build. I have the most current DLL version (1.0.0.43) being used. I am using XP 32-bit OS. Here is a snapshot of the code. I am wanting to take out the button method and setting this up to be a listener. (* raw HEX image data removed - need to upload image and attach, rather than just paste. Pasting results in a large block of HEX(MIME?) data rather than the image. I don't now why - MOD *) Any help will be greatly appreciated and thanks in advanced. Chris Quote Link to comment Share on other sites More sharing options...
Saragani Posted January 15, 2012 Report Share Posted January 15, 2012 What's the error you get in the compiler? Do you need a 1 connection listener, or a "server" that can handle multiple connections from multiple plcs? Quote Link to comment Share on other sites More sharing options...
Chris Graf Posted January 16, 2012 Author Report Share Posted January 16, 2012 Hi Saragani, I have 108 errors. Some are "invalid token" and some are 'ListenerTest.Form1.listener' is a 'field' but is used like a 'type'. Hopefully I have attached the code I have so far this time so you can see how I am writing the code. Right now I just need 1 connection listener. Chris Quote Link to comment Share on other sites More sharing options...
Saragani Posted January 17, 2012 Report Share Posted January 17, 2012 Ok, you have a lot of work to do on your C# syntax. 1) you have a static function called GetPLC which doesn't return anything. 2) That function is not written correctly (c# syntax), instead of having it's code surounded by { }, you have a ; at the end of the function definition. The code you meant should probably be something like this: public static void GetPLC() { The rest of your code here. } You can also have the EthernetListener sent as a parameter to that function (like you did), but then you will need to use it (instead of creating a new one, like you did). Quote Link to comment Share on other sites More sharing options...
Chris Graf Posted January 19, 2012 Author Report Share Posted January 19, 2012 Ok, you have a lot of work to do on your C# syntax. 1) you have a static function called GetPLC which doesn't return anything. 2) That function is not written correctly (c# syntax), instead of having it's code surrounded by { }, you have a ; at the end of the function definition. The code you meant should probably be something like this: public static void GetPLC() { The rest of your code here. } You can also have the EthernetListener sent as a parameter to that function (like you did), but then you will need to use it (instead of creating a new one, like you did). I used the ; instead of {} because it was typed that way in the example. I changed it like you stated, however I am still having a hard time understanding exactly how this works. I created a smaller program just trying to communicate to the PLC without any success. Would it be possible to send you that program so you may take a look at it and hopefully tell me what I am doing wrong? Quote Link to comment Share on other sites More sharing options...
Saragani Posted January 20, 2012 Report Share Posted January 20, 2012 Yes, send it to Support@unitronics.com and request in the mail that it will be passed to Saragani from the R&D. Quote Link to comment Share on other sites More sharing options...
Chris Graf Posted January 23, 2012 Author Report Share Posted January 23, 2012 Yes, send it to Support@unitronics.com and request in the mail that it will be passed to Saragani from the R&D. Hi Saragani, I sent my program to you but I'm not sure if you have received it or not. I think I have narrowed down my problem however. The program will read the line (it does not crash the program) but it will not execute and then proceed to the next line of code. Since it does not execute this line of code, the PLC does not establish a connection to the PC and is not able to read any data from the PLC. I am not sure if this is an Unitronics driver issue (I do have the latest driver downloaded) or if I am missing something within my code. Hopefully I have given you enough information to help. If you need more please let me know. Chris Quote Link to comment Share on other sites More sharing options...
Saragani Posted January 23, 2012 Report Share Posted January 23, 2012 I didn't get any mail from the support yet. Hi, nothing should happen after the line you mentioned (the one that should execute the OnConnect) since it's an event. OnConnect will be executed when the PLC will try to establish a connection to your PC on that specific port that you listen to (make sure that firewall does not prevent that connection). I see that the OnConnect has some code on creating a connection through Ethernet. I don't think that you need it since you already have a connection to the PLC (you get a PLC object as a parameter of the OnConnection function). You can use that PLC object for reading and writing data from the PLC. Few things to check: 1) Check that the PLC actually tries to connect to the PC. 2) Check that the program in the Examples works for you (It manages to get connection and reads MIs etc). Btw, are you sure you need a Listener? Listener will make your PC wait for a connection request from a PLC. On the other hand, if you know the IP and the port of the PLC (and the PLC is listening using TCP, meaning it has a socket in TCP Slave mode) then the PC can establish a connection to it using Ethernet (instead of Ethernet Listener). Quote Link to comment Share on other sites More sharing options...
Chris Graf Posted January 26, 2012 Author Report Share Posted January 26, 2012 I didn't get any mail from the support yet. Hi, nothing should happen after the line you mentioned (the one that should execute the OnConnect) since it's an event. OnConnect will be executed when the PLC will try to establish a connection to your PC on that specific port that you listen to (make sure that firewall does not prevent that connection). I see that the OnConnect has some code on creating a connection through Ethernet. I don't think that you need it since you already have a connection to the PLC (you get a PLC object as a parameter of the OnConnection function). You can use that PLC object for reading and writing data from the PLC. Few things to check: 1) Check that the PLC actually tries to connect to the PC. 2) Check that the program in the Examples works for you (It manages to get connection and reads MIs etc). Btw, are you sure you need a Listener? Listener will make your PC wait for a connection request from a PLC. On the other hand, if you know the IP and the port of the PLC (and the PLC is listening using TCP, meaning it has a socket in TCP Slave mode) then the PC can establish a connection to it using Ethernet (instead of Ethernet Listener). Hi Saragani, I have checked that the PLC is trying to connect to the PC. I have the PLC and the PC connected to each other by a switch on its own network. The PLC has an IP address of 192.168.0.106 and the PC has an IP address 192.168.0.205. I have Socket 1 initialized and set the protocol to ICMP. My firewall has been disabled so I know that is not an issue. I used the Ping function in the PLC and I also used Wireshark (a packet sniffer program) to verify that the PLC is trying to communicate to the PC. As far as I can tell, the PLC is trying to connect to the PC. I have included my program so you may see what I am referring to. The program "Reading MIs using Ethernet Listener - C# Example" is not working for me. I changed the LOCAL_PORT in the program to match port 20260 that I am using in the PLC and OnConnect is still not being executed. Yes we are sure that we want to use a Listener for this project because we are wanting the PLC to call onto the PC and not the other way around. TCP.vlp Quote Link to comment Share on other sites More sharing options...
Saragani Posted January 26, 2012 Report Share Posted January 26, 2012 Hi, you need to set the socket to TCP Master (and have the ladder try to connect to the IP 192.168.0.205) and not ICMP. ICMP is used for ping and not for data transfer. Quote Link to comment Share on other sites More sharing options...
Chris Graf Posted January 31, 2012 Author Report Share Posted January 31, 2012 Hi, I'm sorry to keep asking you questions, but I am still having problems trying to get the PLC and the PC to communicate. I have made all the changes that you have suggested and I even made a different c# program that concentrates on just trying to connect with the PLC. I have researched on the forums, the internet, and have read documentations that Unitronics has provided and I still don't understand exactly what I am doing wrong. I've tried using both TCP and UDP in the PLC to initiate the call to the PC but it doesn't connect. I have included my VisiLogic program and c# program for references. I hope you can help and I appreciate any feedback. Thanks in advance. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Unitronics.ComDriver; using Unitronics.ComDriver.Messages.DataRequest; using System.Runtime.Remoting.Messaging; using System.Reflection; namespace Ethernet { public partial class Form1 : Form { private PLC plc; const int LOCAL_PORT = 20260; delegate void SetControlValueCallback(Control oControl, string propName, object propValue); public Form1() { InitializeComponent(); Listener(); } private void Listener() { try { EthernetListener listener = PLCFactory.GetChannel(LOCAL_PORT); if (listener == null) listener = new EthernetListener(LOCAL_PORT, 3, 3000); listener.OnListenerConnectionAccepted += new EthernetListener.ListenerConnectionAcceptedDelegate(OnConnect); PLCFactory.GetPLC(listener); } catch { MessageBox.Show("Could not connect"); } } private void OnConnect(PLC oPlc) { plc = oPlc; MessageBox.Show("Connected"); } } } TCP.vlp Quote Link to comment Share on other sites More sharing options...
Saragani Posted February 1, 2012 Report Share Posted February 1, 2012 The C# code looks enough for getting a connection, but the PLC program is not even trying to establish a connection. Lets get over the code: Network 1: On PLC init, set PLC name and do a Card Init Network 2: On PLC Init, Do a card Init (why doing it again), and then do a socket init (It's OK, TCP Master). Network 3: Disabled Network 4: On F1 Key down, UDP Raw send some data Network 5: Disabled. No where in the PLC code you try to establish a TCP connection to the PC. The PC listens to TCP connections, so there is no use for sending UDP requests. The PC doesn't expect to get requests from the PLC, but on the other way around... the PC will send a request (using Unitronics standard PCOM) to the PLC (for example, reading MIs) and will get a reply from the PLC. I've attached a small PLC program. When you press F1, it will try to connect to 192.168.0.205 on port 20260 I hope that you will get a "connected" messagebox with that C# program. TCP.vlp Quote Link to comment Share on other sites More sharing options...
Chris Graf Posted February 2, 2012 Author Report Share Posted February 2, 2012 I used your program and got the connected message box now. Thank you so much for you help, as this is all new to me and I still have much to learn. Chris Quote Link to comment Share on other sites More sharing options...
Saragani Posted February 2, 2012 Report Share Posted February 2, 2012 I'm glad to hear that it works :-) Quote Link to comment Share on other sites More sharing options...
John Rachow Posted November 15, 2012 Report Share Posted November 15, 2012 I know that I am digging up an old post but since Chris is no longer with the company and this is exactly the same program I was hoping that I could just re-open this thread. I am taking over this project and must admit that I know virtually nothing regarding the PLC but am familiar with c#. I am having dificulty communicating with the PLC. From what I have been able to determine is that for some reason the listening PC when it gets a connection request from the PLC it somehow thinks that plc is null and therefore never runs the OnConnect method(See code below) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Unitronics.ComDriver; using Unitronics.ComDriver.Messages.DataRequest; using System.Runtime.Remoting.Messaging; using System.Reflection; using System.Data.SqlClient; namespace TestPLC { public partial class Form1 : Form { System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection ("data source=SQLSERVER;initial catalog=Furnaces;integrated security=SSPI;persist security info=False;workstation id=John;packet size=4096"); System.Data.SqlClient.SqlConnection sqlConnection2 = new System.Data.SqlClient.SqlConnection ("data source=SQLSERVER;initial catalog=Catalog;integrated security=SSPI;persist security info=False;workstation id=John;packet size=4096"); private PLC plc; const int LOCAL_PORT = 502; //private Channel listener; delegate void SetControlValueCallback(Control oControl, string propName, object propValue); public Form1() { InitializeComponent(); Listener(); } void plc_EventAbortCompleted() //This method read/write MI's to PLC { MethodInvoker mi = delegate() { }; UpdateUI(mi); } private void UpdateUI(MethodInvoker del) { if (this.InvokeRequired) this.Invoke(del); else del(); } private void Listener() { try { EthernetListener listener = PLCFactory.GetChannel(502); if (listener == null) listener = new EthernetListener(502, 3, 3000); //label1.Text = listener.Status.ToString(); listener.OnListenerConnectionAccepted += new EthernetListener.ListenerConnectionAcceptedDelegate(OnConnect); listener.OnListenerConnectionClosed += new EthernetListener.ListenerConnectionClosedDelegate(OnDisconnect); PLCFactory.GetPLC(listener); } catch (Exception ex) { MessageBox.Show("Could not connect - Listener" + ex.ToString()); } } private void OnDisconnect(EthernetListener ethernetListener) { EthernetListener listener = PLCFactory.GetChannel(LOCAL_PORT); if (plc != null) { MessageBox.Show("Disconnected"); plc.Disconnect(); } else { if (listener != null) { MessageBox.Show("Disconnected2"); listener.Disconnect(); listener.OnListenerConnectionAccepted -= new EthernetListener.ListenerConnectionAcceptedDelegate(OnConnect); listener.OnListenerConnectionClosed -= new EthernetListener.ListenerConnectionClosedDelegate(OnDisconnect); Listener(); } } } private void OnConnect(PLC oPlc) { plc = oPlc; MessageBox.Show("Connected"); } } } This is just a simple program that is being used to test communication and connection with the PLC. Chris supposedly had this working but when he left it was dropped on my desk and I cannot get the PLC to connect with to the PC. I will also try to attach the test PLC program for you to look at as well. Like I said, I am not sure why it thinks the plc is null. Please forgive my lack of knowledge of PLC programming. Quote Link to comment Share on other sites More sharing options...
Saragani Posted November 15, 2012 Report Share Posted November 15, 2012 It's been a while since the last time I wrote a c# program that uses the Listener, but as it seems, I don't see anything wrong. What PLC model is trying to connect with the program? What is the ComDriver version? What Windows version you are running, and do you have full Administrator rights? What kind of connection are you having? (Direct Ethernet connection, or using Switch, or Hub, or maybe over the Internet) If might also be useful to turn the Logger on (and using the LogViewer I might be able to see what the problem is). We will leave that as a last resort. Quote Link to comment Share on other sites More sharing options...
John Rachow Posted November 16, 2012 Report Share Posted November 16, 2012 We are using a V350 to connect and the ComDriver is the latest 1.0.0.47. This is running on Wndows 7 x64 and I am an administrator and am connecting over the network via a switch. I get a response when pressing the F1 button as it is programmed that way but the method that it goes into is OnDisconnect instead of OnConnect which would lead me to believe that for some reason the plc is null. I am able to attach the PLC program for you to peruse and see if there is something that is missing to make C# to think it is null. Do I have to set anything else up on the PLC side to allow connection in C#? Once again thanks for your help. John Enettest.vlp Quote Link to comment Share on other sites More sharing options...
Saragani Posted November 19, 2012 Report Share Posted November 19, 2012 Hi, Chris Graf mentioned testing the Listener Example on our website. Can you please test and see if it works for you? Can you also test SD Card Explorer? This might give us some clues if the problem is maybe in the Listener itself other than your code. Quote Link to comment Share on other sites More sharing options...
John Rachow Posted November 19, 2012 Report Share Posted November 19, 2012 Saragani, Despite my best efforts, I have not been able to get either of the programs you mentioned to load. As I stated before, the PLC is making an attempt to connect, but for some reason C# thinks that the PLC is null and therefore does not run the line of code that would initiate the OnConnect method, instead it runs the OnDisconnect method. Is there something I am missing in the PLC that would make it do that? Again I am attaching the PLC code for you to look at.Enettest.vlp Please let me know if there is anything that is missing. As I stated before, Chris had stated that this was working, but for the life of me I cannot get it to connect and therefore must be overlooking something on the PLC. Quote Link to comment Share on other sites More sharing options...
Saragani Posted November 19, 2012 Report Share Posted November 19, 2012 Ok, 1st note on the vlp you have attached - The Socket is initialized as Slave while it should be Master. The 2nd note would be removing the timer, or at least increasing the interval. The PC should be able to get a connection in less than 5 seconds (on LAN it should be very fast), but those timers (and events because of disconnection) can make your debug harder. Quote Link to comment Share on other sites More sharing options...
mnehez Posted October 21, 2013 Report Share Posted October 21, 2013 I know this is an old post but.... I appreciate these postings and it helped me to figure this out but using the VB.net listener code 2010 and a V570 I replaced "F1" with MB0 then watched the socket in the Socket Paremeters. I noticed that when MB50 went LOW (off) the socket initialized ant the listener went from listen to connected. The opposite is true when MB50 went HIGH (On). Is this the intention? Becuase it looks like the issue may be in the PLC (if any) and not the .Net listener. Quote Link to comment Share on other sites More sharing options...
Saragani Posted October 22, 2013 Report Share Posted October 22, 2013 Depends on the code that runs on the PLC. MB50 is not a system bit, so it should not be affected by any connection, unless the project in the PLC uses it for some reason. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.