usc2019-RobotSim

實踐-機器手臂程式設計

View on GitHub

四、WtFramework 開發框架

Winform Client 端通訊程式

  1. WtFramework開發框架介紹
  2. 安裝
    • 於 WorkVisual 內點選 File/Cataloghandling…
    • 於 Catalogs 視窗點選 KRL Templates 後按 > 按鈕 加入功能
    • Image
    • 將 WtFramework.zip 解壓縮後將 KUKA Templates資料夾 覆蓋 C:\Users\User\Documents\KUKA Templates 資料夾
  3. 網路通訊
<ETHERNETKRL>
   <CONFIGURATION>
      <EXTERNAL>
         <TYPE>Client</TYPE>			
      </EXTERNAL>
      <INTERNAL>
         <IP>192.168.1.147</IP>			<!--設定手臂IP-->
         <PORT>54600</PORT>				<!--設定連線串口-->
         <ALIVE Set_Flag="1"/>			<!--當確定連線後Flag[1] = TRUE-->
      </INTERNAL>
   </CONFIGURATION>
   <RECEIVE>
      <XML>
         <ELEMENT Tag="Data/Direction" Type="INT" Set_Flag="2"/>   <!--設定接收到的資料 Tag="路徑" Type="資料型別" 接收資料後Flag[2]=TRUE-->
      </XML>
   </RECEIVE>
   <SEND>
      <XML>
         <ELEMENT Tag="Result/Answer" Type="STRING"/>  <!--設定輸出資料-->
      </XML>
   </SEND>
</ETHERNETKRL>




  1. 夾娃娃機PC端操作介面
    • 步驟
    1. 介面設計
    2. 開啟關閉功能
    3. 建立按鈕傳送訊息
    4. 註冊Click事件
    5. 傳送接收
    6. 測試及改寫
 		Client client = new Client();
		private void buttonStart_Click(object sender, EventArgs e)
		{
			client.Start("127.0.0.1", 54600);
		}

		private void buttonStop_Click(object sender, EventArgs e)
		{
			client.Stop();
		}
	class Client
	{
		TcpClient myClient;         //建立TcpClient

		public Client() { }


		public void Start(string ip, int port)
		{
			if (myClient != null)                   //myClient有被new出來的話 結束
			{
				return;
			}
			myClient = new TcpClient(ip, port);     //設定Ip跟Port
			Task.Run(() => ClientService());        //在另一個執行續中執行  ClientService()  ; 由電腦決定是否產生新執行續
		}

		public void Stop()
		{
			if (myClient != null)
			{
				myClient.Close();       //關閉Client
				myClient = null;
			}
		}
	}
		private void SetData(string command)
		{
			client.Send(command);
		}

		private void buttonFront_Click(object sender, EventArgs e)
		{
			SetData("1");
		}

		private void buttonBack_Click(object sender, EventArgs e)
		{
			SetData("2");
		}
		
		private void buttonLeft_Click(object sender, EventArgs e)
		{
			SetData("3");
		}

		private void buttonRight_Click(object sender, EventArgs e)
		{
			SetData("4");
		}

		private void buttonGet_Click(object sender, EventArgs e)
		{
			SetData("5");
		}
  	private string sendData = string.Empty;
		
		public void Send(string data)
		{
			sendData = data;
		}

		private void ClientService()
		{
			while (true)
			{

				try
				{

					if (myClient != null)           //myClient有被new出來
					{
						StreamReader streamReader = new StreamReader(myClient.GetStream());     //建立StreamReader
						StreamWriter streamWriter = new StreamWriter(myClient.GetStream());     //建立StreamWriter

						while (myClient.Connected)      //myClient連線
						{
							if (sendData != string.Empty)       //sendData不是空字串
							{
								streamWriter.WriteLine(sendData);       //寫出sendData資料
								streamWriter.Flush();                   //傳送
								Console.WriteLine("Client To Server : " + sendData);

								var data = streamReader.ReadLine();     //讀取Server端傳回資料
								Console.WriteLine("From Server : " + data);

								sendData = string.Empty;
							}
							SpinWait.SpinUntil(() => { return false; }, 10);    //等待0.01秒
						}
					}
				}
				catch (Exception ex)        // 執行try發生錯誤
				{
					Console.WriteLine(ex.ToString());       //印出錯誤訊息
					break;
				}
			}
		}

模擬程式下載: https://github.com/yazelin/usc2019-RobotSim/raw/master/src/Play.zip
解壓縮後執行 USC2019RobotSim.exe

IP 127.0.0.1 
Port 54600
前 <Data><Direction>1</Direction></Data>
後 <Data><Direction>2</Direction></Data>
左 <Data><Direction>3</Direction></Data>
右 <Data><Direction>4</Direction></Data>
夾 <Data><Direction>5</Direction></Data>