Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi Experts,

I am beginner in PLC and C#. Can you guide me or provide sample code to connect and read data from PLC. (example- PLC db2.real16 or db2.dbd16 tags)

Thanks
Posted
Updated 14-Mar-17 14:16pm

As a beginner the best thing to learn right now (before anything else) is how to use Google. For example: Google[^]

and my gosh right near the top of the results is CP:PLC Communication Using .NET[^]

Also look at: C# Corner : Error Display[^]
 
Share this answer
 
v2
Comments
Member 12109707 31-Dec-15 23:31pm    
I checked the same earlier also but need one more help about the same.

Where I have to put value like (db2.real16) or any conversion required.

Dim cmd As String
cmd = ""
cmd = cmd & "5000" ' sub head (not change)
cmd = cmd & "00" ' network number (not change)
cmd = cmd & "FF" ' PLC number (not change)
cmd = cmd & "03FF" ' I/O number (not change)
cmd = cmd & "00" ' (not change)
cmd = cmd & "0018" ' length of demand data (change)
cmd = cmd & "000A" ' CPU INSPECTOR DATA
cmd = cmd & "0401" ' READ COMMAND
cmd = cmd & "0000" ' SUB COMMAND
cmd = cmd & "D*" ' DEVICE CODE
cmd = cmd & "009500" '"010000" 'adBase ' BASE ADDRESS
cmd = cmd & "00022"
[no name] 31-Dec-15 23:38pm    
Spend some time and do some background reading. You need to understand the basics. There are many examples available. Taking a scattergun approach is not the way.
Richard MacCutchan 1-Jan-16 5:16am    
Your main problem is that you are using VB rather than C#.
Per Söderlund 2-Jan-16 3:56am    
Didnt you say you want to connect to a S7 PLC?
Arent you building a buffer from an example that connects to a mitsubishi MELSEC PLC?
That will never work because they have their own protocols, there is no standard that works for all.
Direct connection between PLC and client application is not recommended.
Most PLCs only allow one connection to be established.

Usually one uses an "OPC Server" as middleware to handle multiple clients.
Google for an OPC Server that supports your PLC.
If you are using C# you can search for Graybox OPCDA wrapper which you can use to connect to OPC in a safe and easy way (you also need the native OPCDA dll).

If you do choose to connect straight to PLC, you have to know that the communicationsprotocol is different for every PLC (Siemens,Mitsubishi etc). The PLC have to be set to either text or binary format and you have to translate it manually.
Sometimes there is a networkscard in PLC that have it own communicationsprotocol.

Basically what I´m saying is. Use an OPC Server or work your ass off trying to configure PLC with components and hacking the communications buffer because documentation is almost never right.
 
Share this answer
 
S7.Net Tutorial with C#
https://www.youtube.com/watch?v=nSnGKm3FSbk&list=PLRCEJ0bGSS1ZlU34IXsIKS62IHH5FcgU1


<iframe width="560" height="315" src="https://www.youtube.com/embed/nSnGKm3FSbk" frameborder="0" allowfullscreen></iframe>
 
Share this answer
 
Comments
OriginalGriff 3-Sep-17 6:40am    
I'm going to let this through, because it is relevant - but ...
1) Try not to post YouTube links - they will normally get caught by the spam detector.
2) Please try to avoid posting solutions to old questions - and this one is over a year and a half old, with solutions - as it's unlikely the original poster is still working on the problem and you do look like a rep point farmer, which is counted as abuse.

In this case I think you were genuinely trying to be helpful, so I let the answer through and won't assume you are farming rep points! But there are some trigger-happy users out there, so please try to stick to more recent questions in future to avoid them assuming you are an abuser and starting the "kick him off the site" process.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900