It’s Time To Focas: CNC Router Programming Tutorial – Part 1
ATTENTION!!!!!
We have a newer up to date video version of this tutorial here!
We now offer Consulting, Contracting, and Training services for Focas. Check it out here!
If you have a question with something in this series, check out our new forum here!
Ok, ok. I know you are wondering why I misspelled focus. Well, I didn’t. This post is the start of a series of posts that will center around programming for the Fanuc control system. If you are wondering what Fanuc is, all you need to know is that Fanuc makes industrial control systems.
These control systems can be found in CNC routers, mills, and robots. This post won’t contain any code, however, if you wish to test out any code in future posts in this series, you will need to have access to a Fanuc control system that you can connect to. All the code that appears in future posts will be in .Net. I will be using C#, however, if you are familiar with VB.Net you can apply all the concepts to that.
What is Focas?
Focas is essentially just an API from Fanuc that allows you to make calls to the Fanuc system. Using these calls you can read from and write to the Fanuc. This may be reading information such as the mode or status of the controller, or it may be setting parameters or triggering an action.
Fanuc has APIs for C++, however, we will be working with the .Net API. The .Net API is nothing more than the C++ API with a C# wrapper. In fact, the wrapper is just a .cs file with a bunch of dll Imports from the C++ dll.
the cs file will not contain all of the API calls from the dll, however, if you understand how to use the dll import within C#, it is not difficult to use the C++ header file to import the missing methods. It just takes a little more effort. I have actually done this a few times in the past for things that I needed.
Also, I would like to note that I will be writing code in this series that I have used while writing applications for routers. While a lot of this code will be able to be used for any type of machinery that has a Fanuc control, I will not be using parts of the API that are specific to other types of machinery such as lathes or lasers.
Stuff You Should Know
So there are some things that I am going to assume when writing these posts. The first thing I am going to assume is that you have access to some type of Fanuc control that would be able to use to test your code out.
I will also assume that you have some experience in writing code in .Net. This is not a .Net tutorial, this is a series of informative posts on the Fanuc Focas API. We will be covering some .Net specific stuff, however, learning .Net is not the focus of this series. If you are not familiar with basic .Net programming, then you should find a good tutorial to follow before going through this series.
While I do expect some level of basic understanding of .Net, if I happen to venture into any advanced .Net concepts I will do my best to explain what it is that I am doing and why I am doing it. Again, this is not a .Net tutorial, so if there is something you don’t understand, leave a comment and I will get back to you. If it is a basic concept that you don’t understand, I will probably tell you to go find a .Net tutorial.
It should also be noted, that there are different versions of Fanuc controls. Because of this, some of the code that I cover may not work on specific controls. In the resources below, which we will go over here is a bit, there is a website that documents all the API calls. Each call has a list of controls that it will and will not work on. So if there is a question on whether or not the code will work for you, you may need to consult the documentation.
What This Series is Not
One thing that I think is important to mention, is that this is by no means a G Code tutorial. If you are looking for a tutorial on G Code, you will need to look elsewhere. This is a tutorial on programming applications that can communicate with Fanuc systems and in no way will it teach any G Code.
Resources For This Series
Inventcom
https://www.inventcom.net/fanuc-focas-library/general/fwlib32
The first resource we are going to use is a website called Inventcom. This webpage is going to be our Focas Bible. This is where we will go when we have questions on specific API calls. Whether it is compatibility with the version of your control, the supported parameters for the call, or what the call does. Make sure to bookmark this site, because this is where most of your questions will be answered.
FwLib32
https://github.com/openindustryorg/OpenIndustryFrameworkFanuc/blob/master/FwLib/FwLib32.cs
FwLib32.cs is the file that we will be using to access the API calls. You will need to include this file in your project. What this file does, is import the individual API calls from the dll so that we can use them in C#.
Coming Up
Now, that we have all the preliminary stuff out of the way, we can start getting to the good stuff. In the next post, we will discuss the various ways that we can communicate with the Fanuc systems and a couple of different methods for creating those connections. I hope to see you all there!
24 thoughts on “It’s Time To Focas: CNC Router Programming Tutorial – Part 1”
Leave a Reply
You must be logged in to post a comment.
[…] you have not yet read It’s Time To Focas – Part 1, you should go back and read it first before continuing on with part 2. I list some resources that […]
Thank you so much!!!!!!! Im glad someone is sharing their knowledge!!!
Thanks, this will help a lot of persons..
Where did the Fwlib32.cs file come from? Was that provided from the manufacturer / how was that generated?
Hi Kyle,
Yes, the fwlib2.cs file came from Fanuc. I linked to a copy of the file in the first Fanuc Focas post at the bottom of the page. You can find a copy on this GitHub page https://github.com/openindustryorg/OpenIndustryFrameworkFanuc/blob/master/FwLib/FwLib32.cs
[…] For more information on the Fanuc Focas API and what it can do, be sure to check out my 3-part series on Fanuc Focas programming with C#! […]
Hi Kyle,
I tried running code in part 2 to create connection through ‘cnc_allclibhndl3’. But following error is received from Visual Studio:
System.DllNotFoundException: ‘Unable to load DLL ‘FWLIB32.dll’: The specified module could not be found. (Exception from HRESULT: 0x8007007E)’
Can I please have your advise on this? As I know FWLIB32.dl is written in C++, may I know is there any additional setting/code required so Visual Studio can run C# code with reference (e.g. cnc_allclibhndl3 etc.) to this dll? Thank you.
I’m getting the same error can anyone help?
Hi Nihal,
This error is caused by not having the correct DLLs on the computer you are trying to run the code on. Let’s say you are writing your code on your laptop. You are going to get that error any time you try to run the code on your laptop. However, if you transfer that program you wrote to the Fanuc HMI, it would run fine.
That being said, in order to resolve the issue you are having, you need to go into the Fanuc HMI, go to the Windows Desktop, and then open a folder to C:\Windows\System32. From there look for a bunch of DLLs that start with ‘fwlib’. So for instance you will see things like fwlib30i.dll and a bunch of other ones. Just copy all of those DLLs, put them on a USB key and bring them to your laptop. Plug in the USB to your laptop and copy all those DLLs into the C:\Windows\System32 folder on your laptop.
After you have done this, the program should run fine on your laptop. Keep in mind that if you bring your program to another computer that is not a Fanuc HMI, you will need the DLLs on that computer as well. Let me know if that fixes your issue. If you have any more questions, feel free to visit our forum. There is a Fanuc Focas section in there where you can ask questions and get more in depth answers. Here is the link https://hierthinking.com/forum/forum/cnc-focas/
dediğiniz işlemi uyguladım aynı hatayı alıyorum başka bir öneriniz varmı
I followed the process you said and I get the same error. Do you have any other suggestions?
Hi Nihal,
Sorry, that was my mistake. Since these are 32-bit libraries, they actually need to go to C:\Windows\SysWow64\. Move them to that folder and it should work for you. Sorry about that.
i did it this way but i get the same error
Make sure that one of the DLLs you are dropping into that folder is the fwlib32.dll. If not, you will need to move that over to C:\Windows\SysWOW64\ as well.
i get the same error. When I couldn’t add the file from the add reference section, I did it with drag and drop, probably because of it. How should I refer the libraries normally?
The error has been fixed, thank you very much for your attention.
Hi, I m trying this example. But i can’t comminicate with Fanuc Cnc. When I run the code, it giving me -3 value. How did you solve this problem.
CNC_RDAXISDATA from within the Focas library | POSITION I want to pull this function using the ready-made function below, but I am getting an error in the byte part, can you help?
Absolute position, machine position, relative position, distance to go,
of all axes are got and displayed.
class example
{
public void sample()
{
Focas1.ODBAXDT_data pos = new Focas1.ODBAXDT_data();
short[] types = { 0, 1, 2, 3 };
short len = Focas1.MAX_AXIS;
byte[] bytes = new byte[Marshal.SizeOf(pos) * 4 * Focas1.MAX_AXIS];
IntPtr ptrWork = Marshal.AllocCoTaskMem(Marshal.SizeOf(pos));
short ret = Focas1.cnc_rdaxisdata(h, 1, types, 4, ref len, bytes);
if (ret == Focas1.EW_OK)
{
int i;
Console.WriteLine(“ABSOLUTE:”);
for (i = 0 * Focas1.MAX_AXIS; i < 0 * Focas1.MAX_AXIS + len; i++)
{
Marshal.Copy(bytes, i * Marshal.SizeOf(pos), ptrWork, Marshal.SizeOf(pos));
Marshal.PtrToStructure(ptrWork, pos);
Console.WriteLine("{0} = {1}", pos.name, pos.data);
}
Console.WriteLine("MACHINE:");
for (i = 1 * Focas1.MAX_AXIS; i < 1 * Focas1.MAX_AXIS + len; i++)
{
Marshal.Copy(bytes, i * Marshal.SizeOf(pos), ptrWork, Marshal.SizeOf(pos));
Marshal.PtrToStructure(ptrWork, pos);
Console.WriteLine("{0} = {1}", pos.name, pos.data);
}
Console.WriteLine("RELATIVE:");
for (i = 2 * Focas1.MAX_AXIS; i < 2 * Focas1.MAX_AXIS + len; i++)
{
Marshal.Copy(bytes, i * Marshal.SizeOf(pos), ptrWork, Marshal.SizeOf(pos));
Marshal.PtrToStructure(ptrWork, pos);
Console.WriteLine("{0} = {1}", pos.name, pos.data);
}
Console.WriteLine("DISTANCE TO GO:");
for (i = 3 * Focas1.MAX_AXIS; i < 3 * Focas1.MAX_AXIS + len; i++)
{
Marshal.Copy(bytes, i * Marshal.SizeOf(pos), ptrWork, Marshal.SizeOf(pos));
Marshal.PtrToStructure(ptrWork, pos);
Console.WriteLine("{0} = {1}", pos.name, pos.data);
}
}
Marshal.FreeCoTaskMem(ptrWork);
}
}
[…] is the first video in a new series that will build on the It’s Time to Focas series of blog posts. In this video you will get an introduction to the Fanuc Focas API using C# […]
[…] It’s Time to Focas – Part 1: https://hierthinking.com/2020/04/25/its-time-to-focas-p1/ […]
[…] It’s Time to Focas – Part 1: https://hierthinking.com/2020/04/25/its-time-to-focas-p1/ […]
[…] It’s Time to Focas – Part 1: https://hierthinking.com/2020/04/25/its-time-to-focas-p1/ […]
[…] It’s Time to Focas – Part 1: https://hierthinking.com/2020/04/25/its-time-to-focas-p1/ […]
[…] It’s Time to Focas – Part 1: https://hierthinking.com/2020/04/25/its-time-to-focas-p1/ […]