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 Sharp7;
namespace WCS
{
///
/// PLCB线程
///
class PlcBThread
{
//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);
#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
private int rack = 0, slot = 1;
//S7Client client = new S7Client();
public PlcBThread(int plc_no, string plc_ip, int port, main f)
{
try
{
plcno = plc_no;
plcip = plc_ip;
plcport = port;
form1 = f;
trd = new Thread(new ThreadStart(this.ThreadTask));
trd.IsBackground = true;
trd.Start();
}
catch (Exception em)
{
Common.WriteLogFile("WcsError", "PlcThread/PlcThread--PLC线程启动出错:" + em.Message);
}
}
/////
///// 根据命令字符串判断作业类型
/////
//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", "PlcThread/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中字-----------------------------
/////
///// 读PLC id
/////
//private void GetStaIdArea()
//{
// recvStr = "";
// var client = new S7Client();
// int result = 0;
// try
// {
// result = client.ConnectTo(plcip, rack, slot);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】连接PLC " + plcip + "错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaIdArea--连接PLC " + plcip + "错误:" + client.ErrorText(result));
// InitStn();
// return;
// }
// byte[] dbBuffer = new byte[66];
// result = client.DBRead(540, 0, 66, dbBuffer);
// //Thread.Sleep(Common.ci_PLCtimeInterval);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】读取PLCB站点ID错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaIdArea--读取站点状态错误:" + client.ErrorText(result));
// client.Disconnect();
// return;
// }
// for (int i = 0; i < 33; i++)
// {
// Common.plc_i_Wrk_no[plcno - 1, i + 30] = S7.GetIntAt(dbBuffer, i * 2);
// recvStr = recvStr + Common.plc_i_Wrk_no[plcno - 1, i + 30];
// DisplayPLCDataInfo(1, i + 30);
// }
// DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCB ID--" + recvStr);//同步显示
// }
// catch (Exception em)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#读取站点状态失败: " + em.Message);//同步显示
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaIdArea--读取站点状态失败:" + em.Message);
// return;
// }
// finally
// {
// client.Disconnect();
// }
//}
/////
///// 读取PLC设备状态地址值
/////
//private void GetStaStsArea()
//{
// recvStr = "";
// var client = new S7Client();
// int result = 0;
// try
// {
// result = client.ConnectTo(plcip, rack, slot);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】连接PLC " + plcip + "错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaStsArea--连接PLC " + plcip + "错误:" + client.ErrorText(result));
// InitStn();
// return;
// }
// byte[] dbBuffer = new byte[66];
// result = client.DBRead(540, 200, dbBuffer.Length, dbBuffer);
// //Thread.Sleep(Common.ci_PLCtimeInterval);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】读取PLCB站点状态错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaStsArea--读取站点状态错误:" + client.ErrorText(result));
// client.Disconnect();
// return;
// }
// for (int i = 0; i < 33; i++)
// {
// Common.plc_s_autoing[plcno - 1, i + 30] = (S7.GetBitAt(dbBuffer, i * 2, 0) == true) ? "Y" : "N";
// Common.plc_s_loading[plcno - 1, i + 30] = (S7.GetBitAt(dbBuffer, i * 2, 1) == true) ? "Y" : "N";
// Common.plc_s_inreq1[plcno - 1, i + 30] = (S7.GetBitAt(dbBuffer, i * 2, 2) == true) ? "Y" : "N";
// Common.plc_s_inreq2[plcno - 1, i + 30] = (S7.GetBitAt(dbBuffer, i * 2, 3) == true) ? "Y" : "N";
// if (S7.GetBitAt(dbBuffer, i * 2, 4) == true) //高库位
// {
// Common.plc_s_loctype[plcno - 1, i + 30] = "Y";
// }
// else if (S7.GetBitAt(dbBuffer, i * 2, 5) == true) //低库位
// {
// Common.plc_s_loctype[plcno - 1, i + 30] = "N";
// }
// else
// {
// Common.plc_s_loctype[plcno - 1, i + 30] = "0";
// }
// if (Common.plc_s_loading[plcno - 1, i + 30] == "N" && Common.plc_i_Wrk_no[plcno - 1, i + 30] == 0)
// {
// Common.plc_i_pakmk[plcno - 1, i + 30] = 0;
// }
// recvStr = recvStr + Common.plc_s_autoing[plcno - 1, i + 30] + Common.plc_s_loading[plcno - 1, i + 30]
// + Common.plc_s_inreq1[plcno - 1, i + 30];// +Common.plc_s_inreq2[plcno - 1, i + 30];// +Common.plc_s_loctype[plcno - 1, i + 30];
// DisplayPLCDataInfo(2, i + 30);
// }
// DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCB 状态--" + recvStr);
// }
// catch (Exception em)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#读取站点状态失败: " + em.Message);//同步显示
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaStsArea--读取站点状态失败:" + em.Message);
// return;
// }
// finally
// {
// client.Disconnect();
// }
//}
/////
///// 分析站点各状态
/////
///// 站点序列
///// 状态值
//private void AnalyseStaStatus1(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";
// }
// 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 站点异常
/////
///// 得到设备异常
/////
//private void GetStaErrArea1()
//{
// recvStr = "";
// var client = new S7Client();
// int result = 0;
// try
// {
// result = client.ConnectTo(plcip, rack, slot);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】连接PLC " + plcip + "错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaStsArea--连接PLC " + plcip + "错误:" + client.ErrorText(result));
// InitStn();
// return;
// }
// byte[] dbBuffer = new byte[68];
// result = client.DBRead(53, 0, dbBuffer.Length, dbBuffer);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】读取PLCA设备异常失败:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcAThread/GetStaErrArea1--读取设备异常失败:" + client.ErrorText(result));
// return;
// }
// for (int i = 0; i < 35; i++)
// {
// if (i == 0)
// {
// Common.plcerr[i + 128] = (S7.GetBitAt(dbBuffer, i, 0) == true) ? "Y" : "N";
// Common.plcerr[i + 129] = (S7.GetBitAt(dbBuffer, i, 1) == true) ? "Y" : "N";
// Common.plcerr[i + 130] = (S7.GetBitAt(dbBuffer, i, 2) == true) ? "Y" : "N";
// Common.plcerr[i + 131] = (S7.GetBitAt(dbBuffer, i, 3) == true) ? "Y" : "N";
// Common.plcerr[i + 132] = (S7.GetBitAt(dbBuffer, i, 4) == true) ? "Y" : "N";
// Common.plcerr[i + 133] = (S7.GetBitAt(dbBuffer, i, 7) == true) ? "Y" : "N";
// }
// if (i == 1)
// {
// Common.plcerr[134] = (S7.GetBitAt(dbBuffer, i, 0) == true) ? "Y" : "N";
// }
// if (i > 1)
// {
// Common.plcerr[i * 4 + 127] = (S7.GetBitAt(dbBuffer, i * 2 - 2, 0) == true) ? "Y" : "N";
// Common.plcerr[i * 4 + 128] = (S7.GetBitAt(dbBuffer, i * 2 - 2, 1) == true) ? "Y" : "N";
// Common.plcerr[i * 4 + 129] = (S7.GetBitAt(dbBuffer, i * 2 - 2, 2) == true) ? "Y" : "N";
// Common.plcerr[i * 4 + 130] = (S7.GetBitAt(dbBuffer, i * 2 - 2, 3) == true) ? "Y" : "N";
// }
// }
// //DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCA 状态--" + recvStr);
// }
// catch (Exception em)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#读取设备异常失败: " + em.Message);//同步显示
// Common.WriteLogFile("WcsError", "PlcAThread/GetStaStsArea--读取设备异常失败:" + em.Message);
// return;
// }
// finally
// {
// client.Disconnect();
// }
//}
/////
///// 分析站点异常
/////
///// 站点序列
///// 异常值
//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
/////
///// 读取PLC目标站地址值
/////
//private void GetStaDestArea()
//{
// recvStr = "";
// var client = new S7Client();
// int result = 0;
// try
// {
// result = client.ConnectTo(plcip, rack, slot);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】连接PLC " + plcip + "错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaDestArea--连接PLC " + plcip + "错误:" + client.ErrorText(result));
// InitStn();
// return;
// }
// byte[] dbBuffer = new byte[66];
// result = client.DBRead(540, 100, 66, dbBuffer);
// //Thread.Sleep(Common.ci_PLCtimeInterval);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】读取PLCB目标站错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaDestArea--读取目标站错误:" + client.ErrorText(result));
// client.Disconnect();
// return;
// }
// for (int i = 0; i < 33; i++)
// {
// Common.plc_s_next_stn[plcno - 1, i + 30] = S7.GetIntAt(dbBuffer, i * 2).ToString();
// recvStr = recvStr + Common.plc_s_next_stn[plcno - 1, i + 30];
// DisplayPLCDataInfo(3, i + 30);
// }
// DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCB 目标站--" + recvStr);//同步显示
// ///////////////////PLC读取托盘条码/////////////////////////////
// recvStr = "";
// byte[] dbBuffer1 = new byte[4];
// result = client.DBRead(540, 300, dbBuffer1.Length, dbBuffer1);
// //Thread.Sleep(Common.ci_PLCtimeInterval);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】读取PLCB条码数据错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/GetBarcodeData--读取条码数据错误:" + client.ErrorText(result));
// client.Disconnect();
// return;
// }
// for (int i = 0; i < 1; i++)
// {
// //long kk = ;
// Common.gs_barcode_data[2] = S7.GetDIntAt(dbBuffer1, i * 4).ToString();
// //Common.plc_s_next_stn[plcno - 1, i] = S7.GetIntAt(dbBuffer, i * 2).ToString();
// recvStr = recvStr + (i + 1) + "站:" + Common.gs_barcode_data[2] + "; ";
// DisplayPLCDataInfo(3, i + 30);
// }
// DisplayInfo("【" + DateTime.Now.ToString() + "】扫描PLCB 条码数据--" + recvStr);//同步显示
// //////////////////////////////////////////////////////////////
// }
// catch (Exception em)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#读取目标站失败: " + em.Message);//同步显示
// Common.WriteLogFile("WcsError", "PlcBThread/GetStaDestArea--读取目标站失败:" + em.Message);
// return;
// }
// finally
// {
// client.Disconnect();
// }
//}
/////
///// 根据命令判断写PLC类型
/////
/////
//private void WriteStaArea(string cmd)
//{
// int type = 0, stn = 0, dest = 0, id = 0, i_startadd = 0;
// bool bl = false;
// 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)];
// //bl = WriteAddr(i_startadd, id, dest, 2);
// bl = WriteAddr(Common.GetStnSeq(stn), id, dest, 2);
// //Thread.Sleep(Common.ci_PLCtimeInterval);
// if (bl)
// {
// plc_cmd = "";
// }
// break;
// case 3:
// //写目标
// i_startadd = Common.g_ari_station_destaddr[Common.GetStnSeq(stn)];
// //bl = WriteAddr(i_startadd, id, dest, 3);
// bl = WriteAddr(Common.GetStnSeq(stn), id, dest, 3);
// //Thread.Sleep(Common.ci_PLCtimeInterval);
// if (bl)
// {
// plc_cmd = "";
// }
// break;
// case 4:
// //写ID
// i_startadd = Common.g_ari_station_idaddr[Common.GetStnSeq(stn)];
// //bl = WriteAddr(i_startadd, id, dest, 4);
// bl = WriteAddr(Common.GetStnSeq(stn), id, dest, 4);
// //Thread.Sleep(Common.ci_PLCtimeInterval);
// if (bl)
// {
// ////写目标
// //i_startadd = Common.g_ari_station_destaddr[Common.GetStnSeq(stn)];
// //bool bl1 = WriteAddr(i_startadd, dest);
// ////Thread.Sleep(200);
// //if (bl1)
// //{
// plc_cmd = "";
// //}
// }
// break;
// case 5:
// //清除空栈板出库需求信号
// var client = new S7Client();
// int result = 0;
// try
// {
// int pos = 0;
// if (id == 1) //需求出信号
// {
// i_startadd = 216;
// pos = 3;
// }
// else //需求入信号
// {
// i_startadd = 208;
// pos = 2;
// }
// result = client.ConnectTo(plcip, rack, slot);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】连接PLC " + plcip + "错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/WriteAddr--连接PLC " + plcip + "错误:" + client.ErrorText(result));
// return;
// }
// byte[] dbBuffer = new byte[2];
// S7.SetBitAt(ref dbBuffer, 0, pos, false);
// //S7.SetIntAt(dbBuffer, 0, 0);
// result = client.DBWrite(540, i_startadd, dbBuffer.Length, dbBuffer);
// if (result == 0)
// {
// plc_cmd = "";
// }
// else
// {
// client.Disconnect();
// }
// }
// catch (Exception)
// { }
// finally
// {
// client.Disconnect();
// }
// //Thread.Sleep(200);
// break;
// case 6:
// //清空信号
// //Thread.Sleep(200);
// plc_cmd = "";
// break;
// default:
// break;
// }
// }
// catch (Exception em)
// {
// Common.WriteLogFile("WcsError", "PlcBThread/WriteStaArea--写PLC错误:" + em.Message);
// DisplayInfo("【" + DateTime.Now.ToString() + "】写PLC错误:" + em.Message);//同步显示
// return;
// }
//}
/////
///// 写PLC地址值
/////
/////
/////
//private bool WriteAddr(int pos, int id, int dest, int type)
//{
// bool bl = false;
// var client = new S7Client();
// bool ok = false;
// int icount = 0;
// try
// {
// int result = -1;
// result = client.ConnectTo(plcip, rack, slot);
// if (result != 0)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】连接PLC " + plcip + "错误:" + client.ErrorText(result));
// Common.WriteLogFile("WcsError", "PlcBThread/WriteAddr--连接PLC " + plcip + "错误:" + client.ErrorText(result));
// return bl;
// }
// byte[] dbBuffer = new byte[2];
// byte[] dbBuffer1 = new byte[2];
// if (type == 2 || type == 4)
// {
// S7.SetIntAt(dbBuffer, 0, (short)id);
// }
// if (type == 3 || type == 4)
// {
// S7.SetIntAt(dbBuffer1, 0, (short)dest);
// }
// var s7MultiVar = new S7MultiVar(client);
// //s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 540, Common.g_ari_station_idaddr[pos], dbBuffer.Length, ref dbBuffer);
// //s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 540, Common.g_ari_station_destaddr[pos], dbBuffer1.Length, ref dbBuffer1);
// do //如果失败,尝试下发3次
// {
// if (type == 2 || type == 4)
// {
// s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 540, Common.g_ari_station_idaddr[pos], dbBuffer.Length, ref dbBuffer);
// //result = client.DBWrite(540, Common.g_ari_station_idaddr[pos], dbBuffer.Length, dbBuffer);
// }
// if (type == 3 || type == 4)
// {
// s7MultiVar.Add(S7Consts.S7AreaDB, S7Consts.S7WLByte, 540, Common.g_ari_station_destaddr[pos], dbBuffer1.Length, ref dbBuffer1);
// //result = client.DBWrite(540, Common.g_ari_station_destaddr[pos], dbBuffer1.Length, dbBuffer1);
// }
// result = -1;
// if (Common.writeFlag)
// {
// Common.writeFlag = false;
// result = s7MultiVar.Write();
// Thread.Sleep(100);
// //result = s7MultiVar.Write();
// Common.WriteLogFile("PLCBCmd", "PlcBThread/PLC命令:" + plc_cmd + ";结果:" + result);
// Common.writeFlag = true;
// }
// if (result == 0)
// {
// bl = true;
// ok = true;
// }
// else
// {
// icount++;
// if (icount >= 5)
// {
// ok = true;
// }
// else
// {
// Thread.Sleep(Common.ci_PLCtimeInterval);
// }
// }
// } while (ok == false);
// }
// catch (Exception em)
// {
// DisplayInfo("【" + DateTime.Now.ToString() + "】" + plcno + "#写PLCB地址失败: " + em.Message);//同步显示
// Common.WriteLogFile("WcsError", "PlcBThread/WriteAddr--写PLCB地址失败:" + em.Message);
// return bl;
// }
// finally
// {
// client.Disconnect();
// }
// return bl;
//}
/////
///// 根据ascii码值得到对应字符
/////
///// ascii码
///// 返回对应字符
//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", "PlcBThread/GetStringByASCII--ASCII转字符串错误:" + em.Message);
// return "";
// }
//}
//#region 画面同步显示
/////
///// 同步显示plc通讯信息
/////
///// 扫描信息
///// 备注信息
//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", "PlcBThread/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);
// }
//}
/////
///// 同步显示站点的状态
/////
///// 类别
///// 序号
//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", "PlcBThread/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();
// this.plclist.Items[k].SubItems[8].Text = Common.plc_s_loctype[plcno - 1, k].ToString();
// //this.plclist.Items[k].SubItems[9].Text = Class1.plc_s_loctype[plcno - 1, k].ToString();
// }
// else if (type == 3)
// {
// this.plclist.Items[k].SubItems[9].Text = Common.plc_s_next_stn[plcno - 1, k].ToString();
// }
// }
//}
/////
///// 更新PLC命令信息到界面显示
/////
///// 队列数
//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", "PlcBThread/DisplayQuerecmd--更新PLC命令信息到界面错误:" + em.Message);
// }
//}
//#endregion
/////
///// 通讯失败后初始化站点状态
/////
//private void InitStn()
//{
// try
// {
// for (int i = 0; i < 33; i++)
// {
// Common.plc_s_autoing[1, i + 30] = "N";
// Common.plc_s_loading[1, i + 30] = "N";
// Common.plc_s_canining[1, i + 30] = "N";
// Common.plc_s_canouting[1, i + 30] = "N";
// Common.plc_s_inreq1[1, i + 30] = "N";
// Common.plc_s_inreq2[1, i + 30] = "N";
// Common.plc_s_loctype[1, i + 30] = "0";
// Common.plc_s_error[1, i + 30] = "N";
// Common.plc_i_buff_count[1, i + 30] = 0;
// Common.plc_i_Wrk_no[1, i + 30] = 0;
// Common.plc_s_stn_status[1, i + 30] = "N";
// Common.plc_s_next_stn[1, i + 30] = "0";
// //Common.plc_i_pakmk[1, i + 30] = 0;
// }
// }
// catch (Exception)
// { }
//}
//#region 注释
///////
/////// 同步显示站点buffer
///////
/////// 类别
/////// 序号
////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();
//// }
////}
/////
///// 同步显示站点的状态
/////
///// 类别
///// 序号
///////
/////// 同步显示队列数
///////
/////// 队列数
////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
///
/// PLC主线程
///
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", "PlcBThread/GetStaIdArea--连接PLC " + plcip + "错误:" + client.ErrorText(result));
//// InitStn();
//// //return;
////}
//while (true)
//{
// //if (client.Connected)
// //{
// try
// {
// //DisplayQuereCount(Common.GetPlcQuereCount(plcno - 1));
// //if (Common.PlcBQueue.Count > 0 && plc_cmd == "")
// //{
// // plc_cmd = Common.PlcBQueue.Dequeue();
// //}
// if (Common.GetPlcQuereCount(plcno - 1) > 0 && plc_cmd == "")
// {
// plc_cmd = Common.GetPlcQuereCmd(plcno - 1);
// }
// DisplayQuerecmd(plc_cmd);
// CommandStr_nkindstep();
// switch (i_plc_step)
// {
// case 1:
// GetStaIdArea(); //读ID
// GetStaStsArea(); //读状态
// GetStaDestArea(); //读目标站
// GetStaErrArea1(); //读设备异常
// 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;
// Thread.Sleep(Common.ci_PLCtimeInterval);
// }
// catch (Exception em)
// {
// Common.WriteLogFile("WcsError", "PlcBThread/ThreadTask--PLC主线程循环错误:" + em.Message);
// }
// //}
// //else
// //{
// // client.Disconnect();
// // result = client.ConnectTo(plcip, rack, slot);
// // if (result != 0)
// // {
// // DisplayInfo("【" + DateTime.Now.ToString() + "】连接PLC " + plcip + "错误:" + client.ErrorText(result));
// // Common.WriteLogFile("WcsError", "PlcBThread/GetStaIdArea--连接PLC " + plcip + "错误:" + client.ErrorText(result));
// // InitStn();
// // }
// //}
//}
}
}
}