using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Net;
|
using System.Net.Sockets;
|
using System.Windows.Forms;
|
using System.Threading;
|
using System.Net.NetworkInformation;
|
using DevComponents.DotNetBar;
|
using System.Runtime.InteropServices;
|
using System.Data;
|
using com.force.json;
|
using HslCommunication.Profinet.Siemens;
|
using HslCommunication;
|
using HslCommunication.Core.Net;
|
|
namespace WCS
|
{
|
/// <summary>
|
/// PLCA线程
|
/// </summary>
|
class PlcAThread
|
{
|
//NetworkStream myNetworkStream;
|
//Thread thr = null;
|
delegate void displayresponse(string text);
|
//delegate void displayinseresponse(int val);
|
string recvStr = "";
|
string plcip = "";
|
int plcno = 0;
|
Thread trd = null;
|
int i_plc_step = 0;
|
int plcport = 0;
|
main form1;
|
public static string plc_cmd = "";
|
//delegate void SetTextCallback(string text);
|
delegate void DisplayData(int type, int i);
|
//delegate void DisplayQuereData(int count);
|
delegate void DisplaySendTxtInfo(string info);
|
//delegate void DisplayErrData(int i);
|
//delegate void DisplayDataBuffer(int k);
|
|
private SiemensS7Net siemensTcpNet = null;
|
private OperateResult connect = null;
|
|
#region 画面上同步显示控件
|
public ListView plclist;
|
//public ListView plcbuflist;
|
//public DevComponents.DotNetBar.SuperGrid.SuperGridControl plcbuflist;
|
public ListBox tb;
|
//public LabelX lb_backMsg;
|
//public DevComponents.DotNetBar.LabelX querecount;
|
//public ListView errlist;
|
public LabelX sendtoplc;
|
//public DevComponents.DotNetBar.PanelEx os;
|
//public Panel os;
|
//public Microsoft.VisualBasic.PowerPacks.OvalShape mvpo;
|
#endregion
|
|
|
public PlcAThread(int plc_no, string plc_ip, int port, main f)
|
{
|
try
|
{
|
plcno = plc_no;
|
plcip = plc_ip;
|
plcport = port;
|
form1 = f;
|
|
siemensTcpNet = new SiemensS7Net(SiemensPLCS.S1200);
|
siemensTcpNet.IpAddress = this.plcip;
|
siemensTcpNet.Port = this.plcport;
|
siemensTcpNet.Rack = 0;
|
siemensTcpNet.Slot = 0;
|
siemensTcpNet.ConnectClose();
|
|
trd = new Thread(new ThreadStart(this.ThreadTask));
|
trd.IsBackground = true;
|
trd.Start();
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/PlcThread--PLC线程启动出错:" + em.Message);
|
}
|
}
|
|
/// <summary>
|
/// 断开连接后,重新连接
|
/// </summary>
|
/// <returns></returns>
|
private bool ReConnect()
|
{
|
bool result = false;
|
try
|
{
|
siemensTcpNet.ConnectClose();
|
connect = siemensTcpNet.ConnectServer();
|
if (connect.IsSuccess)
|
{
|
result = true;
|
DisplayInfo("【" + DateTime.Now.ToString() + "】PLCA重新连接PLC成功 ");
|
}
|
else
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】PLCA重新连接PLC失败---" + connect.ToMessageShowString());
|
}
|
}
|
catch (Exception em)
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】PLCA重新连接PLC失败---" + em.Message);
|
}
|
return result;
|
}
|
|
/// <summary>
|
/// 根据命令字符串判断作业类型
|
/// </summary>
|
private void CommandStr_nkindstep()
|
{
|
int i = 0;
|
try
|
{
|
if (plc_cmd == null || plc_cmd == "")
|
{
|
i = 1;
|
}
|
else
|
{
|
i = Convert.ToInt32(plc_cmd.Substring(0, 2));
|
}
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/CommandStr_nkindstep--根据命令字符串判断作业类型出错:" + em.Message);
|
return;
|
}
|
switch (i)
|
{
|
case 1:
|
i_plc_step = 1;
|
break;
|
case 2:
|
i_plc_step = 2;
|
break;
|
case 3:
|
i_plc_step = 3;
|
break;
|
case 4:
|
i_plc_step = 4;
|
break;
|
case 5://清buffer
|
i_plc_step = 5;
|
break;
|
case 6://插单
|
i_plc_step = 6;
|
break;
|
default:
|
i_plc_step = 1;
|
break;
|
}
|
}
|
|
/////////////////////////////////////---------------------------------------读plc中字-----------------------------
|
/// <summary>
|
/// 读PLC id
|
/// </summary>
|
private void GetStaIdArea()
|
{
|
string s_plchead = "", s_chksum = "", s_SendString = "";
|
string s_RecStr = "", s_startadd = "", s_len = "";
|
byte[] buf = new byte[12];
|
byte[] buf1 = new byte[500];
|
byte[] content = new byte[200];
|
int n = 0, num = 0, num1 = 0, k = 0, pi_return = 0, i_startadd = 0, i_len = 0, i_val = 0;
|
byte[] bte_value = new byte[4];
|
byte[] bte_addr = new byte[2];
|
s_RecStr = "";
|
recvStr = "";
|
try
|
{
|
s_startadd = "DB100.0"; //读取堆垛机状态起始地址
|
i_len = 60; //读取堆垛机状态地址长度
|
OperateResult<byte[]> read = siemensTcpNet.Read(s_startadd, (ushort)i_len);
|
if (read.IsSuccess)
|
{
|
for (int i = 0; i < 10; i++) //站点数
|
{
|
num = siemensTcpNet.ByteTransform.TransInt16(read.Content, i * 4);
|
Common.plc_i_Wrk_no[plcno - 1, i] = num; //ID
|
DisplayPLCDataInfo(1, i);
|
recvStr = recvStr + num.ToString();
|
|
num = siemensTcpNet.ByteTransform.TransInt16(read.Content, 2 + (i * 4));
|
Common.plc_s_next_stn[plcno - 1, i] = num.ToString(); //目标
|
DisplayPLCDataInfo(3, i);
|
recvStr = recvStr + num.ToString();
|
}
|
|
bool[] stsArray = siemensTcpNet.ByteTransform.TransBool(read.Content, 60, 10); //地址起始长度,站点数
|
for (int j = 0; j < 10; j++) //状态
|
{
|
int i = j;
|
if (stsArray[j * 8])
|
{
|
Common.plc_s_autoing[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_autoing[plcno - 1, i] = "N";
|
}
|
if (stsArray[j * 8 + 1])
|
{
|
Common.plc_s_loading[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_loading[plcno - 1, i] = "N";
|
//出现过无物情况清除plc_i_pakmk标记,通过判断plc_i_pakmk标记,防止重复下指令
|
Common.plc_i_pakmk[plcno - 1, i] = 0;
|
}
|
if (stsArray[j * 8 + 2])
|
{
|
Common.plc_s_canining[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_canining[plcno - 1, i] = "N";
|
}
|
if (stsArray[j * 8 + 3])
|
{
|
Common.plc_s_canouting[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_canouting[plcno - 1, i] = "N";
|
}
|
if (stsArray[j * 8 + 4])
|
{
|
Common.plc_s_inreq1[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_inreq1[plcno - 1, i] = "N";
|
}
|
if (stsArray[j * 8 + 5])
|
{
|
Common.plc_s_inreq2[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_inreq2[plcno - 1, i] = "N";
|
}
|
if (stsArray[j * 8 + 6])
|
{
|
Common.plc_s_loctype[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_loctype[plcno - 1, i] = "N";
|
}
|
|
DisplayPLCDataInfo(2, i);
|
recvStr = recvStr + num.ToString();
|
}
|
|
//读取条码
|
string i_barcode = "";
|
for (int i = 0; i < Common.ci_barcode_count; i++) //BarCODE
|
{
|
if (i == 0)
|
{
|
OperateResult<byte[]> barcode = siemensTcpNet.Read("DB100.90", (byte)8);
|
i_barcode = Encoding.UTF8.GetString(barcode.Content);
|
}
|
else
|
{
|
OperateResult<byte[]> barcode = siemensTcpNet.Read("DB100.98", (byte)8);
|
i_barcode = Encoding.UTF8.GetString(barcode.Content);
|
}
|
Common.gs_barcode_data[i] = i_barcode;
|
//i_barcode = ParseSource(barcode.Content);
|
//i_barcode = siemensTcpNet.ByteTransform.TransString(barcode.Content, i * 8, 8,UTF8 );
|
//i_barcode = Encoding.UTF8.GetString(barcode.Content);
|
//long i_barcode = siemensTcpNet.ByteTransform.TransInt32(read.Content, 86 + (i * 4));
|
//Common.gs_barcode_data[i] = i_barcode;
|
}
|
|
#region 台车
|
//for (int j = 48; j < 50; j++) //状态
|
//{
|
// int i = j - 48;
|
// num = siemensTcpNet.ByteTransform.TransInt16(read.Content, 48 + i * 2);
|
// if (j == 48) //台车位置
|
// {
|
// Common.gi_RGVPos[0] = num;
|
// }
|
// if (j == 49) //心跳
|
// {
|
// int heartBeat = num;
|
// if (heartBeat != 2)
|
// {
|
// heartBeat = 2;
|
// }
|
// else
|
// {
|
// heartBeat = 1;
|
// }
|
// OperateResult result = siemensTcpNet.Write("DB100.50", (short)heartBeat);
|
// }
|
//}
|
#endregion
|
DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCA数据--" + recvStr);//同步显示
|
}
|
else
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCA数据失败:" + read.ToMessageShowString());
|
}
|
}
|
catch (Exception em)
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#读取站点ID失败: " + em.Message);//同步显示
|
Common.WriteLogFile("WcsError", "PlcAThread/GetStaIdArea--读取站点ID失败:" + em.Message);
|
return;
|
}
|
}
|
|
/// <summary>
|
/// 读取PLC设备状态地址值
|
/// </summary>
|
private void GetStaStsArea()
|
{
|
string s_plchead = "", s_chksum = "", s_SendString = "";
|
string s_RecStr = "", s_startadd = "", s_len = "";
|
byte[] buf = new byte[12];
|
byte[] buf1 = new byte[500];
|
byte[] content = new byte[100];
|
int n = 0, num = 0, num1 = 0, k = 0, pi_return = 0, i_startadd = 0, i_len = 0, i_val = 0;
|
byte[] bte_value = new byte[4];
|
byte[] bte_addr = new byte[2];
|
int l = 0;
|
s_RecStr = "";
|
recvStr = "";
|
try
|
{
|
s_startadd = "DB100.60"; //读取堆垛机状态起始地址
|
i_len = 10; //读取堆垛机状态地址长度
|
OperateResult<byte[]> read = siemensTcpNet.Read(s_startadd, (ushort)i_len);
|
if (read.IsSuccess)
|
{
|
bool[] stsArray = siemensTcpNet.ByteTransform.TransBool(read.Content, 0, 10);
|
for (int i = 0; i < 11; i++) //状态
|
{
|
if (stsArray[i * 8])
|
{
|
Common.plc_s_autoing[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_autoing[plcno - 1, i] = "N";
|
}
|
if (stsArray[i * 8 + 1])
|
{
|
Common.plc_s_loading[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_loading[plcno - 1, i] = "N";
|
//出现过无物情况清除plc_i_pakmk标记,通过判断plc_i_pakmk标记,防止重复下指令
|
Common.plc_i_pakmk[plcno - 1, i] = 0;
|
}
|
if (stsArray[i * 8 + 2])
|
{
|
Common.plc_s_canining[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_canining[plcno - 1, i] = "N";
|
}
|
if (stsArray[i * 8 + 3])
|
{
|
Common.plc_s_canouting[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_canouting[plcno - 1, i] = "N";
|
}
|
if (stsArray[i * 8 + 4])
|
{
|
Common.plc_s_inreq1[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_inreq1[plcno - 1, i] = "N";
|
}
|
if (stsArray[i * 8 + 5])
|
{
|
Common.plc_s_inreq2[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_inreq2[plcno - 1, i] = "N";
|
}
|
if (stsArray[i * 8 + 6])
|
{
|
Common.plc_s_loctype[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_loctype[plcno - 1, i] = "N";
|
}
|
|
//buf1 = read.Content;
|
//num = 256 * (int)buf1[i * 2] + (int)buf1[i * 2 + 1];
|
//AnalyseStaStatus(i, num);
|
DisplayPLCDataInfo(2, i);
|
|
recvStr = recvStr + num.ToString();
|
}
|
DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCA状态--" + recvStr);//同步显示
|
}
|
else
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCA状态失败:" + read.ToMessageShowString());
|
}
|
#region 施耐德
|
//Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
//socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
//try
|
//{
|
// socket.Connect(plcip, plcport);
|
// i_startadd = 3001; //3001 //模拟器代表地址1001开始,程序中必须实际地址-1 ,#0+#$64表示读100个地址
|
// s_startadd = i_startadd.ToString("X4");
|
// bte_value[0] = (byte)Convert.ToInt32(s_startadd.Substring(0, 2), 16);
|
// bte_value[1] = (byte)Convert.ToInt32(s_startadd.Substring(2, 2), 16);
|
// //统一格式,读地址长度
|
// i_len = 13; //目前最长读125个
|
// s_len = i_len.ToString("X4");
|
// bte_value[2] = (byte)Convert.ToInt32(s_len.Substring(0, 2), 16);
|
// bte_value[3] = (byte)Convert.ToInt32(s_len.Substring(2, 2), 16);
|
// ////发送格式
|
// //#0+#1+#0+#0+#0+#$15+#$FF+#$10+chr(bte1[1])+chr(bte1[2])+#0+#$7+#$E+chr(bte[1])+chr(bte[2])+chr(bte[3])+chr(bte[4])+chr(bte[5])+chr(bte[6])+chr(bte[7])+chr(bte[8])+chr(bte[9])+chr(bte[10])+chr(bte[11])+chr(bte[12])+chr(bte[13])+chr(bte[14]);
|
// //s_SendString = GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(6) + " " + GetStringByASCII(3) + GetStringByASCII(bte_value[0]) + GetStringByASCII(bte_value[1]) + GetStringByASCII(bte_value[2]) + GetStringByASCII(bte_value[3]);
|
// buf[0] = (byte)(0);
|
// buf[1] = (byte)(0);
|
// buf[2] = (byte)(0);
|
// buf[3] = (byte)(0);
|
// buf[4] = (byte)(0);
|
// buf[5] = (byte)(6);
|
// buf[6] = (byte)(32);
|
// buf[7] = (byte)(3);
|
// buf[8] = (byte)(bte_value[0]);
|
// buf[9] = (byte)(bte_value[1]);
|
// buf[10] = (byte)(bte_value[2]);
|
// buf[11] = (byte)(bte_value[3]);
|
|
// socket.Send(buf, buf.Length, 0);
|
// int bytes = 0;
|
// bytes = socket.Receive(buf1, buf1.Length, 0);
|
// socket.Close();
|
// Thread.Sleep(Common.ci_PLCtimeInterval);
|
// for (int i = 0; i < 13; i++)
|
// {
|
// try
|
// {
|
// num = 256 * (int)buf1[i * 2 + 9] + (int)buf1[i * 2 + 10];
|
// j = i;
|
// }
|
// catch (Exception r)
|
// {
|
// num = 0;
|
// }
|
// recvStr = recvStr + num.ToString();
|
// AnalyseStaStatus(j, num);
|
// DisplayPLCDataInfo(2, j);
|
// // ChkCrnSts(i, num);
|
// }
|
// DisplayInfo("【" + DateTime.Now.ToString() + "】扫描状态1--" + recvStr);//同步显示
|
//}
|
//catch (Exception t)
|
//{
|
// DisplayInfo("【" + DateTime.Now.ToString() + "】扫描状态1失败:" + t.Message);//同步显示
|
// //Nondisstnsts();
|
// //for (int h = 0; h < 27; h++)
|
// //{
|
|
// // DisplayPLCDataInfo(2, h);
|
// //}
|
// socket.Close();
|
// return;
|
//}
|
#endregion
|
}
|
catch (Exception em)
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#读取站点状态失败: " + em.Message);//同步显示
|
Common.WriteLogFile("WcsError", "PlcAThread/GetStaStsArea--读取站点状态失败:" + em.Message);
|
return;
|
}
|
}
|
|
/// <summary>
|
/// 分析站点各状态
|
/// </summary>
|
/// <param name="i">站点序列</param>
|
/// <param name="num">状态值</param>
|
private void AnalyseStaStatus(int i, int num)
|
{
|
try
|
{
|
if (Common.fun_ChkAndByte(num, Common.ch_d1) == true)
|
{
|
Common.plc_s_autoing[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_autoing[plcno - 1, i] = "N";
|
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d2) == true)
|
{
|
Common.plc_s_loading[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_loading[plcno - 1, i] = "N";
|
//出现过无物情况清除plc_i_pakmk标记,通过判断plc_i_pakmk标记,防止重复下指令
|
Common.plc_i_pakmk[plcno - 1, i] = 0;
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d3) == true)
|
{
|
Common.plc_s_canining[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_canining[plcno - 1, i] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d4) == true)
|
{
|
Common.plc_s_canouting[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_canouting[plcno - 1, i] = "N";
|
Common.plc_i_pakmk[plcno - 1, i] = 0;
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d5) == true)
|
{
|
Common.plc_s_inreq1[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_inreq1[plcno - 1, i] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d6) == true)
|
{
|
Common.plc_s_inreq2[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_inreq2[plcno - 1, i] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d7) == true)
|
{
|
Common.plc_s_loctype[plcno - 1, i] = "Y";
|
}
|
else
|
{
|
Common.plc_s_loctype[plcno - 1, i] = "N";
|
}
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcThread/AnalyseStaStatus1--扫描站点状态字失败:" + em.Message);
|
}
|
}
|
|
#region 设备异常
|
/// <summary>
|
/// 得到设备异常
|
/// </summary>
|
private void GetStaErrArea1()
|
{
|
string s_plchead = "", s_chksum = "", s_SendString = "";
|
string s_RecStr = "", s_startadd = "", s_len = "";
|
byte[] buf = new byte[12];
|
byte[] buf1 = new byte[500];
|
byte[] content = new byte[100];
|
int n = 0, num = 0, num1 = 0, k = 0, pi_return = 0, i_startadd = 0, i_len = 0, i_val = 0;
|
byte[] bte_value = new byte[4];
|
byte[] bte_addr = new byte[2];
|
s_RecStr = "";
|
recvStr = "";
|
int seq = 0;
|
try
|
{
|
//s_startadd = "DB101.4"; //读取堆垛机状态起始地址
|
//i_len = 12; //读取堆垛机状态地址长度
|
//OperateResult<byte[]> read = siemensTcpNet.Read(s_startadd, (ushort)i_len);
|
//if (read.IsSuccess)
|
//{
|
// bool[] StaErr = siemensTcpNet.ByteTransform.TransBool(read.Content,0, 12); //地址起始长度,站点数
|
// for (int i = 0; i < 96; i++)
|
// {
|
|
// Common.plcerr[i] = StaErr[i] ? "Y" : "N";
|
// }
|
//}
|
|
s_startadd = "DB101.4"; //读取堆垛机状态起始地址
|
i_len = 4; //读取堆垛机状态地址长度
|
OperateResult<byte[]> read = siemensTcpNet.Read(s_startadd, (ushort)i_len);
|
if (read.IsSuccess)
|
{
|
bool[] StaErr = siemensTcpNet.ByteTransform.TransBool(read.Content, 0, 4); //地址起始长度,站点数
|
for (int i = 0; i < 27; i++)
|
{
|
Common.plcerr[i] = StaErr[i] ? "Y" : "N";
|
}
|
}
|
|
s_startadd = "DB101.12"; //读取堆垛机状态起始地址
|
i_len = 4; //读取堆垛机状态地址长度
|
OperateResult<byte[]> read1 = siemensTcpNet.Read(s_startadd, (ushort)i_len);
|
if (read1.IsSuccess)
|
{
|
bool[] StaErr = siemensTcpNet.ByteTransform.TransBool(read1.Content, 0, 4); //地址起始长度,站点数
|
for (int i = 0; i < 27; i++)
|
{
|
Common.plcerr[i + 27] = StaErr[i] ? "Y" : "N";
|
}
|
}
|
}
|
#region 施耐德
|
//try
|
//{
|
// Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
// socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
// try
|
// {
|
// socket.Connect(plcip, plcport);
|
// i_startadd = 800; //模拟器代表地址1001开始,程序中必须实际地址-1 ,#0+#$64表示读100个地址
|
// s_startadd = i_startadd.ToString("X4");
|
// bte_value[0] = (byte)Convert.ToInt32(s_startadd.Substring(0, 2), 16);
|
// bte_value[1] = (byte)Convert.ToInt32(s_startadd.Substring(2, 2), 16);
|
// //统一格式,读地址长度
|
// i_len = 4; //目前最长读125个
|
// s_len = i_len.ToString("X4");
|
// bte_value[2] = (byte)Convert.ToInt32(s_len.Substring(0, 2), 16);
|
// bte_value[3] = (byte)Convert.ToInt32(s_len.Substring(2, 2), 16);
|
// ////发送格式
|
// //#0+#1+#0+#0+#0+#$15+#$FF+#$10+chr(bte1[1])+chr(bte1[2])+#0+#$7+#$E+chr(bte[1])+chr(bte[2])+chr(bte[3])+chr(bte[4])+chr(bte[5])+chr(bte[6])+chr(bte[7])+chr(bte[8])+chr(bte[9])+chr(bte[10])+chr(bte[11])+chr(bte[12])+chr(bte[13])+chr(bte[14]);
|
// //s_SendString = GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(6) + " " + GetStringByASCII(3) + GetStringByASCII(bte_value[0]) + GetStringByASCII(bte_value[1]) + GetStringByASCII(bte_value[2]) + GetStringByASCII(bte_value[3]);
|
// buf[0] = (byte)(0);
|
// buf[1] = (byte)(0);
|
// buf[2] = (byte)(0);
|
// buf[3] = (byte)(0);
|
// buf[4] = (byte)(0);
|
// buf[5] = (byte)(6);
|
// buf[6] = (byte)(32);
|
// buf[7] = (byte)(3);
|
// buf[8] = (byte)(bte_value[0]);
|
// buf[9] = (byte)(bte_value[1]);
|
// buf[10] = (byte)(bte_value[2]);
|
// buf[11] = (byte)(bte_value[3]);
|
|
// socket.Send(buf, buf.Length, 0);
|
// int bytes = 0;
|
// bytes = socket.Receive(buf1, buf1.Length, 0);
|
// socket.Close();
|
// Thread.Sleep(Common.ci_PLCtimeInterval);
|
// for (int i = 0; i < 4; i++)
|
// {
|
// try
|
// {
|
// num = 256 * (int)buf1[i * 2 + 9] + (int)buf1[i * 2 + 10];
|
// if (i == 0)
|
// {
|
// Common.gi_RGVPos[0] = num;
|
// }
|
// else
|
// {
|
// AnalyseStaErr1(i - 1, num);
|
// }
|
// }
|
// catch (Exception r)
|
// {
|
// num = 0;
|
// }
|
// recvStr = recvStr + num.ToString();
|
// }
|
// DisplayInfo("【" + DateTime.Now.ToString() + "】扫描设备异常1--" + recvStr);//同步显示
|
|
// //禁用Socket
|
// ////socket.Shutdown(SocketShutdown.Both);
|
// ////socket.Disconnect(true);
|
// //socket.Close();
|
// }
|
// catch (Exception t)
|
// {
|
// DisplayInfo("【" + DateTime.Now.ToString() + "】扫描设备异常1失败:" + t.Message);//同步显示
|
// //Nondisstnsts();
|
// //socket.Shutdown(SocketShutdown.Both);
|
// //socket.Disconnect(true);
|
// socket.Close();
|
// return;
|
// }
|
//}
|
#endregion
|
catch (Exception em)
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#读取设备异常失败: " + em.Message);//同步显示
|
Common.WriteLogFile("WcsError", "PlcAThread/GetStaStsArea--读取设备异常失败:" + em.Message);
|
return;
|
}
|
}
|
|
/// <summary>
|
/// 分析站点异常
|
/// </summary>
|
/// <param name="i">站点序列</param>
|
/// <param name="num">异常值</param>
|
private void AnalyseStaErr1(int i, int num)
|
{
|
try
|
{
|
if (Common.fun_ChkAndByte(num, Common.ch_d1) == true)
|
{
|
Common.plcerr[i * 16 + 0] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 0] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d2) == true)
|
{
|
Common.plcerr[i * 16 + 1] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 1] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d3) == true)
|
{
|
Common.plcerr[i * 16 + 2] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 2] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d4) == true)
|
{
|
Common.plcerr[i * 16 + 3] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 3] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d5) == true)
|
{
|
Common.plcerr[i * 16 + 4] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 4] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d6) == true)
|
{
|
Common.plcerr[i * 16 + 5] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 5] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d7) == true)
|
{
|
Common.plcerr[i * 16 + 6] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 6] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d8) == true)
|
{
|
Common.plcerr[i * 16 + 7] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 7] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d9) == true)
|
{
|
Common.plcerr[i * 16 + 8] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 8] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d10) == true)
|
{
|
Common.plcerr[i * 16 + 9] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 9] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d11) == true)
|
{
|
Common.plcerr[i * 16 + 10] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 10] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d12) == true)
|
{
|
Common.plcerr[i * 16 + 11] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 11] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d13) == true)
|
{
|
Common.plcerr[i * 16 + 12] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 12] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d14) == true)
|
{
|
Common.plcerr[i * 16 + 13] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 13] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d15) == true)
|
{
|
Common.plcerr[i * 16 + 14] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 14] = "N";
|
}
|
if (Common.fun_ChkAndByte(num, Common.ch_d16) == true)
|
{
|
Common.plcerr[i * 16 + 15] = "Y";
|
}
|
else
|
{
|
Common.plcerr[i * 16 + 15] = "N";
|
}
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcThread/AnalyseStaErr1--分析站点字异常失败:" + em.Message);
|
}
|
}
|
#endregion
|
|
/// <summary>
|
/// 读取PLC目标站地址值
|
/// </summary>
|
private void GetStaDestArea()
|
{
|
string s_plchead = "", s_chksum = "", s_SendString = "";
|
string s_RecStr = "", s_startadd = "", s_len = "";
|
byte[] buf = new byte[12];
|
byte[] buf1 = new byte[500];
|
byte[] content = new byte[200];
|
int n = 0, num = 0, num1 = 0, k = 0, pi_return = 0, i_startadd = 0, i_len = 0, i_val = 0;
|
byte[] bte_value = new byte[4];
|
byte[] bte_addr = new byte[2];
|
s_RecStr = "";
|
recvStr = "";
|
int j = 0;
|
try
|
{
|
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
try
|
{
|
socket.Connect(plcip, plcport);
|
i_startadd = 2001; //2001 //模拟器代表地址1001开始,程序中必须实际地址-1 ,#0+#$64表示读100个地址
|
s_startadd = i_startadd.ToString("X4");
|
bte_value[0] = (byte)Convert.ToInt32(s_startadd.Substring(0, 2), 16);
|
bte_value[1] = (byte)Convert.ToInt32(s_startadd.Substring(2, 2), 16);
|
//统一格式,读地址长度
|
i_len = 13; //目前最长读125个
|
s_len = i_len.ToString("X4");
|
bte_value[2] = (byte)Convert.ToInt32(s_len.Substring(0, 2), 16);
|
bte_value[3] = (byte)Convert.ToInt32(s_len.Substring(2, 2), 16);
|
////发送格式
|
//#0+#1+#0+#0+#0+#$15+#$FF+#$10+chr(bte1[1])+chr(bte1[2])+#0+#$7+#$E+chr(bte[1])+chr(bte[2])+chr(bte[3])+chr(bte[4])+chr(bte[5])+chr(bte[6])+chr(bte[7])+chr(bte[8])+chr(bte[9])+chr(bte[10])+chr(bte[11])+chr(bte[12])+chr(bte[13])+chr(bte[14]);
|
//s_SendString = GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(0) + GetStringByASCII(6) + " " + GetStringByASCII(3) + GetStringByASCII(bte_value[0]) + GetStringByASCII(bte_value[1]) + GetStringByASCII(bte_value[2]) + GetStringByASCII(bte_value[3]);
|
buf[0] = (byte)(0);
|
buf[1] = (byte)(0);
|
buf[2] = (byte)(0);
|
buf[3] = (byte)(0);
|
buf[4] = (byte)(0);
|
buf[5] = (byte)(6);
|
buf[6] = (byte)(32);
|
buf[7] = (byte)(3);
|
buf[8] = (byte)(bte_value[0]);
|
buf[9] = (byte)(bte_value[1]);
|
buf[10] = (byte)(bte_value[2]);
|
buf[11] = (byte)(bte_value[3]);
|
|
socket.Send(buf, buf.Length, 0);
|
int bytes = 0;
|
bytes = socket.Receive(buf1, buf1.Length, 0);
|
socket.Close();
|
Thread.Sleep(Common.ci_PLCtimeInterval);
|
for (int i = 0; i < 10; i++)
|
{
|
try
|
{
|
num = 256 * (int)buf1[i * 2 + 9] + (int)buf1[i * 2 + 10];
|
Common.plc_s_next_stn[plcno - 1, i] = num.ToString();
|
j = i;
|
}
|
catch (Exception r)
|
{
|
num = 0;
|
}
|
recvStr = recvStr + num.ToString();
|
|
DisplayPLCDataInfo(3, j);
|
// ChkCrnSts(i, num);
|
}
|
DisplayInfo("【" + DateTime.Now.ToString() + "】扫描目标站1--" + recvStr);//同步显示
|
|
//禁用Socket
|
//socket.Shutdown(SocketShutdown.Both);
|
//socket.Disconnect(true);
|
//socket.Close();
|
}
|
catch (Exception t)
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】扫描目标站1失败:" + t.Message);//同步显示
|
//Nondisstnsts();
|
//socket.Shutdown(SocketShutdown.Both);
|
//socket.Disconnect(true);
|
socket.Close();
|
return;
|
}
|
}
|
catch (Exception em)
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#读取目标站失败: " + em.Message);//同步显示
|
Common.WriteLogFile("WcsError", "PlcAThread/GetStaDestArea--读取目标站失败:" + em.Message);
|
return;
|
}
|
}
|
|
/// <summary>
|
/// 根据命令判断写PLC类型
|
/// </summary>
|
/// <param name="cmd"></param>
|
private void WriteStaArea(string cmd)
|
{
|
string s_startadd = "", s_val = "";
|
int icount = 0;
|
bool ok = false;
|
int n = 0, num = 0, num1 = 0, k = 0, pi_return = 0, i_startadd = 0, i_len = 0, i_val = 0;
|
byte[] bte_value = new byte[14];
|
byte[] bte_addr = new byte[4];
|
int type = 0, stn = 0, dest = 0, id = 0, add = 0;
|
OperateResult result;
|
try
|
{
|
type = Int32.Parse(cmd.Substring(0, 2));
|
id = Int32.Parse(cmd.Substring(2, 4));
|
stn = Int32.Parse(cmd.Substring(6, 4));
|
dest = Int32.Parse(cmd.Substring(10, 4));
|
//p_TcpClient_plc.Active:=true;
|
////20100810修改统一格式
|
//类型
|
switch (type)
|
{
|
case 2:
|
//写ID
|
i_startadd = Common.g_ari_station_idaddr[Common.GetStnSeq(stn)];
|
s_startadd = "DB100." + i_startadd.ToString();
|
result = siemensTcpNet.Write(s_startadd, (short)id);
|
if (result.IsSuccess)
|
{
|
plc_cmd = "";
|
}
|
//Thread.Sleep(Common.ci_PLCtimeInterval);
|
break;
|
case 3:
|
//写目标
|
i_startadd = Common.g_ari_station_destaddr[Common.GetStnSeq(stn)];
|
s_startadd = "DB100." + i_startadd.ToString();
|
result = siemensTcpNet.Write(s_startadd, (short)dest);
|
if (result.IsSuccess)
|
{
|
plc_cmd = "";
|
}
|
break;
|
case 4:
|
//写ID
|
i_startadd = Common.g_ari_station_idaddr[Common.GetStnSeq(stn)];
|
s_startadd = "DB100." + i_startadd.ToString();
|
result = siemensTcpNet.Write(s_startadd, (short)id);
|
|
//写目标.ToString("X4");;
|
i_startadd = Common.g_ari_station_destaddr[Common.GetStnSeq(stn)];
|
s_startadd = "DB100." + i_startadd.ToString();
|
result = siemensTcpNet.Write(s_startadd, (short)dest);
|
if (result.IsSuccess)
|
{
|
plc_cmd = "";
|
}
|
//Thread.Sleep(200);
|
break;
|
default:
|
break;
|
}
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/WriteStaArea--写PLC错误:" + em.Message);
|
DisplayInfo("【" + DateTime.Now.ToString() + "】写PLC错误:" + em.Message);//同步显示
|
return;
|
}
|
}
|
|
/// <summary>
|
/// 写PLC地址值
|
/// </summary>
|
/// <param name="pos"></param>
|
/// <param name="id"></param>
|
private void WriteAddr(byte val1, byte val2, byte val3, byte val4)
|
{
|
string s_plchead = "", s_chksum = "", s_SendString = "";
|
string s_RecStr = "", s_startadd = "", s_len = "";
|
int icount = 0;
|
bool ok = false;
|
byte[] buf = new byte[15];
|
byte[] buf1 = new byte[500];
|
byte[] content = new byte[100];
|
int n = 0, num = 0, num1 = 0, k = 0, pi_return = 0, i_startadd = 0, i_len = 0, i_val = 0;
|
byte[] bte_value = new byte[14];
|
byte[] bte_addr = new byte[4];
|
s_RecStr = "";
|
recvStr = "";
|
int type = 0, stn = 0, dest = 0, id = 0, add = 0;
|
do
|
{
|
try
|
{
|
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
try
|
{
|
socket.Connect(plcip, plcport);
|
buf[0] = 0;
|
buf[1] = 1;
|
buf[2] = 0;
|
buf[3] = 0;
|
buf[4] = 0;
|
buf[5] = 9;
|
buf[6] = 32;
|
buf[7] = 16;
|
buf[8] = val1;
|
buf[9] = val2;
|
buf[10] = 0;
|
buf[11] = 1;
|
buf[12] = 2;
|
buf[13] = val3;
|
buf[14] = val4;
|
|
int bytes = 0;
|
bytes = socket.Send(buf, buf.Length, 0);
|
Thread.Sleep(Common.ci_PLCtimeInterval);
|
socket.Close();
|
|
if (bytes != buf.Length)
|
{
|
icount++;
|
if (icount >= 5)
|
{
|
ok = true;
|
}
|
}
|
else
|
{
|
ok = false;
|
}
|
|
//禁用Socket
|
//socket.Shutdown(SocketShutdown.Both);
|
//socket.Disconnect(true);
|
// socket.Close();
|
}
|
catch (Exception em)
|
{
|
DisplayInfo("【" + DateTime.Now.ToString() + "】写PLC错误:" + em.Message);//同步显示
|
Common.WriteLogFile("WcsError", "PlcAThread/WriteAddr--写PLCA地址失败:" + em.Message);
|
//socket.Shutdown(SocketShutdown.Both);
|
//socket.Disconnect(true);
|
socket.Close();
|
return;
|
}
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/WriteAddr--写PLCA地址失败:" + em.Message);
|
return;
|
}
|
|
} while (ok == true);
|
}
|
|
/// <summary>
|
/// 根据ascii码值得到对应字符
|
/// </summary>
|
/// <param name="asciiCode">ascii码</param>
|
/// <returns>返回对应字符</returns>
|
public string GetStringByASCII(int asciiCode)
|
{
|
try
|
{
|
if (asciiCode >= 0 && asciiCode <= 127)
|
{
|
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
|
byte[] byteArray = new byte[] { (byte)asciiCode };
|
string strCharacter = asciiEncoding.GetString(byteArray);
|
return (strCharacter);
|
}
|
else if (asciiCode >= 128 && asciiCode <= 255)
|
{
|
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
|
byte[] byteArray = new byte[] { (byte)(asciiCode + 65536) };
|
string strCharacter = Encoding.Default.GetString(byteArray);
|
return (strCharacter);
|
}
|
else
|
{
|
throw new Exception("无效的ASCII码!");
|
|
}
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/GetStringByASCII--ASCII转字符串错误:" + em.Message);
|
return "";
|
}
|
}
|
|
#region 画面同步显示
|
/// <summary>
|
/// 同步显示plc通讯信息
|
/// </summary>
|
/// <param name="text">扫描信息</param>
|
/// <param name="memo">备注信息</param>
|
private void DisplayInfo(string text)
|
{
|
if (tb.InvokeRequired)
|
{
|
try
|
{
|
displayresponse d = new displayresponse(DisplayInfo);
|
tb.Invoke(d, new object[] { text });
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/DisplayInfo--同步显示plc通讯信息错误:" + em.Message);
|
}
|
}
|
else
|
{
|
if (tb.Items.Count > 50)
|
{
|
tb.Items.Clear();
|
}
|
tb.Items.Add(text);
|
tb.TopIndex = tb.Items.Count - (int)(tb.Height / tb.ItemHeight);
|
}
|
|
}
|
|
/// <summary>
|
/// 同步显示站点的状态
|
/// </summary>
|
/// <param name="type">类别</param>
|
/// <param name="k">序号</param>
|
private void DisplayPLCDataInfo(int type, int k)
|
{
|
if (plclist.InvokeRequired)
|
{
|
try
|
{
|
DisplayData d = new DisplayData(DisplayPLCDataInfo);
|
plclist.Invoke(d, new object[] { type, k });
|
plclist.Focus();
|
//plclist.Update();
|
//plclist.Refresh();
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/DisplayPLCDataInfo--同步显示站点状态错误:" + em.Message);
|
}
|
}
|
else
|
{
|
if (type == 1)
|
{
|
this.plclist.Items[k].SubItems[1].Text = Common.plc_i_Wrk_no[plcno - 1, k].ToString();
|
}
|
else if (type == 2)
|
{
|
//if (k == 8 || k == 11) return;
|
this.plclist.Items[k].SubItems[2].Text = Common.plc_s_autoing[plcno - 1, k].ToString();
|
this.plclist.Items[k].SubItems[3].Text = Common.plc_s_loading[plcno - 1, k].ToString();
|
this.plclist.Items[k].SubItems[4].Text = Common.plc_s_canining[plcno - 1, k].ToString();
|
this.plclist.Items[k].SubItems[5].Text = Common.plc_s_canouting[plcno - 1, k].ToString();
|
this.plclist.Items[k].SubItems[6].Text = Common.plc_s_inreq1[plcno - 1, k].ToString();
|
this.plclist.Items[k].SubItems[7].Text = Common.plc_s_inreq2[plcno - 1, k].ToString();
|
|
if (k == 4)
|
{
|
this.plclist.Items[k].SubItems[8].Text = Common.gs_barcode_data[0];
|
}
|
else if (k == 9)
|
{
|
this.plclist.Items[k].SubItems[8].Text = Common.gs_barcode_data[1];
|
}
|
else
|
{
|
this.plclist.Items[k].SubItems[8].Text = "";
|
}
|
|
|
}
|
else if (type == 3)
|
{
|
this.plclist.Items[k].SubItems[9].Text = Common.plc_s_next_stn[plcno - 1, k].ToString();
|
}
|
|
}
|
}
|
|
/// <summary>
|
/// 更新PLC命令信息到界面显示
|
/// </summary>
|
/// <param name="count">队列数</param>
|
private void DisplayQuerecmd(string info)
|
{
|
try
|
{
|
if (sendtoplc.InvokeRequired)
|
{
|
DisplaySendTxtInfo d = new DisplaySendTxtInfo(DisplayQuerecmd);
|
sendtoplc.Invoke(d, new object[] { info });
|
}
|
else
|
{
|
sendtoplc.Text = info.ToString();
|
}
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/DisplayQuerecmd--更新PLC命令信息到界面错误:" + em.Message);
|
}
|
}
|
#endregion
|
|
/// <summary>
|
/// 通讯失败后初始化站点状态
|
/// </summary>
|
private void InitStn()
|
{
|
try
|
{
|
for (int i = 0; i < 30; i++)
|
{
|
Common.plc_s_autoing[0, i] = "N";
|
Common.plc_s_loading[0, i] = "N";
|
Common.plc_s_canining[0, i] = "N";
|
Common.plc_s_canouting[0, i] = "N";
|
Common.plc_s_inreq1[0, i] = "N";
|
Common.plc_s_inreq2[0, i] = "N";
|
Common.plc_s_loctype[0, i] = "0";
|
Common.plc_s_error[0, i] = "N";
|
Common.plc_i_buff_count[0, i] = 0;
|
Common.plc_i_Wrk_no[0, i] = 0;
|
Common.plc_s_stn_status[0, i] = "N";
|
Common.plc_s_next_stn[0, i] = "0";
|
//Common.plc_i_pakmk[0, i] = 0;
|
}
|
}
|
catch (Exception)
|
{ }
|
}
|
|
#region 注释
|
///// <summary>
|
///// 同步显示站点buffer
|
///// </summary>
|
///// <param name="type">类别</param>
|
///// <param name="k">序号</param>
|
//private void DisplayBuffer(int k)
|
//{
|
// if (plcbuflist.InvokeRequired)
|
// {
|
// try
|
// {
|
// DisplayDataBuffer d = new DisplayDataBuffer(DisplayBuffer);
|
// plcbuflist.Invoke(d, new object[] { k});
|
// }
|
// catch (Exception r)
|
// {
|
|
// }
|
// }
|
// else
|
// {
|
// this.plcbuflist.Items[0].SubItems[k].Text = Common.gi_stabuffer[k].ToString();
|
// }
|
|
//}
|
|
/// <summary>
|
/// 同步显示站点的状态
|
/// </summary>
|
/// <param name="type">类别</param>
|
/// <param name="k">序号</param>
|
|
///// <summary>
|
///// 同步显示队列数
|
///// </summary>
|
///// <param name="count">队列数</param>
|
//private void DisplayQuereCount(int count)
|
//{
|
// try
|
// {
|
// if (querecount.InvokeRequired)
|
// {
|
// DisplayQuereData d = new DisplayQuereData(DisplayQuereCount);
|
// querecount.Invoke(d, new object[] { count });
|
// }
|
// else
|
// {
|
// querecount.Text = count.ToString();
|
// }
|
// }
|
// catch (Exception t)
|
// {
|
|
// }
|
//}
|
#endregion
|
|
/// <summary>
|
/// PLC主线程
|
/// </summary>
|
private void ThreadTask()
|
{
|
//int result = client.ConnectTo(plcip, rack, slot);
|
//if (result != 0)
|
//{
|
// DisplayInfo("【" + DateTime.Now.ToString() + "】连接PLC " + plcip + "错误:" + client.ErrorText(result));
|
// Common.WriteLogFile("WcsError", "PlcAThread/GetStaIdArea--连接PLC " + plcip + "错误:" + client.ErrorText(result));
|
// InitStn();
|
// return;
|
//}
|
i_plc_step = 1;
|
connect = siemensTcpNet.ConnectServer();
|
while (true)
|
{
|
//Common.plcerr[0] = "Y";
|
try
|
{
|
if (!connect.IsSuccess)
|
{
|
if (!ReConnect())
|
{
|
goto re;
|
}
|
}
|
//DisplayQuereCount(Common.GetPlcQuereCount(plcno - 1));
|
if (Common.GetPlcQuereCount(plcno - 1) > 0 && plc_cmd == "")
|
//if (Common.PlcAQueue.Count > 0 && plc_cmd == "")
|
{
|
plc_cmd = Common.GetPlcQuereCmd(plcno - 1);
|
//plc_cmd = Common.PlcAQueue.Dequeue();
|
}
|
DisplayQuerecmd(plc_cmd);
|
CommandStr_nkindstep();
|
switch (i_plc_step)
|
{
|
case 1:
|
GetStaIdArea(); //读ID
|
//GetStaStsArea(); //读状态
|
//GetStaDestArea(); //读目标站
|
GetStaErrArea1(); //读设备异常
|
//Thread.Sleep(Common.ci_PLCtimeInterval);
|
break;
|
case 2://写id
|
WriteStaArea(plc_cmd);
|
break;
|
case 3://写目标
|
WriteStaArea(plc_cmd);
|
break;
|
case 4://写iD和目标
|
WriteStaArea(plc_cmd);
|
break;
|
//case 5://清buffer
|
// WriteStaArea(plc_cmd);
|
// break;
|
//case 6://插单
|
// WriteStaArea(plc_cmd);
|
// //WriteStaBuffer();
|
// break;
|
}
|
i_plc_step = 1;
|
re:
|
Thread.Sleep(Common.ci_PLCtimeInterval);
|
}
|
catch (Exception em)
|
{
|
Common.WriteLogFile("WcsError", "PlcAThread/ThreadTask--PLC主线程循环错误:" + em.Message);
|
}
|
}
|
|
}
|
|
}
|
|
}
|