Beckoff Twincat interaction with .NET

jllowrey

Member
Join Date
Sep 2012
Location
Boston
Posts
1
Hello,

I am a robotics engineering student. I have recently been put on to an older project that is driven with twincat using a beckoff PLC. Just a little background this is a robot with a differential drive which we are hooking up to a kinect(vision system) in order to navigate and move the robot. We are doing this with a seperate software called MRDS Microsoft Robotic Development studio. MRDS communicates to various sensors and motors with DSS using .NET framework.

My question is how do I make my twincat a service in .NET. I want to be able to read and write values to my motors and encoders. I am not familiar with twincat and do not have a lot of time.

-If you think you have a solution or could help me that would be greatly appreciated. Post here or send me an email at [email protected]. (I will read my email first)

Thanks,
-Jeremy
 
I have no experience with that, but I've been told that this is fairly easy, using ADS. Google for 'Beckhoff ADS read', or go to the beckhoff site and check their huge online information section.
 
Not sure what a .net "service" means, haven't done that.

A few references:
"Exchange variable between C# and TwinCat working kinda slow?" on LinkedIn TwinCAT Developers forum.

Beckhoff .net examples:
http://infosys.beckhoff.com/espanol...phi/html/webservice_delphi_8_net.htm&id=10060
(link is a little off, but gets you close).


Some code snipets from my VB.net code. The formatting and colors get corrupted here. Good luck.

Try' connect to PLC
tcClient = New TcAdsClient()
tcClient.Connect("192.168.0.30.1.1", 801) ' establish connection to PLC via AMS address.

Sub ResetPLC()
tcClient.WriteControl(New StateInfo(AdsState.Reset, tcClient.ReadState().DeviceState)) ' to be safe, reset PLC
tcClient.WriteControl(New StateInfo(AdsState.Run, tcClient.ReadState().DeviceState)) ' restart PLC code from top
EndSub

Call subroutine to read a variable. Give the POU name & "." & variable name. If global variable, give as "." & name.
Call WritePLC("Main.bDirectDrive", flgSetDirect)


Read PLC stream and parse into variables. Note that the ReadPLC function returns a BinaryReader object (just like reading a text file), which I then parse. Kind of amazed this worked. Poking around and trying things in .net can prove productive.
Dim br As BinaryReader, i%, setpt!, actual!
br = ReadPLC("Main.Adata", Nchan% * (2 * Npt&)) ' link binary reader to datastream array
For i% = 0 To Npt& - 1 ' step thru datastream
setpt = br.ReadInt16 ' read stem position setpoint
actual = br.ReadInt16 ' read actual stem position setpoint


Function ReadPLC(ByVal PLCvar$, ByVal Nbytes%) As BinaryReader ' reads variable or structure from PLC
Dim hPLC%
Try
Dim ds AsNew AdsStream(Nbytes%) ' dimension data stream for #bytes to read
Dim br AsNew BinaryReader(ds) ' link binary reader to data stream
hPLC% = tcClient.CreateVariableHandle(PLCvar$)
tcClient.Read(hPLC%, ds)
tcClient.DeleteVariableHandle(hPLC%) ' delete handle to PLC variable
ds.Position = 0 ' reset pointer to 1st byte in stream
ReadPLC = br
Catch err As Exception
txtError.Text = "error reading PLC variable: " & PLCvar$ & vbLf & vbLf & err.Message ' often because not connected to PLC
ReadPLC = Nothing
EndTry
EndFunction


Note that writing a PLC variable is easier since the WriteAny function figures out the type. I haven't figured out similar for reading a variable and must hard code the types in VB as I read values.

Sub WritePLC(ByVal PLCvar$, ByVal PLCvalue AsObject) ' writes a variable or structure from PLC, Narr% = #elements in array (1 for single value)
Dim hPLC%
txtError.Text = ""
Try
hPLC% = tcClient.CreateVariableHandle(PLCvar$)
tcClient.WriteAny(hPLC%, PLCvalue)
tcClient.DeleteVariableHandle(hPLC%) ' delete handle to PLC variable
Catch err As Exception
txtError.Text = "error writing PLC variable: " & PLCvar$ & vbLf & vbLf & err.Message
EndTry
EndSub

And so above makes sense to .net, put this at the top of your code:
Imports TwinCAT.Ads
and set a project Reference to Twincat.ads.dll
If setup right, after you type "." in a TwinCAT dll function (class), you should get a drop-down list of all methods and properties for that function.
 
Last edited:

Similar Topics

I'm wondering how should I use persistent variables in my program. Should I just use PERSISTENT on the declaration of the variables I want to...
Replies
1
Views
2,263
I just see new tech of relationship between BECKOFF PLC Modules and IPC through Profibus in HUSKY Hypet, i am little confuse to understand it, if...
Replies
3
Views
2,269
I am creating an application using Labview and I'm trying to populate some variables in the PLC. I went to beckoff's website to download the...
Replies
8
Views
8,937
How i Can connect Delta plc, with Beckoff remote IO cards?
Replies
2
Views
173
I am facing ADS Error reading/writing not permit while trying to connect Cx8090 PLC controller to my PC (see screnshots). I guess it is because of...
Replies
4
Views
1,728
Back
Top Bottom