393 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			393 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | ||
| using System.Collections.Generic;
 | ||
| using System.ComponentModel;
 | ||
| using System.Data;
 | ||
| using System.Drawing;
 | ||
| using System.Linq;
 | ||
| using System.Text;
 | ||
| using System.Windows.Forms;
 | ||
| using OPCAutomation;
 | ||
| namespace epickHost
 | ||
| {
 | ||
|     public partial class OPCForm : Form
 | ||
|     {
 | ||
|         string computerName = "LAPTOP-KVQJES02";  
 | ||
|         OPCAutomation.OPCServer opcServer;
 | ||
|         OPCBrowser opcBrowser;       
 | ||
|         OPCGroups opcGroups;
 | ||
|         OPCGroup  opcGroupMoreBoxesW , opcGroupBarcodeR,opcGroupTerminalW;
 | ||
|         List<string> lstMoreBoxesItems = new List<string>() ;
 | ||
|         List<string> lstBarcodeItems = new List<string>();
 | ||
|         List<string> lstTerminalItems = new List<string>();
 | ||
|         int transactionId=1;
 | ||
|         Array serverHandlesMOreBox, serverHandlesBarcode, serverHandleTerminal;
 | ||
|         Array errors;
 | ||
|         int cancelId;
 | ||
|         Dictionary<string,string> varDic = new Dictionary<string,string>() ;
 | ||
|         Dictionary<int, Dictionary<string, string>> varTransactionDics = new Dictionary<int, Dictionary<string, string>>();
 | ||
|          
 | ||
|         public OPCForm()
 | ||
|         {
 | ||
|             InitializeComponent();
 | ||
|             opcServer = new OPCAutomation.OPCServer();
 | ||
|             object serverList = opcServer.GetOPCServers(computerName);
 | ||
|             foreach (var item in (Array)serverList)
 | ||
|             {
 | ||
|                 if (!listServers.Items.Contains(item))
 | ||
|                 {
 | ||
|                     listServers.Items.Add(item);
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|           
 | ||
|         }
 | ||
|       
 | ||
| 
 | ||
|         private void btnConnect_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 opcServer.Connect(listServers.SelectedItem.ToString(), computerName);
 | ||
|                 showInfo("连接成功"); 
 | ||
|                 initialGroups();
 | ||
|             }catch(Exception ex)
 | ||
|             {
 | ||
|                 showInfo(ex.Message);
 | ||
|             }
 | ||
|         }
 | ||
|          
 | ||
|         private void initialGroups()
 | ||
|         {
 | ||
|             opcBrowser = opcServer.CreateBrowser();
 | ||
|             opcBrowser.ShowBranches();
 | ||
|             opcBrowser.ShowLeafs(true);
 | ||
|             opcGroups = opcServer.OPCGroups;
 | ||
| 
 | ||
|             opcGroupMoreBoxesW = opcGroups.Add("NeedMoreBoxes");
 | ||
|             opcGroupMoreBoxesW.IsActive=true;
 | ||
|             opcGroupMoreBoxesW.IsSubscribed = true;
 | ||
|             opcGroupMoreBoxesW.UpdateRate = 200;
 | ||
|             opcGroupMoreBoxesW.AsyncCancelComplete += OpcGroupMoreBoxesW_AsyncCancelComplete;  
 | ||
|             opcGroupMoreBoxesW.AsyncReadComplete += OpcGroupMoreBoxesW_AsyncReadComplete; ;
 | ||
|             opcGroupMoreBoxesW.AsyncWriteComplete += OpcGroupMoreBoxesW_AsyncWriteComplete;
 | ||
| 
 | ||
| 
 | ||
|             opcGroupBarcodeR = opcGroups.Add("readBoxBarcode");
 | ||
|             opcGroupBarcodeR.IsActive = true;
 | ||
|             opcGroupBarcodeR.IsSubscribed = true;
 | ||
|             opcGroupBarcodeR.UpdateRate = 200;
 | ||
|             opcGroupBarcodeR.AsyncCancelComplete += OpcGroupBoxBarcodeR_AsyncCancelComplete;  
 | ||
|             opcGroupBarcodeR.AsyncReadComplete += OpcGroupBoxBarcodeR_AsyncReadComplete;
 | ||
|             opcGroupBarcodeR.AsyncWriteComplete += OpcGroupBoxBarcodeR_AsyncWriteComplete;
 | ||
| 
 | ||
| 
 | ||
|             opcGroupTerminalW = opcGroups.Add("writeTerminial");
 | ||
|             opcGroupTerminalW.IsActive = true;
 | ||
|             opcGroupTerminalW.IsSubscribed = true;
 | ||
|             opcGroupTerminalW.UpdateRate = 200;
 | ||
|             opcGroupTerminalW.AsyncCancelComplete += OpcGroupBoxTerminalW_AsyncCancelComplete;  
 | ||
|             opcGroupTerminalW.AsyncReadComplete += OpcGroupBoxTerminalW_AsyncReadComplete; 
 | ||
|             opcGroupTerminalW.AsyncWriteComplete += OpcGroupBoxTerminalW_AsyncWriteComplete;  
 | ||
| 
 | ||
|             listItems.Items.Clear();
 | ||
|             foreach (var item in opcBrowser)
 | ||
|             {
 | ||
|                if(item.ToString().Contains("ModbusRtu"))
 | ||
|                 listItems.Items.Add(item);
 | ||
|             }
 | ||
|            
 | ||
|                 
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupBoxTerminalW_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)
 | ||
|         {
 | ||
|             lbTerminal.Text = String.Format(" transactionId:{0},numItems:{1}", TransactionID, NumItems);
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupBoxTerminalW_AsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors)
 | ||
|         {
 | ||
|             throw new NotImplementedException();
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupBoxTerminalW_AsyncCancelComplete(int CancelID)
 | ||
|         {
 | ||
|             throw new NotImplementedException();
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupMoreBoxesW_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)
 | ||
|         {
 | ||
|             lbMoreBoxes.Text = String.Format(" transactionId:{0},numItems:{1}", TransactionID, NumItems);
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupMoreBoxesW_AsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors)
 | ||
|         {
 | ||
|            // throw new NotImplementedException();
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupMoreBoxesW_AsyncCancelComplete(int CancelID)
 | ||
|         {
 | ||
|             throw new NotImplementedException();
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupBoxBarcodeR_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)
 | ||
|         {
 | ||
|             lbBarcode.Text = String.Format(" transactionId:{0},numItems:{1}", TransactionID, NumItems);
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupBoxBarcodeR_AsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors)
 | ||
|         {
 | ||
|             varDic = new Dictionary<string, string>();
 | ||
|             showInfo("读取结束");
 | ||
|             StringBuilder sb = new StringBuilder();
 | ||
|             sb.Append(TransactionID + " -> ");
 | ||
|             for (int i = 1; i <= NumItems; i++)
 | ||
|             {
 | ||
|                 int index = Convert.ToInt32(ClientHandles.GetValue(i)) - 0;
 | ||
|                 string item = lstBarcodeItems[index];
 | ||
|                 string value = ItemValues.GetValue(i).ToString();
 | ||
|                 sb.Append(item).Append(":").Append(value).Append("; ");
 | ||
|                 varDic[item] = value;
 | ||
|             }
 | ||
|             varTransactionDics[TransactionID] = varDic;
 | ||
|             showResult(sb.ToString());
 | ||
|             lbBarcode.Text = sb.ToString();
 | ||
|             setBoxTerminal(TransactionID);
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         private void setBoxTerminal(int transactionID)
 | ||
|         {
 | ||
|             Dictionary<string, string> varDic = new Dictionary<string, string>();
 | ||
|             varDic = varTransactionDics[transactionId];
 | ||
|              
 | ||
|             foreach (string item in varDic.Values)
 | ||
|             {
 | ||
|                 //TO process wms function 
 | ||
|                 int barcode = Convert.ToInt32(item);
 | ||
|                 string firstTerminal = "02"; //货区
 | ||
|                 string secondTerminal= "04";//复核
 | ||
|                 string plcTerminal = barcode + firstTerminal + secondTerminal;
 | ||
|                 asyncWriteTerminal(plcTerminal);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         private void asyncWriteTerminal(string terminal)
 | ||
|         {
 | ||
|             putItems(lstTerminalItems, opcGroupTerminalW,ref serverHandleTerminal);
 | ||
|             cnt = 0;
 | ||
|             Array newValues = new object[] { 0, terminal };
 | ||
| 
 | ||
|             opcGroupTerminalW.AsyncWrite(lstTerminalItems.Count, serverHandleTerminal, newValues, out errors, ++transactionId, out cancelId);
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroupBoxBarcodeR_AsyncCancelComplete(int CancelID)
 | ||
|         {
 | ||
|             throw new NotImplementedException();
 | ||
|         }
 | ||
| 
 | ||
|         private void showResult(string v)
 | ||
|         {
 | ||
|             lbResult.Text = v;
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroup_AsyncCancelComplete(int CancelID)
 | ||
|         {
 | ||
|             throw new NotImplementedException();
 | ||
|         }
 | ||
| 
 | ||
|         void showInfo(string msg)
 | ||
|         {
 | ||
|             lbMsg.Text = msg;
 | ||
|         }
 | ||
|             
 | ||
|         
 | ||
|         private void btnAddItems_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
| 
 | ||
|             if (!lstMoreBoxesItems.Contains(listItems.SelectedItem.ToString())){
 | ||
|                 lstMoreBoxesItems.Add(listItems.SelectedItem.ToString());
 | ||
|                 listMoreBoxes.Items.Add((string)listItems.SelectedItem.ToString());
 | ||
|             }
 | ||
|             
 | ||
|         }
 | ||
| 
 | ||
|         private void btnRead_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             timer1.Interval = 300;
 | ||
|             timer1.Tick += Timer1_Tick;
 | ||
|           //  timer1.Enabled = !timer1.Enabled;
 | ||
|             btnWriteMoreBoxes.Text = timer1.Enabled ? "停止读取" : "开始读取";
 | ||
| 
 | ||
|            // asyncReadMOreBoxes();
 | ||
| 
 | ||
|              
 | ||
|         }
 | ||
| 
 | ||
|         int cnt;
 | ||
|         private void Timer1_Tick(object sender, EventArgs e)
 | ||
|         {
 | ||
|             //asyncReadMOreBoxes();
 | ||
|            // lbCycle.Text = "执行次数:" + (++cnt);
 | ||
|         }
 | ||
|             
 | ||
|         void putItemsMoreBoxes()
 | ||
|         {
 | ||
|             varDic.Clear();
 | ||
|             List<string> opcItemIds = new List<string>();
 | ||
|             List<int> clientHandles = new List<int>();
 | ||
|             List<int> values = new List<int>();    
 | ||
|             
 | ||
| 
 | ||
|             opcItemIds.Add("");
 | ||
|             clientHandles.Add(0);
 | ||
| 
 | ||
|             foreach(var item in lstBarcodeItems)
 | ||
|             {
 | ||
|                 opcItemIds.Add(item.ToString());
 | ||
|                 clientHandles.Add(lstBarcodeItems.IndexOf(item)+0);
 | ||
|                 varDic[item.ToString()] = null;
 | ||
|                  
 | ||
|             }
 | ||
|            // varTransactionDics[transactionId] = varDic;
 | ||
|             Array items = opcItemIds.ToArray();
 | ||
|             Array clienthandels =clientHandles.ToArray();
 | ||
| 
 | ||
|             opcGroupBarcodeR.OPCItems.AddItems(lstBarcodeItems.Count,ref items, ref clienthandels, out serverHandlesMOreBox,out errors);
 | ||
|         }
 | ||
|         void putItems(List<string> listItems,OPCGroup oPCGroup, ref Array serverHandles)
 | ||
|         {
 | ||
|           //  varDic=new Dictionary<string, string>();
 | ||
|             List<string> opcItemIds = new List<string>();
 | ||
|             List<int> clientHandles = new List<int>();
 | ||
|             List<int> values = new List<int>();
 | ||
| 
 | ||
| 
 | ||
|             opcItemIds.Add("");
 | ||
|             clientHandles.Add(0);
 | ||
| 
 | ||
|             foreach (var item in listItems)
 | ||
|             {
 | ||
|                 opcItemIds.Add(item.ToString());
 | ||
|                 clientHandles.Add(listItems.IndexOf(item) + 0);
 | ||
|            //     varDic[item.ToString()] = null;
 | ||
| 
 | ||
|             }
 | ||
|             // varTransactionDics[transactionId] = varDic;
 | ||
|             Array items = opcItemIds.ToArray();
 | ||
|             Array clienthandels = clientHandles.ToArray();
 | ||
| 
 | ||
|             oPCGroup.OPCItems.AddItems(listItems.Count, ref items, ref clienthandels, out serverHandles, out errors);
 | ||
|         }
 | ||
| 
 | ||
|         void asyncReadMOreBoxes()
 | ||
|         {
 | ||
|             putItems(lstMoreBoxesItems, opcGroupMoreBoxesW,ref serverHandlesMOreBox);
 | ||
|             varTransactionDics[++transactionId] = varDic;
 | ||
|             opcGroupMoreBoxesW.AsyncRead(lstMoreBoxesItems.Count, serverHandlesMOreBox, out errors, transactionId, out cancelId);
 | ||
|            
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         private void btnWrite_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             
 | ||
|         }
 | ||
| 
 | ||
|         void ayncWriteMoreBoxes()
 | ||
|         {
 | ||
|             putItems(lstMoreBoxesItems, opcGroupMoreBoxesW, ref serverHandlesMOreBox); 
 | ||
|             cnt = 0;
 | ||
|             Array newValues = new object[] { 0, chkBoxMoreboxes.Checked?1:0};
 | ||
|           //  varTransactionDics[++transactionId] = varDic;
 | ||
|             opcGroupMoreBoxesW.AsyncWrite(lstMoreBoxesItems.Count, serverHandlesMOreBox, newValues, out errors, ++transactionId, out cancelId);
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         private void OpcGroup_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)
 | ||
|         {
 | ||
|             showInfo("write completed");
 | ||
|         }
 | ||
| 
 | ||
|         private void btnBarcode_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
| 
 | ||
|             if (!lstBarcodeItems.Contains(listItems.SelectedItem.ToString()))
 | ||
|             {
 | ||
|                 lstBarcodeItems.Add(listItems.SelectedItem.ToString());
 | ||
|                 listBoxBarcode.Items.Add((string)listItems.SelectedItem.ToString());
 | ||
|             }
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         private void btnTerminal_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             if (!this.lstTerminalItems.Contains(listItems.SelectedItem.ToString()))
 | ||
|             {
 | ||
|                 lstTerminalItems.Add(listItems.SelectedItem.ToString());
 | ||
|                 listBoxTerminal.Items.Add((string)listItems.SelectedItem.ToString());
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         private void btnWriteMoreBoxes_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             ayncWriteMoreBoxes();
 | ||
|         }
 | ||
| 
 | ||
|         private void btnWriteTerminal_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             asyncWriteTerminal(txtTerminal.Text);
 | ||
|         }
 | ||
| 
 | ||
|         private void btnReadBarcode_Click(object sender, EventArgs e)
 | ||
|         {
 | ||
|             asyncReadBarcodes();
 | ||
|         }
 | ||
| 
 | ||
|         private void asyncReadBarcodes()
 | ||
|         {
 | ||
|             putItems(lstBarcodeItems, opcGroupBarcodeR, ref serverHandlesBarcode); 
 | ||
|             //varTransactionDics[++transactionId] = varDic;
 | ||
|             opcGroupBarcodeR.AsyncRead(lstBarcodeItems.Count, serverHandlesBarcode, out errors, ++transactionId, out cancelId);
 | ||
|           
 | ||
|         }
 | ||
| 
 | ||
|         private void OpcGroup_AsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors)
 | ||
|         {
 | ||
|             showInfo("读取结束");
 | ||
|             StringBuilder sb = new StringBuilder();
 | ||
|             sb.Append(TransactionID + " -> ");
 | ||
|             for (int i = 1; i <= NumItems; i++)
 | ||
|             {
 | ||
|                 int index = Convert.ToInt32(ClientHandles.GetValue(i)) - 0;
 | ||
|                 string item = lstMoreBoxesItems[index];
 | ||
|                 string value = ItemValues.GetValue(i).ToString();
 | ||
|                 sb.Append(item).Append(":").Append(value).Append("; ");
 | ||
|                 varDic[item] = value;
 | ||
|             }
 | ||
|             varTransactionDics[TransactionID] = varDic;
 | ||
|             showResult(sb.ToString());
 | ||
|            // processWmsFunctions(TransactionID);
 | ||
| 
 | ||
| 
 | ||
|         }
 | ||
|         
 | ||
|         /// <summary>
 | ||
|         /// 读取到周转箱条码
 | ||
|         /// 请求WMS,分配周转箱巷道任务,返回分配的弹出口,并写入plc
 | ||
|         /// </summary>
 | ||
|         /// <param name="transactionID"></param>
 | ||
|     
 | ||
|         private void needMoreBoxes(int transactionID)
 | ||
|         {
 | ||
|             //分配巷道任务给周转箱,并写入plc,plc将其弹出到对应货区的缓冲区 
 | ||
|             //系统分配波次任务,按货区顺序注册周转箱,并分别弹出到货区的缓冲区
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|         }
 | ||
|     }
 | ||
| }
 |