Kawasaki robot c# communication (transfer)

Since I am doing machine vision work in the industrial automation industry, in addition to mastering image processing, I also need to communicate with industrial robots. I recently learned the TCP/IP communication between the computer and the Kawasaki robot, so I recorded it here.

In addition to communicating directly with the robot, there is a way to communicate indirectly through the PLC. Attach another article of mine, which talks about the communication between c# and Mitsubishi PLC: C# communicates with Mitsubishi PLC

First assume a simple application situation. There is an industrial robot with a camera. When the robot moves to a certain position, it needs to send a signal, and then the vision system takes a picture and processes it, and finally sends the return signal back to the robot. This program is written based on this application. The following figure is the program on the robot teach pendant: after moving to the P1 point, send the photo signal photo=1 to the computer, and then the computer returns to over=1 after processing, and the robot moves to the P2 point after receiving it.

Image 2Image 2

The program on the robot teach pendant is very simple, and then the program on the computer is explained.

The first is to establish a TCP link between the computer and the robot, where the computer is the client and the robot is the server.

[csharp] view plain copy
 
  1. public  void ConnectRobot() //Link tcp  
  2.         {  
  3.             byte[] data = new byte[1024];  
  4.             newclient_Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  5.             string ipadd =  "192.168.30.238"; //robot IP address  
  6.             int port = Convert.ToInt32( "23"); //Port number  
  7.             IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port);  
  8.             try  
  9.             {  
  10.                 newclient_Client.Connect(ie); //establish a connection  
  11.                 Connected_Client =  true; //Connection flag           
  12.                 btnConn.Enabled = false;  
  13.                 sendToRobot( "as\r\n"); //After the connection is established, first send as, then you can send as language commands through the computer  
  14.                 SetTishi( "Robot link successfully..."); //Display  
  15.   
  16.   
  17.             }  
  18.             catch (SocketException e)  
  19.             {  
  20.                 SetTishi( "Failed to connect to robot" + e.Message);  
  21.                 return;  
  22.             }  
  23. <span style= "white-space:pre;"> </span> //Used to monitor whether the robot sends data  
  24.             ThreadStart myThreaddelegate = new ThreadStart(ReceiveMsg);  
  25.             myThread_client = new Thread(myThreaddelegate);  
  26.             myThread_client.Start();  
  27.   
  28.   
  29.         }  

The basic knowledge of TCP communication is not covered here. What is to be explained here is why there is a sentence "as" sent to the robot after connect. In the Kawasaki Robot E series TCP/IP communication manual, there are the following contents:

Picture 1

That is, after connecting "as" to the robot first, the AS language command of the robot can be sent directly from the computer. In this way, the robot program can be controlled from the computer.

After the connection is established, all you have to do is send and receive messages. It is very simple for the computer to send a message to the robot, because the computer can send the as language command to the robot, so it is a simple TCP to send a message, such as directly sending "over=1", the robot can get it. What needs to be said is that the robot sends a message to the computer. In this program, the method of establishing a TCP server in the robot and then monitoring it is not adopted, but the function is implemented in the computer. This method uses the list command in the as language of Kawasaki robot. The function of the list command is to display all program steps and variables. To query the value of the variable photo, you only need to send "list/r photo" to the robot, so that you can get The value of the variable photo. In this way, the computer terminal can be continuously queried to achieve the effect of sending data by the robot, thereby simplifying the robot program and reducing the complexity. Here is the monitoring code:

 

[csharp] view plain copy
 
  1. public void ReceiveMsg()//监听tcp  
  2.         {  
  3.   
  4.             int thelastData = 999;  
  5.             while (true)  
  6.             {  
  7.                 try  
  8.                 {  
  9.   
  10.                     byte[] data = new byte[1024];  
  11.                     int recv = newclient_Client.Receive(data); //Receive message  
  12.                     string stringdata = Encoding.UTF8.GetString(data, 0, recv); //Convert bytes to characters  
  13.                     //string stringdata = Encoding.Unicode.GetString(data, 0, recv);  
  14.                     if (stringdata ==  "") //Determine whether to disconnect  
  15.                     {  
  16.                         SetTishi( "The server has exited");  
  17.                         newclient_Client.Close();  
  18.                         Connected_Client = false;  
  19.                         break;  
  20.                     }  
  21.                     //Using the command list of the AS language to detect variables to implement the monitoring function  
  22.                     //Because there are other characters returned by sending 'list/r photo' to monitor the value of the variable photo, so remove these characters  
  23.                     stringdata = stringdata.Trim();  
  24.                     string strTemp = "photo    =";  
  25.                     int iCount = stringdata.IndexOf(strTemp); //Get the length of extra characters  
  26.                     if (iCount > 0)  
  27.                     {  
  28.                         string read = stringdata.Substring(strTemp.Length + iCount + 1, 2); //Remove extra characters and photo  
  29.                         int kk =  int.Parse(read); //Get the value of the variable photo  
  30.                         if (kk != 0)  
  31.                         {  
  32.   
  33.                             if (thelastData != kk) //The signal is only considered to receive the signal from 0->1  
  34.                             {  
  35.                                 SetTishi( "Received a camera signal from the robot...");  
  36.                                 sendToRobot( "over=1\r\n"); //Signal to the robot to complete  
  37.                                 SetTishi( "Received message: " + stringdata);  
  38.                             }  
  39.                         }  
  40.                         thelastData = kk;  
  41.                     }  
  42.                 }  
  43.                 catch  
  44.                 {  
  45.   
  46.                 }  
  47.                 sendToRobot( "list/R photo\r\n"); //Send the as language command to the robot to query the value of photo  
  48.                 Thread.Sleep(100);  
  49.   
  50.             }  
  51.         }  

By continuously obtaining the value of the variable photo from the robot, when photo=1 is detected, the completion signal over=1 can be sent to the robot, and then the robot program will be executed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324844169&siteId=291194637