Click here to Skip to main content
15,867,870 members
Articles / Programming Languages / C#

Atlas Copco PowerFocus (3000/4000) / NutRunner Tools Ethernet Communication Using C#

Rate me:
Please Sign up or sign in to vote.
4.91/5 (23 votes)
2 Mar 2015CPOL4 min read 134.7K   4.6K   19   45
Open protocol using C# socket programming for NutRunner

Image 1

Introduction

In one of my projects, I had an opportunity to create an Atlas Copco PowerFocus (3000/4000) Nutrunner Tools communication using C# socket programming. When I started the Nutrunner tool communication program, it was like a blank page. I did not have an idea of how to complete the task; I was Googling for a couple of days but couldn’t find a sample code to communicate with Nutrunner tools using C#. Finally, I got a solution using an Open protocol. Today, I will explain to you what is C# socket communication program for Nutrunner tool communication.

You can download the Open protocol document from this website.

Atlas Copco OpenProtocol Forum

Communication Messages

MID 0001 Communication Start

This message enables the communication. The controller does not respond to any other command before this:

  • Message sent by: Integrator
  • Answers: MID 0002 Communication start acknowledge or MID 0004 Command error, Client already connected.

If the communication is accepted, we can receive the result as MID 0002, if not, we receive the MID 0004. For more details about Open protocol, please review the attached Open protocol PDF document.

C# Socket Communication Program for Nutrunner Tool Communication

  1. Connect
  2. Communicate
  3. Data send and receive
  4. Disconnect

The detailed code will be listed below.

Background

The main aim was to develop a simple and easy to use .NET communication program for Atlas Copco Power Focus (3000/4000) – Nutrunner Tool.

Using the Code

The code is very simple and has comments on all functions. The main aim is to make the program very simple and easy to use; all the functions have been well commented in the project.

C#
using System.Net;
using System.IO;
using System.Net.Sockets;
C#
Socket server;// Declare your Socket
IPEndPoint ip; // Declare your IPEndpoint

For connection, we need the Nutrunner tool IP address. Here my tool's IP address is “10.126.224.186” and the default port for communication is “4545”. Cross check with your hardware engineer for the port used on your tools. Here, we use the socket communication program for connection. After the connection is OK, we need to send the Communication Start signal to the NutRunner tool “ MID 0001” – (“00200001000000000000#).

Server.Send()

C#
String szData = "00200001000000000000#";//Nutrunner Communication Start
byte[] byData = Encoding.Default.GetBytes(szData);//convert the string as Byte
int sent = server.Send(byData, SocketFlags.None);

server.Receive()

C#
byte[] bytesFrom = new byte[1025];
int iRx = server.Receive(bytesFrom);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
String resultchk = dataFromClient.Substring(4, 4).ToString();

If we receive MID 0002 as result, then the communication start is successful. If we receive MID 0004 as result, then there is an error in the communication start. The detail connect functions:

C#
private void button1_Click(object sender, EventArgs e)
{
    try
    {
        server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        String ipaddress = "10.126.224.186";
        ip = new IPEndPoint(IPAddress.Parse(ipaddress), 4545);
        server.Connect(ip);
        
        label2.BackColor = System.Drawing.Color.GreenYellow;
        label2.Text = "Connected";
        connectServer();
    }
    catch (Exception ex)
    {
    }
}

private void connectServer()
{
    try
    {
        if (server.Connected == false)
        {
            label2.BackColor = System.Drawing.Color.Red;
            server = new Socket(AddressFamily.InterNetwork, 
                                SocketType.Stream, ProtocolType.Tcp);
            String ipaddress = textips.Text.Trim();
            ip = new IPEndPoint(IPAddress.Parse(ipaddress), 4545);
            server.Connect(ip);
            label2.BackColor = System.Drawing.Color.GreenYellow;
            label2.Text = "Connected";
        }
        if (server.Connected == true)
        {
            String szData = "00200001000000000000#";          //Nutrunner Communication Start
            byte[] byData = Encoding.Default.GetBytes(szData);//System.Text.Encoding.ASCII.
                                                              //GetBytes(szData);

            int sent = server.Send(byData, SocketFlags.None);
            byte[] bytesFrom = new byte[1025];
            //  string bytesSent = server.Send(
            int iRx = server.Receive(bytesFrom);
            string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
            String resultchk = dataFromClient.Substring(4, 4).ToString();

            ////String szData = "00200060000000000000#";          //Nutrunner 
                                                                  //Communication Start
            ////byte[] byData = Encoding.Default.GetBytes(szData);//System.Text.Encoding.
                                                                  //ASCII.GetBytes(szData);
            ////int sent = server.Send(byData, SocketFlags.None);
            timer1.Enabled = true;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

Here in my program, I have used a Timer control for Torque data receive. In the timer, we frequently check for the connection. If connection fails, we reconnect to the NutRunner tools. To receive the Torque data, we use the message ID MID 0060. For Torque data receive, we send the MID 0060 to the Nutrunner tool.

MID 0060 Last Tightening Result Data Subscribe

Set the subscription for the result tightening. The result of this command will be the transmission of the tightening result after the tightening is performed (push function). The MID revision in the header is used to subscribe to different revisions of MID 0061 Last tightening result data upload reply.

Send data to get Torque result:

C#
String szData = "00200060000000000000#";//Torque data Request Send
byte[] byData = Encoding.Default.GetBytes(szData);
int sent = server.Send(byData, SocketFlags.None);

Receive Torque Result

C#
byte[] bytesFrom = new byte[1025];
int iRx = server.Receive(bytesFrom);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
String resultchk = dataFromClient.Substring(4, 4).ToString();

We receive MID 0061 as result with the Torque data. We will receive the result data something like this: "023100610010 010001020103airbag7 04KPOL3456JKLO897 05000600307000008000009010011112000840 130014001400120015000739160000017099991800000 1900000202001-06-02:09:54:09212001-05-29:12:34:3322123345675 NUL". Format the result string and get the Torque final result. For example, I have used the substring and get the final torque result here:

C#
txtfinaltorque.Text = dataFromClient.Substring(132, 6).ToString();

The detailed timer code:

C#
private void timer1_Tick(object sender, EventArgs e)
{
    try
    {
        if (server.Connected == false)
        {
            label2.BackColor = System.Drawing.Color.Red;
            label2.Text = "Not Connected";
            server.Disconnect(true);
            server.Close();
            server.Dispose();
            connectServer();
        }
        String szData = "00200060000000000000#";//Nutrunner Communication Start
        byte[] byData = Encoding.Default.GetBytes(szData);//System.Text.Encoding.ASCII.
                                                          //GetBytes(szData);

        int sent = server.Send(byData, SocketFlags.None);

        byte[] bytesFrom = new byte[1025];
        //  string bytesSent = server.Send(
        int iRx = server.Receive(bytesFrom);

        string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
        String resultchk = dataFromClient.Substring(4, 4).ToString();
        if (resultchk == "0061")
        {
            label2.BackColor = Color.Violet;
            label2.Text = "Torque Data Readed ";
            textBox1.Text = dataFromClient.ToString();

            txtcellid.Text = dataFromClient.Substring(22, 4).ToString();

            txtchannelid.Text = dataFromClient.Substring(28, 2).ToString();

            txtjobid.Text = dataFromClient.Substring(86, 2).ToString();

            txtControlorName.Text = dataFromClient.Substring(32, 25).ToString();

            txtBatchSize.Text = dataFromClient.Substring(95, 4).ToString();

            txtBatchCount.Text = dataFromClient.Substring(101, 4).ToString();

            txttighteningstatus.Text = dataFromClient.Substring(107, 1).ToString();
            if (txttighteningstatus.Text.Trim() == "0")
            {
                txttighteningstatusok.Text = "NOK";
            }
            else
            {
                txttighteningstatusok.Text = "OK";
            }

            txttorquestatus.Text = dataFromClient.Substring(110, 1).ToString();

            if (txttorquestatus.Text.Trim() == "0")
            {
                txttorquestatusok.Text = "Low";
            }
            else if (txttorquestatus.Text.Trim() == "1")
            {
                txttorquestatusok.Text = "OK";
            }
            else 
            {
                txttorquestatusok.Text = "High";
            }

            txtanglestatus.Text = dataFromClient.Substring(113, 1).ToString();

            txttorqueminlimit.Text = dataFromClient.Substring(116, 6).ToString();

            txttorquemax.Text = dataFromClient.Substring(124, 6).ToString();

            txtfinaltorque.Text = dataFromClient.Substring(132, 6).ToString();


            txttorque.Text = dataFromClient.Substring(142, 2).ToString() + "." + 
              dataFromClient.Substring(144, 2).ToString();//dataFromClient.Substring(140,6).
                                                          //ToString()

            txtTighteningID.Text = dataFromClient.Substring(221, 10).ToString();


            String szData3 = "00200062000000000000#";           //Nutrunner Communication Start
            byte[] byData3 = Encoding.Default.GetBytes(szData3);//System.Text.Encoding.ASCII.
                                                                //GetBytes(szData);

            int sent3 = server.Send(byData3, SocketFlags.None);
        }
    }
    catch (Exception ex)
    {
    }
}

Check for the connection if it's connected, then disconnect the communication in the detailed code below:

C#
private void button2_Click(object sender, EventArgs e)
{
    if (server.Connected == true)
    {
        label2.BackColor = System.Drawing.Color.Red;
        label2.Text = "Not Connected";
        server.Disconnect(true);
        server.Close();
        server.Dispose();
        clears();
    }
}
  1. First, we start with namespace declarations
  2. Variable declarations
  3. Connect (to connect the Nutrunner tool for communication)
  4. Torque Data Receive
    • Message sent by: Integrator
    • Answer: MID 0005 Command accepted or MID 0004 Command error, Last tightening subscription already exists or MID revision not supported
  5. Disconnect the connection

Points of Interest

I love to work and play with the HMI (Human Interface Program). I have worked with several HML programs using C# like PLC, sensor programming, and Nutrunner tool communication program. While working with this program, I didn’t get the proper guides to achieve the result, but finally I got it to work. I want end users who read this article to benefit from this program.

If you like my article, leave me a comment and vote for my article.

History

  • 26th February, 2015: Initial post

License

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


Written By
Team Leader
India India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog

My Interview on Microsoft TechNet Wiki Ninja Link

Comments and Discussions

 
QuestionConnection refused error while trying to connect to Torque Controller over a open protocol Pin
Member 1484735129-May-20 1:50
Member 1484735129-May-20 1:50 
PraiseThis is very helpful! Pin
azhararmy15-Nov-19 0:15
azhararmy15-Nov-19 0:15 
QuestionPF 4000 giving response as 0 Pin
Apurva Mande8-Jul-19 20:11
Apurva Mande8-Jul-19 20:11 
QuestionVB.NET Pin
Member 1310578516-Jul-17 9:35
Member 1310578516-Jul-17 9:35 
QuestionOther commands Pin
Xacobo20-Sep-16 3:50
Xacobo20-Sep-16 3:50 
GeneralError Pin
Member 1227033717-Jan-16 20:41
Member 1227033717-Jan-16 20:41 
GeneralRe: Error Pin
Member 1579871015-Oct-22 5:40
Member 1579871015-Oct-22 5:40 
QuestionMID 0064 Old tightening result upload request Pin
enjoywithme6-Sep-15 0:29
enjoywithme6-Sep-15 0:29 
QuestionMy vote of 5 Pin
Fastfootskater20-Mar-15 0:28
Fastfootskater20-Mar-15 0:28 
GeneralMy vote of 5! Pin
jediYL4-Mar-15 16:35
professionaljediYL4-Mar-15 16:35 
SuggestionMeh Pin
Barremob3-Mar-15 8:35
Barremob3-Mar-15 8:35 
GeneralRe: Meh Pin
syed shanu3-Mar-15 11:32
mvasyed shanu3-Mar-15 11:32 
GeneralMy vote of 4 Pin
vytheese3-Mar-15 5:45
professionalvytheese3-Mar-15 5:45 
Questionmy vote of 2 Pin
sx200828-Feb-15 8:45
sx200828-Feb-15 8:45 
QuestionPlease give due reference to source material Pin
super25-Feb-15 22:47
professionalsuper25-Feb-15 22:47 
AnswerRe: Please give due reference to source material Pin
syed shanu26-Feb-15 3:15
mvasyed shanu26-Feb-15 3:15 
GeneralRe: Please give due reference to source material Pin
Nelek2-Mar-15 7:05
protectorNelek2-Mar-15 7:05 
GeneralRe: Please give due reference to source material Pin
syed shanu2-Mar-15 9:22
mvasyed shanu2-Mar-15 9:22 
GeneralRe: Please give due reference to source material Pin
Nelek3-Mar-15 10:35
protectorNelek3-Mar-15 10:35 
GeneralRe: Please give due reference to source material Pin
syed shanu3-Mar-15 11:23
mvasyed shanu3-Mar-15 11:23 
QuestionThank you for sharing ..Do you any way to clear the actual pset on the controller? Pin
soniasolis22-Oct-14 11:19
soniasolis22-Oct-14 11:19 
QuestionDo you have examples of the use of Power Focus API? Pin
loveyuejing18-Aug-14 5:41
loveyuejing18-Aug-14 5:41 
AnswerRe: Do you have examples of the use of Power Focus API? Pin
syed shanu18-Aug-14 15:30
mvasyed shanu18-Aug-14 15:30 
GeneralRe: Do you have examples of the use of Power Focus API? Pin
loveyuejing19-Aug-14 3:24
loveyuejing19-Aug-14 3:24 
GeneralRe: Do you have examples of the use of Power Focus API? Pin
syed shanu19-Aug-14 13:35
mvasyed shanu19-Aug-14 13:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.