16
Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide you through creating a simple sample application that jogs axis 1 in a single direction using Soft Servo Systems’ ServoWorks .NET API. For this tutorial, you will need the following files (included in the ServoWorks .NET API). SwDll.dll ServoWorks API library SwDLLWrapper.dll Wrapper class to access the ServoWorks API library from .NET managed code MotionEngine.rtss, MotionParser.rtss, PLC.rtss Real time motion engines You will also need Microsoft Visual Studio 2005 or Microsoft Visual Studio 2008. If you are using Visual Studio 2008, you will need to set the .NET framework to version 2.0. 1. First, create a new Visual C# Windows Application. 2. Compile the new application. Navigate to the folder containing the compiled executable (typically bin\Debug). Place the files “SwDll.dll” and “SwDLLWrapper.dll” in this folder. Inside this folder, create a new folder names “Simulation_Engines”. Inside this new folder, place the files “MotionEngine.rtss”, “MotionParser.rtss”, and “PLC.rtss”.

Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

Quick Guide for the ServoWorks .NET API 2010/7/13

This document will guide you through creating a simple sample application that jogs axis 1 in a single

direction using Soft Servo Systems’ ServoWorks .NET API.

For this tutorial, you will need the following files (included in the ServoWorks .NET API).

SwDll.dll – ServoWorks API library

SwDLLWrapper.dll – Wrapper class to access the ServoWorks API library from .NET managed code

MotionEngine.rtss, MotionParser.rtss, PLC.rtss – Real time motion engines

You will also need Microsoft Visual Studio 2005 or Microsoft Visual Studio 2008. If you are using

Visual Studio 2008, you will need to set the .NET framework to version 2.0.

1. First, create a new Visual C# Windows Application.

2. Compile the new application. Navigate to the folder containing the compiled executable

(typically bin\Debug). Place the files “SwDll.dll” and “SwDLLWrapper.dll” in this folder.

Inside this folder, create a new folder names “Simulation_Engines”. Inside this new folder, place

the files “MotionEngine.rtss”, “MotionParser.rtss”, and “PLC.rtss”.

Page 2: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

3. In Visual Studio, go to “Project -> Add Reference”. In the window that appears, click on the

“Browse” tab. Go to the directory you just placed SwDLLWrapper and click “OK.”

Page 3: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

4. You should now see “SwDLLWrapper” under “References” in the Solution Explorer.

Page 4: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

5. Double click on “SwDLLWrapper” in the Solution Explorer to open the Object Browser. You

will see that all members of SwDLLWrapper are in the namespace “SWAPI”. In particular, the

“API” class contains all the ServoWorks .NET API functions.

Page 5: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

6. Right click “Form1.cs” in the Solution Explorer and click on “View Code”. Add the following

line to the list of used namespaces at the top of the code.

using SWAPI;

Page 6: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

7. Add the following line just above the constructor of Form1 (public Form1()). This will create an

API object that will be used to call ServoWorks .NET API functions elsewhere in the program.

There should only be one object of the API class in any project that uses the ServoWorks .NET

API.

private API api;

Page 7: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

8. Create a function “InitializeSWAPI” and call it in the constructor of Form1. This function will

handle the initialization routine for the ServoWorks .NET API.

public Form1()

{

InitializeComponent();

InitializeSWAPI();

}

private void InitializeSWAPI()

{

}

Page 8: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

9. Add the following code to the “InitializeSWAPI” function. Be sure to change the “filepath”

string to the file path containing the simulator engines on your local PC. If you want to, you may

use C# functions that detect the path that the program executable is in.

private void InitializeSWAPI()

{

this.api = new API(); //Initialize the api object

//Opens ServoWorks CNC device

this.api.sssOpenDevice("Engines", null);

String filepath =

"C:\\Documents and Settings\\jskim\\My Documents"

+ "\\Visual Studio 2005\\Projects\\Tutorial\\Tutorial\\bin"

+ "\\Debug\\Simulation_Engines"; //File path of motion engines

//Open motion engines

this.api.sssInstallDrivers(filepath, (short)1, (short)1, null);

//Starts communication

this.api.sssStartInterrupt();

Page 9: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

//Turns servo axis 1 on

this.api.sssSetServoStatus(1, 1);

}

10. Most ServoWorks .NET API functions return an error code. You can add simple error checking

to “InitializeSWAPI”, as shown in the code below.

private void InitializeSWAPI()

{

Int32 error;

this.api = new API(); //Initialize the api object

//Opens ServoWorks CNC device

error = this.api.sssOpenDevice("Engines", null);

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

Application.Exit();

String filepath =

"C:\\Documents and Settings\\jskim\\My Documents"

+ "\\Visual Studio 2005\\Projects\\Tutorial\\Tutorial\\bin"

+ "\\Debug\\Simulation_Engines"; //File path of motion engines

Page 10: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

//Open motion engines

error = this.api.sssInstallDrivers(filepath, (short)1, (short)1, null);

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

Application.Exit();

//Starts communication

error = this.api.sssStartInterrupt();

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

Application.Exit();

//Turns servo axis 1 on

error = this.api.sssSetServoStatus(1, 1);

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

Application.Exit();

}

11. Go to the Design view of Form1 and add a label, two buttons, and a timer. Set the Text of

button1 to “Start” and button2 to “Stop”. Set the Interval of timer1 to 50 (ms). Set the Enabled

state of timer1 to true.

Page 11: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

12. Double click on “timer1” in the Design view to add an event to the timer. Add the following

code to the timer routine. This routine will update label1 with the current position of axis1.

private void timer1_Tick(object sender, EventArgs e)

{

this.api.sssGetStatus(ref this.status);

this.label1.Text = this.status.ActualPosition[0].ToString("f4");

}

Also, add the following private object just under the declaration of the API object.

private SSS_STATUS status;

Page 12: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

13. Return to the design view and double click on the “Start” button to add another event. Add the

code below to the event.

private void button1_Click(object sender, EventArgs e)

{

JOG_DATA jogData = new JOG_DATA();

jogData.AxisFlags = 1; //Move axis 1 only

jogData.Feedrate = 1000; //1000mm/min

jogData.MoveDirection = 1; //Positive Direction

Int32 error = this.api.sssSetNcMode(SWAPI.Constants.SSS_OPMODE_JOG);

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

MessageBox.Show("Error starting Jog operation.");

error = this.api.sssJogStart(jogData);

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

MessageBox.Show("Error starting Jog operation.");

Page 13: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

}

14. Return to the design view and double click on the “Stop” button to add another event. Add the

code below to the event.

private void button2_Click(object sender, EventArgs e)

{

Int32 error = this.api.sssJogStop();

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

MessageBox.Show("Error stopping Jog operation.");

Page 14: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

}

15. Finally, we need to add a close sequence to run when the program closes. We will do this by

overriding the OnClosed function that is executed when the main form of the program is closed.

Add the following code just below the event handler for the “Stop” button.

protected override void OnClosed(EventArgs e)

{

Int32 error;

//Stops communication

error = this.api.sssStopInterrupt();

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

MessageBox.Show("Error during closing sequence.");

//Closes motion engines

error = this.api.sssRemoveDrivers();

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

MessageBox.Show("Error during closing sequence.");

//Closes ServoWorks CNC device

error = this.api.sssCloseDevice();

if (error != SWAPI.Constants.SWAPI_ERROR_SUCCESS)

Page 15: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

MessageBox.Show("Error during closing sequence.");

base.OnClosed(e);

}}

16. You can now compile and run the program! Obviously, this program does not do much – it just

jogs Axis 1 in the positive direction. You can refer to other sample programs and ServoWorks

manuals to learn more about other ServoWorks features, such as incorporating the ServoWorks

PLC to your program, using ServoWorks parameters, accessing all of the other variables in

SSS_STATUS, and using other features such as the Auto mode, which can be used to run a CNC

G code program.

Page 16: Quick Guide for the ServoWorks .NET API 2010/7/13softservo.com/wp-content/...dotNET_API_QuickGuide.pdf · Quick Guide for the ServoWorks .NET API 2010/7/13 This document will guide

17. When you deploy a finished program, please be sure to include “SwDll.dll” and

“SwDLLWrapper.dll” in the same folder as the program executable.