Microsoft Visual Basic Automation Error Exception Occurred Error' title='Microsoft Visual Basic Automation Error Exception Occurred Error' />Communication with Siemens S7 Plc with C and S7.Net plc driver Mesta Automation.In this article I explain how to implement a Siemens S7 plc driver by using the open source driver S7.Net. You can find S7.Net sources on Git.Hub https github.How to video. Summary.Oshibka.png' alt='Microsoft Visual Basic Automation Error Exception Occurred Error' title='Microsoft Visual Basic Automation Error Exception Occurred Error' />Why S7.Net features and capabilities of the driver.S7. Net is a plc driver written in C, this means that you dont have to handle any interoperability with native code, but it just use OO programming and all the.Net features that you are familiar with.Basic capabilities Can connect and disconnect with the plc using sockets Can read and write bytes from a single area of memory, given a starting address and the number of bytes.High level features Can directly map DBs to classes and structs The types of C are mapped into types of S7 and there are converters for every type double to REAL, int to DEC, etc It is easy to use, well written and perfectly readable Its open source, MIT license permit you to use it in every commercial application Did I already say that its written in C, no interop at all What its not good about S7.Net, roadmap for future upgrades Lack of documentation Lack of a function that permit to readwrite multiple non connected variables with a single request to the plc.Windows 7 Error 2732 Fix, Clean WINDOWS 7 ERROR 2732 And Optimize PC SPEED Up Your PC FREE Scan Now Recommended.Windows Update Error 0X80240018.Documentation on the driver and S7 protocol.The documentation of S7.Net is here. S7. Net exposes a class called Plc that contains all the methods that we can use to communicate with the plc.Plc. public string IP get set.Is. Connected get.Cpu. Type CPU get set.Int. 16 Rack get set.Int. 16 Slot get set.Name get set. Tag get set.Is. Available get.Error. Code Open.Close. public byte Read.BytesData. Type data.Type, int DB, int start.Byte. Adr, int count.ReadData. Type data.Type, int db, int start.Byte. Adr, Var. Type var.Type, int var. Count.Readstring variable.Read. StructType struct.Type, int db. Read.Classobject source.Class, int db. Error.Code Write. BytesData.Type data. Type, int db, int start.Byte. Adr, byte value.WriteData. Type data.Type, int db, int start.Byte. Adr, object value.Writestring variable, object value.Error. Code Write.Structobject struct.Value, int db. Error.Code Write. Classobject class.Value, int db. Last.Error. String get.Error. Code Last.Error. Code get. To connect and disconnect you can use the Open and Close functions, to communicate you can use any of the methods to read and write variables from the plc memory.Every method return an Error.Code, or the object that its supposed to return if there are no errors.Unfortunately this is quite a messy concept, because usually drivers throws exceptions in case of errors, or returns a value indicating the error and put the requested values inside a memory area passing a pointer to that area.Here there is the documentation of the protocol, in german http www.Some more documentation about the S7 protocol can be found at the snap.Snap. 7 full latest full http sourceforge.Also some documentation on S7 protocol can be found inside Lib.No. Dave. Getting started with S7.Net. This paragraph explains how to get S7.Net driver and all the steps from compile the driver to add it into your application.You can choose to add download it from Nu.Get fastest way or to include the full sources in your project.The steps to download from Nu.Get are 1 Right click on the project and select Manage Nu.Get packages 2 Search for S7.Net in the online tab, then install it.Repeat for every project where you need the dll.These steps are to include the sources in your application 1 Download the driver from the Github repository https github.Copy the S7. Net project folder in the folder that contain your project.Add the S7. Net project to the solution.Add the references.A simple application with S7.Net. I know that create an application to showcase the use of the driver is difficult and will not meet everyone requirements, thats why i tried to keep it as simple as possible, just to show how to create a PLC object, how to handle a polling to refresh the data read from the PLC and how to visualize the data around the application in a simple way.Also how to write data to the PLC.The sample application is a WPF application that can connect and disconnect from the PLC, read and write the DB1 variables on the PLC and visualize the values.The application contains 2 projects, one is a wrapper for the plc driver, the other one is a project that contains all the graphic pages.You can download the sample application on Git.Hub https github.S7. Net example. Creating a wrapper of S7.Net. Using a wrapper for the plc driver is a strategy that I usually use to implement features that the plc driver doesnt contain, and to abstract the most of the code required to interface the application with the plc driver.The wrapper exposes an interface IPlc.Sync. Driver that contains all the methods needed to communicate with the PLC connectdisconnect and connection statereadwrite single and multiple variables, called Tags a Tag is an object that contains an address and a valuereadwrite a class special feature of the S7.Net protocol. public interface IPlc. Install Squid On Fedora 15 Dvd . Sync. Driver. Connection.States Connection.State get. void Connect.Disconnect. List lt Tag gt Read.ItemsList lt Tag gt item.List. void Read. Classobject source.Class, int db. void Write.ItemsList lt Tag gt item.List. As you can see this methods are less and differents from the ones named inside the IPlc interface of the driver, and the reason is because I handle the communication errors by throwing exceptions, so the returned values can be just the objects that i need.Also I just use the highest level features of the driver, and handle all the complexity inside the wrapper.I use the concept of Tags inside my wrapper, where a Tag is an object containing an address and a value.This is familiar if you already used OPC Servers and Clients, but here it is much more simple and basic.Creation of PLC class inside the main project.In the main project I usually define the class that contains the PLC values and communication thread in a singleton.You can read more about it in this article https www.You can find the plc class inside Plc.Connectivity folder.The class exposes the properties and methods that are used in all application to communicate with the PLC.Connection. States Connection.State. public DB1 Db.Time. Span Cycle.Read. Time. public void Connectstring ip.Address. public void Disconnect.Writestring name, object value.WriteList lt Tag gt tags.Inside the class there is a multi threaded timer that will poll the plc once every 1.The timer callback is responsible to refresh the tags and to calculate the time passed after every read.You can of course use multiple timers, with different Interval value, to better manage the network resources.Elapsedobject sender, System.Timers. Elapsed. Event.Args e. if plc. Driver null plc.Driver. Connection.State Connection.States. Online. return.Enabled false. Cycle.Read. Time Date. Time.Now last. Read. Time.Refresh. Tags. Enabled true.Read. Time Date. Time.Now. To read values from the PLC I use the feature that read a class directly from a DB.Refresh. Tags. plc.Driver. Read. ClassDb.Write values on the PLCTo write the variables I use the method that permit to write a single object by giving an address and a value.Plc. Instance. WritePlc.Tags. Dword. Variable, dword.Var. You can read an example of write and read in the handlers of the Main.Window page. PLC data visualization.To visualize the values I used a Dispatcher.Timer but you can use also MVVM and Data.Binding. void timerTickobject sender, Event.Args e. btn. Connect.Is. Enabled Plc. Instance.Connection. State Connection.States. Offline. btn.Disconnect. Is. Enabled Plc.Instance. Connection.State Connection.States. Offline. lbl.Connection. State.Text Plc. Instance.Connection. State.To. String. led. Machine.In. Run. Fill Plc.Instance. Db. 1. Bit.Variable. Brushes.Green Brushes. Gray.Speed. Content Plc.Instance. Db. 1. Int.Variable. Temperature.Content Plc. Instance.Db. 1. Real. Variable.Automatic. Speed.Content Plc. Instance.Db. 1. DInt. Variable.Set. Dword. Variable.Content Plc. Instance.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |