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 System.Runtime.InteropServices; using com.force.json; using DevComponents.DotNetBar.Controls; using System.Drawing; using System.Data; using HslCommunication.Profinet.Siemens; using HslCommunication; using HslCommunication.Core.Net; namespace WCS { /// /// 堆垛机线程 /// class CraneThread { //[DllImport("kernel32")] //private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); //[DllImport("kernel32")] //private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); main form1; //NetworkStream myNetworkStream; //Thread thr = null; delegate void displayresponse(string text); //int wrk_no = 0, sstnno = 0, dstnno = 0,type=0; //string type_cmd = "", slocno = "", dlocno = ""; string recvStr = ""; string crnip = ""; int crnport = 0; int crnno = 0; //string headstr = ""; //string testip = ""; Thread trd = null; int i_crn_step = 0; //int i_crn_iotype = 0; public static string crn_cmd = ""; delegate void SetTextCallback(string text); delegate void DisplayData(int type, int i); delegate void DisplaycrnstsData(int crn, int bay, int lev, double dist, double height, int err); delegate void SetTextCallback1(); private SiemensS7Net siemensTcpNet = null; private OperateResult connect = null; #region 画面上同步显示控件 //public DevComponents.DotNetBar.LabelX cmd; //public ListView crncmdlist; //public ListView crnslist; public ListView crnstslist; public ListBox tb; public DataGridViewX dgv_crnsts; public TextBoxX tb_sendtocrn; #endregion public CraneThread(int crn_no, string crnip, int crnport, main f) { try { this.crnip = crnip; this.crnno = crn_no; this.crnport = crnport; form1 = f; siemensTcpNet = new SiemensS7Net(SiemensPLCS.S1200); siemensTcpNet.IpAddress = this.crnip; siemensTcpNet.Port = this.crnport; 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", "CraneThread/CraneThread--堆垛机线程启动出错:" + em.Message); } } public void Destroy() { siemensTcpNet.ConnectClose(); trd.Abort(); } /// /// 断开连接后,重新连接 /// /// private bool ReConnect() { bool result = false; try { siemensTcpNet.ConnectClose(); connect = siemensTcpNet.ConnectServer(); if (connect.IsSuccess) { result = true; DisplayInfo("【" + DateTime.Now.ToString() + "】" + crnno + "号堆垛机重新连接PLC成功 "); } else { DisplayInfo("【" + DateTime.Now.ToString() + "】" + crnno + "号堆垛机重新连接PLC失败---" + connect.ToMessageShowString()); } } catch (Exception em) { DisplayInfo("【" + DateTime.Now.ToString() + "】" + crnno + "号堆垛机重新连接PLC失败---" + em.Message); } return result; } /// /// 根据堆垛机命令判断作业类型 /// /// 命令 private void ChkCrnCmd(string cmd) { int nkind = 0; try { nkind = Int32.Parse(cmd.Substring(0, 2)); i_crn_step = nkind; Common.gi_crn_iotype[crnno - 1] = nkind; //i_crn_iotype = nkind; } catch (Exception em) { i_crn_step = 6; Common.WriteLogFile("WcsError", "CraneThread/ChkCrnCmd--根据命令判断作业类型出错:" + em.Message); //i_crn_iotype = 6; } } /// /// 清除命令操作 /// /// private void Proc_ClearCmd(string cmd) { //int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { int type = Int32.Parse(cmd.Substring(0, 2)); //int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 3) { return; } //iotype = 3; //i_srow = 0;//源row //i_sbay = 0;//源bay //i_slev = 0;//源lev //i_drow = 0;//目标rpw //i_dbay = 0;//目标bay //i_dlev = 0;//目标lev Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_IDLE; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = 0; Common.crn_i_fstn[crnno - 1] = 0; Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = ""; Common.crn_s_commandstr[crnno - 1] = ""; Common.gs_crncmd[crnno - 1] = ""; //WriteCrnData(iotype, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); // ReadCrnData(); //WriteCrnData(0, 0, 0, 0, 0, 0, 0, 0); OperateResult result = siemensTcpNet.Write("DB90.0", (short)1); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); i_crn_step = 6; } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/Proc_ClearCmd--清除命令出错:" + em.Message); return; } } /// /// 回原点 /// /// private void Proc_RebackHp(string cmd) { if (Common.Mode[crnno - 1] != Common.ci_CRN_ONLINE || Common.CrnState[crnno - 1] != Common.ci_CRN_STS_IDLE || Common.AlarmCode[crnno - 1] != 0 || Common.TaskNo[crnno - 1] != 0)// || Common.Loaded[crnno - 1] != 0) { ReadCrnData(); return; } int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { //07 0001 0100203 0200304 int type = Int32.Parse(cmd.Substring(0, 2)); int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 4) { return; } iotype = 5; i_srow = Int32.Parse(cmd.Substring(6, 2)); i_sbay = Int32.Parse(cmd.Substring(8, 3)); i_slev = Int32.Parse(cmd.Substring(11, 2)); i_drow = Int32.Parse(cmd.Substring(13, 2)); i_dbay = Int32.Parse(cmd.Substring(15, 3)); i_dlev = Int32.Parse(cmd.Substring(18, 2)); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_Wrkno[crnno - 1] = wrkno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_HP_POSITION; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = type; Common.crn_i_fstn[crnno - 1] = Common.GetStnByLoc(cmd.Substring(6, 7)); Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = cmd.Substring(13, 7); Common.CrnState[crnno - 1] = 3; WriteCrnData(iotype, wrkno, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/Proc_RebackHp--回原点除命令出错:" + em.Message); return; } } /// /// 坐标移动 /// /// private void Proc_CoordMove(string cmd) { if (Common.Mode[crnno - 1] != Common.ci_CRN_ONLINE || Common.CrnState[crnno - 1] != Common.ci_CRN_STS_IDLE || Common.AlarmCode[crnno - 1] != 0 || Common.TaskNo[crnno - 1] != 0)// || Common.Loaded[crnno - 1] != 0) { ReadCrnData(); return; } int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { //07 0001 0100203 0200304 int type = Int32.Parse(cmd.Substring(0, 2)); int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 5) { return; } Common.crn_i_Wrkno[crnno - 1] = wrkno; iotype = 7; i_srow = Int32.Parse(cmd.Substring(6, 2)); i_sbay = Int32.Parse(cmd.Substring(8, 3)); i_slev = Int32.Parse(cmd.Substring(11, 2)); i_drow = Int32.Parse(cmd.Substring(13, 2)); i_dbay = Int32.Parse(cmd.Substring(15, 3)); i_dlev = Int32.Parse(cmd.Substring(18, 2)); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_Wrkno[crnno - 1] = wrkno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_MOVING; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = type; Common.crn_i_fstn[crnno - 1] = Common.GetStnByLoc(cmd.Substring(6, 7)); Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = cmd.Substring(13, 7); Common.CrnState[crnno - 1] = 3; WriteCrnData(iotype, wrkno, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/Proc_CoordMove--坐标移动命令出错:" + em.Message); return; } } /// /// 移动至站 /// /// private void Proc_MoveToStn(string cmd) { if (Common.Mode[crnno - 1] != Common.ci_CRN_ONLINE || Common.CrnState[crnno - 1] != Common.ci_CRN_STS_IDLE || Common.AlarmCode[crnno - 1] != 0 || Common.TaskNo[crnno - 1] != 0)// || Common.Loaded[crnno - 1] != 0) { ReadCrnData(); return; } int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { //07 0001 0100203 0200304 int type = Int32.Parse(cmd.Substring(0, 2)); int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 9) { return; } iotype = 4; i_srow = 0; i_sbay = 0; i_slev = 0; i_drow = 0; i_dbay = 0; i_dlev = Int32.Parse(cmd.Substring(13, 4)); i_srow = Int32.Parse(cmd.Substring(6, 2)); i_sbay = Int32.Parse(cmd.Substring(8, 3)); i_slev = Int32.Parse(cmd.Substring(11, 2)); i_drow = Int32.Parse(cmd.Substring(13, 2)); i_dbay = Int32.Parse(cmd.Substring(15, 3)); i_dlev = Int32.Parse(cmd.Substring(18, 2)); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_Wrkno[crnno - 1] = wrkno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_MOVING; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = type; Common.crn_i_fstn[crnno - 1] = Common.GetStnByLoc(cmd.Substring(6, 7)); Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = cmd.Substring(13, 7); Common.CrnState[crnno - 1] = 3; WriteCrnData(iotype, wrkno, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/Proc_MoveToStn--移动至站命令出错:" + em.Message); return; } } /// /// 入库 /// /// private void Proc_Store(string cmd) { if (Common.Mode[crnno - 1] != Common.ci_CRN_ONLINE || Common.CrnState[crnno - 1] != Common.ci_CRN_STS_IDLE || Common.AlarmCode[crnno - 1] != 0 || Common.TaskNo[crnno - 1] != 0)// || Common.Loaded[crnno - 1] != 0) { //DisplayInfo("Crane abnormal or not in the computer models or not in standby state or crane not free,Please Check!" + " Datetime:" + DateTime.Now.ToString());//同步显示 ReadCrnData(); return; } int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { //07 0001 0100203 0200304 int type = Int32.Parse(cmd.Substring(0, 2)); int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 7) { return; } iotype = 1; i_srow = Int32.Parse(cmd.Substring(6, 2)); i_sbay = Int32.Parse(cmd.Substring(8, 3)); i_slev = Int32.Parse(cmd.Substring(11, 2)); i_drow = Int32.Parse(cmd.Substring(13, 2)); i_dbay = Int32.Parse(cmd.Substring(15, 3)); i_dlev = Int32.Parse(cmd.Substring(18, 2)); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_Wrkno[crnno - 1] = wrkno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_STORE_MOVE; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = type; Common.crn_i_fstn[crnno - 1] = Common.GetStnByLoc(cmd.Substring(6, 7)); Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = cmd.Substring(13, 7); Common.CrnState[crnno - 1] = 3; WriteCrnData(iotype, wrkno, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/Proc_Store--入库命令出错:" + em.Message); return; } } /// /// 去反原点 /// /// private void Proc_RebackOHP(string cmd) { if (Common.Mode[crnno - 1] != Common.ci_CRN_ONLINE || Common.CrnState[crnno - 1] != Common.ci_CRN_STS_IDLE || Common.AlarmCode[crnno - 1] != 0 || Common.TaskNo[crnno - 1] != 0)// || Common.Loaded[crnno - 1] != 0) { ReadCrnData(); return; } int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { //07 0001 0100203 0200304 int type = Int32.Parse(cmd.Substring(0, 2)); int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 10) { return; } iotype = 7;// 6; i_srow = Int32.Parse(cmd.Substring(6, 2)); i_sbay = Int32.Parse(cmd.Substring(8, 3)); i_slev = Int32.Parse(cmd.Substring(11, 2)); i_drow = Int32.Parse(cmd.Substring(13, 2)); i_dbay = Int32.Parse(cmd.Substring(15, 3)); i_dlev = Int32.Parse(cmd.Substring(18, 2)); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_Wrkno[crnno - 1] = wrkno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_MOVING; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = type; Common.crn_i_fstn[crnno - 1] = Common.GetStnByLoc(cmd.Substring(6, 7)); Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = cmd.Substring(13, 7); Common.CrnState[crnno - 1] = 3; WriteCrnData(iotype, wrkno, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); } catch (Exception em) { Common.gs_crncmd[crnno - 1] = "";//必须赋空 DisplayInfo("【" + DateTime.Now.ToString() + "】" + crnno + "去反原点命令失败: " + em.Message);//同步显示 Common.WriteLogFile("WcsError", "CraneThread/Proc_RebackOHP--去反原点命令出错:" + em.Message); return; } } /// /// 出库 /// /// private void Proc_Retrieve(string cmd) { if (Common.Mode[crnno - 1] != Common.ci_CRN_ONLINE || Common.CrnState[crnno - 1] != Common.ci_CRN_STS_IDLE || Common.AlarmCode[crnno - 1] != 0 || Common.TaskNo[crnno - 1] != 0)// || Common.Loaded[crnno - 1] != 0) { DateTime.Now.ToString();//同步显示 ReadCrnData(); return; } int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { int type = Int32.Parse(cmd.Substring(0, 2)); int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 8) { return; } iotype = 2; i_srow = Int32.Parse(cmd.Substring(6, 2)); i_sbay = Int32.Parse(cmd.Substring(8, 3)); i_slev = Int32.Parse(cmd.Substring(11, 2)); i_drow = Int32.Parse(cmd.Substring(13, 2)); i_dbay = Int32.Parse(cmd.Substring(15, 3)); i_dlev = Int32.Parse(cmd.Substring(18, 2)); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_Wrkno[crnno - 1] = wrkno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_STORE_MOVE; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = type; Common.crn_i_fstn[crnno - 1] = 0; Common.crn_i_tstn[crnno - 1] = Common.GetStnByLoc(cmd.Substring(13, 7)); Common.crn_s_Flocno[crnno - 1] = cmd.Substring(6, 7); Common.crn_s_Tlocno[crnno - 1] = ""; Common.CrnState[crnno - 1] = 3; WriteCrnData(iotype, wrkno, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/Proc_Retrieve--出库命令出错:" + em.Message); return; } } /// /// 站到站操作 /// /// private void Proc_StnToStn(string cmd) { if (Common.Mode[crnno - 1] != Common.ci_CRN_ONLINE || Common.CrnState[crnno - 1] != Common.ci_CRN_STS_IDLE || Common.AlarmCode[crnno - 1] != 0 || Common.TaskNo[crnno - 1] != 0)// || Common.Loaded[crnno - 1] != 0) { ReadCrnData(); return; } int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { int type = Int32.Parse(cmd.Substring(0, 2)); int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 11) { return; } iotype = 4; i_srow = Int32.Parse(cmd.Substring(6, 2)); i_sbay = Int32.Parse(cmd.Substring(8, 3)); i_slev = Int32.Parse(cmd.Substring(11, 2)); i_drow = Int32.Parse(cmd.Substring(13, 2)); i_dbay = Int32.Parse(cmd.Substring(15, 3)); i_dlev = Int32.Parse(cmd.Substring(18, 2)); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_Wrkno[crnno - 1] = wrkno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_MOVING; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = type; Common.crn_i_fstn[crnno - 1] = Common.GetStnByLoc(cmd.Substring(6, 7)); Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = cmd.Substring(13, 7); Common.CrnState[crnno - 1] = 3; WriteCrnData(iotype, wrkno, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/Proc_StnToStn--站到站命令出错:" + em.Message); return; } } /// /// 库位移转操作 /// /// private void Proc_LocationMove(string cmd) { if (Common.Mode[crnno - 1] != Common.ci_CRN_ONLINE || Common.CrnState[crnno - 1] != Common.ci_CRN_STS_IDLE || Common.AlarmCode[crnno - 1] != 0)// || Common.TaskNo[crnno - 1] != 0)// || Common.Loaded[crnno - 1] != 0) { ReadCrnData(); return; } int iotype = 0, i_srow = 0, i_sbay = 0, i_slev = 0, i_drow = 0, i_dbay = 0, i_dlev = 0; if (cmd == "" || cmd == null) { return; } try { int type = Int32.Parse(cmd.Substring(0, 2)); int wrkno = Int32.Parse(cmd.Substring(2, 4)); if (type != 12) { return; } iotype = 3; i_srow = Int32.Parse(cmd.Substring(6, 2)); i_sbay = Int32.Parse(cmd.Substring(8, 3)); i_slev = Int32.Parse(cmd.Substring(11, 2)); i_drow = Int32.Parse(cmd.Substring(13, 2)); i_dbay = Int32.Parse(cmd.Substring(15, 3)); i_dlev = Int32.Parse(cmd.Substring(18, 2)); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_Wrkno[crnno - 1] = wrkno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_LOCATION_MOVE; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = type; Common.crn_i_fstn[crnno - 1] = 0; Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = cmd.Substring(6, 7); Common.crn_s_Tlocno[crnno - 1] = cmd.Substring(13, 7); Common.CrnState[crnno - 1] = 3; WriteCrnData(iotype, wrkno, i_srow, i_sbay, i_slev, i_drow, i_dbay, i_dlev); Thread.Sleep(Common.ci_CRNtimeInterval); ReadCrnData(); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/Proc_LocationMove--库位移转命令出错:" + em.Message); return; } } #region 堆垛机读写指令 /// /// 读取堆垛机状态数据 /// private void ReadCrnData() { string s_startadd = ""; int num = 0, i_len = 0; recvStr = ""; try { s_startadd = "DB91.0"; //读取堆垛机状态起始地址 i_len =60; //读取堆垛机状态地址长度 OperateResult read = siemensTcpNet.Read(s_startadd, (ushort)i_len); if (read.IsSuccess) { for (int i = 0; i < 14; i++) //DB8.18到DB8.52 { num = siemensTcpNet.ByteTransform.TransInt16(read.Content, i * 2); //buf1 = read.Content; //num = 256 * (int)buf1[i * 2] + (int)buf1[i * 2 + 1]; recvStr = recvStr + num.ToString(); AnalyseStatus(i, num); } DisplayInfo("【" + DateTime.Now.ToString() + "】" + recvStr);//同步显示 } else { DisplayInfo("【" + DateTime.Now.ToString() + "】扫描堆垛机状态失败:" + read.ToMessageShowString()); } OperateResult read1 = siemensTcpNet.ReadFloat("DB91.28", (ushort)7); if (read.IsSuccess) { for (int i = 0; i < 7; i++) //DB8.18到DB8.50 { float num1 = read1.Content[i]; recvStr = recvStr + num1.ToString(); AnalyseStatus(i + 17, num1); } } } catch (Exception em) { //DisplayInfo("读" + crnno.ToString() + "号吊车目标主机无应答,可能原因:吊车ip设定异常或丢失,请检查!");//同步显示 DisplayInfo("【" + DateTime.Now.ToString() + "】扫描堆垛机状态失败: " + em.Message);//同步显示 Common.WriteLogFile("WcsError", "CraneThread/ReadCrnData--读取堆垛机状态失败:" + em.Message); return; } } /// /// 堆垛机完成入出库任务后,更新工作状态 /// /// private void UpdateWrkStsByFinish(int wrk_no) { int wrk_sts = 0, iotype = 0, newwrk_sts = 0; try { DateTime dt1 = System.DateTime.Now; DataView dv = Common.ExecAsrsSelect("select top 1 wrk_sts,io_type from dbo.asr_wrk_mast where wrk_no=" + wrk_no + ""); foreach (System.Data.DataRowView drow in dv) { wrk_sts = Convert.ToInt32(drow[0].ToString()); iotype = Convert.ToInt32(drow[1].ToString()); } if ((wrk_sts == 3 && (iotype == 1 || iotype == 10 || iotype == 53 || iotype == 54 || iotype == 57)) || (wrk_sts == 12 && iotype == 11)) { newwrk_sts = 4; } else if (wrk_sts == 12 && (iotype == 101 || iotype == 110) || iotype == 103 || iotype == 104 || iotype == 107) { newwrk_sts = 14; } if (newwrk_sts > 0) { bool result = Common.ExecAsrsModify("update dbo.asr_wrk_mast set wrk_sts=" + newwrk_sts + " ,crn_end_time ='" + dt1 + "' where wrk_no =" + wrk_no + ""); } } catch (Exception em) { DisplayInfo("【" + DateTime.Now.ToString() + "】" + crnno + "(" + wrk_no + ")#更新工作状态失败: " + em.Message); Common.WriteLogFile("WcsError", "CraneThread/UpdateWrkStsByFinish--更新工作状态失败--工作号" + wrk_no + ":" + em.Message); } } /// /// 根据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", "CraneThread/GetStringByASCII--ASCII转换字符串失败:" + em.Message); return ""; } } #region 分析状态 /// /// 分析堆垛机状态 /// /// 地址序列 /// 状态值 private void AnalyseStatus(int i, float lnum) { int num = (int)lnum; switch (i) { case 0: //堆垛机模式 Common.Mode[crnno - 1] = num; break; case 1: //工作号/任务号 Common.TaskNo[crnno - 1] = num; break; case 2: //堆垛机状态 Common.CrnState[crnno - 1] = num; if (Common.Mode[crnno - 1] == Common.ci_CRN_ONLINE && Common.CrnState[crnno - 1] == Common.ci_CRN_STS_TASK_FINISH) { Common.TaskFinish[crnno - 1] = 1; } else { Common.TaskFinish[crnno - 1] = 0; } break; //测试用 //Common.CrnState[crnno - 1] = num; //if (Common.Mode[crnno - 1] == Common.ci_CRN_ONLINE && Common.CrnState[crnno - 1] == Common.ci_CRN_STS_TASK_FINISH // && Common.gi_Online_Flag == Common.ch_CMD_PAUSE && Common.TaskNo[crnno - 1] == 9999) //{//系统暂停模式,堆垛机状态显示等待WCS确认,任务号为9999,表示手工下发的堆垛机任务,置位确认位 // OperateResult result = siemensTcpNet.Write("DB90.0", (short)1); // Common.TaskFinish[crnno - 1] = 1; //} //else //{ // Common.TaskFinish[crnno - 1] = 0; //} //break; case 3: //当前列号 Common.crn_dqps[crnno - 1] = num; break; case 4: //当前层号 Common.crn_dqcs[crnno - 1] = num; break; case 5: //货叉位置 Common.ForkPos[crnno - 1] = num; break; case 6: //载货台位置 Common.LiftPos[crnno - 1] = num; break; case 7: //走行是否在定位 Common.WalkPos[crnno - 1] = num; break; case 8: //载货台是否有物 Common.Loaded[crnno - 1] = num; break; case 9: //堆垛机异常代码 Common.AlarmCode[crnno - 1] = num; if (Common.AlarmCode[crnno - 1] == 40 || Common.AlarmCode[crnno - 1] == 41) { Common.crn_jt[crnno - 1] = true; } else { Common.crn_jt[crnno - 1] = false; } break; //case 11: //堆垛机任务完成 // if (Common.Mode[crnno - 1] == Common.ci_CRN_ONLINE && Common.CrnState[crnno - 1] == Common.ci_CRN_STS_TASK_FINISH) // { // Common.TaskFinish[crnno - 1] = 1; // } // else // { // Common.TaskFinish[crnno - 1] = 0; // } // break; case 17: //堆垛机行走速度 Common.WalkSpeed[crnno - 1] = System.Math.Abs(lnum); break; case 18: //堆垛机升降速度 Common.LiftSpeed[crnno - 1] = System.Math.Abs(lnum); break; case 19: //堆垛机叉牙速度 Common.ForkSpeed[crnno - 1] = System.Math.Abs(lnum); break; case 20: //走行总距离 Common.XDistance[crnno - 1] = System.Math.Abs(lnum); break; case 21: //升降总距离 Common.YDistance[crnno - 1] = System.Math.Abs(lnum); break; case 22: //走行总时长 Common.XDuration[crnno - 1] = System.Math.Abs(lnum); break; case 23: //升降总时长 Common.YDuration[crnno - 1] = System.Math.Abs(lnum); break; } #region 施耐德 //if (i == 0) //{ // if (Common.fun_ChkAndByte(num, Common.ch_crn_sdms) == true) // { // Common.crn_sdms[crnno - 1] = true; // } // else // { // Common.crn_sdms[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_zdms) == true) // { // Common.crn_zdms[crnno - 1] = true; // } // else // { // Common.crn_zdms[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_dnms) == true) // { // Common.crn_dnms[crnno - 1] = true; // } // else // { // Common.crn_dnms[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_sdz) == true) // { // Common.crn_sdz[crnno - 1] = true; // } // else // { // Common.crn_sdz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_zdz) == true) // { // Common.crn_zdz[crnno - 1] = true; // } // else // { // Common.crn_zdz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_dnmsz) == true) // { // Common.crn_dnmsz[crnno - 1] = true; // } // else // { // Common.crn_dnmsz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_jt) == true) // { // Common.crn_jt[crnno - 1] = true; // } // else // { // Common.crn_jt[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_yc) == true) // { // Common.crn_yc[crnno - 1] = true; // } // else // { // Common.crn_yc[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_dndmz) == true) // { // Common.crn_dndmz[crnno - 1] = true; // } // else // { // Common.crn_dndmz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_dnmljs) == true) // { // Common.crn_dnmljs[crnno - 1] = true; // } // else // { // Common.crn_dnmljs[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_rkz) == true) // { // Common.crn_rkz[crnno - 1] = true; // } // else // { // Common.crn_rkz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_ckz) == true) // { // Common.crn_ckz[crnno - 1] = true; // } // else // { // Common.crn_ckz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_kdkz) == true) // { // Common.crn_kdkz[crnno - 1] = true; // } // else // { // Common.crn_kdkz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_zdzz) == true) // { // Common.crn_zdzz[crnno - 1] = true; // } // else // { // Common.crn_zdzz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_yxz) == true) // { // Common.crn_yxz[crnno - 1] = true; // } // else // { // Common.crn_yxz[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_dcqhjd) == true) // { // Common.crn_dcqhjd[crnno - 1] = true; // } // else // { // Common.crn_dcqhjd[crnno - 1] = false; // } //} //else if (i == 1) //{ // if (Common.fun_ChkAndByte(num, Common.ch_crn_dcfhjd) == true) // { // Common.crn_dcfhjd[crnno - 1] = true; // } // else // { // Common.crn_dcfhjd[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_dckx) == true) // { // Common.crn_dckx[crnno - 1] = true; // } // else // { // Common.crn_dckx[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_zxyddw) == true) // { // Common.crn_zxyddw[crnno - 1] = true; // } // else // { // Common.crn_zxyddw[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_sjyddw) == true) // { // Common.crn_sjyddw[crnno - 1] = true; // } // else // { // Common.crn_sjyddw[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_zxzdw) == true) // { // Common.crn_zxzdw[crnno - 1] = true; // } // else // { // Common.crn_zxzdw[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_cyzzj) == true) // { // Common.crn_cyzzj[crnno - 1] = true; // } // else // { // Common.crn_cyzzj[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_cyzzd) == true) // { // Common.crn_cyzzd[crnno - 1] = true; // } // else // { // Common.crn_cyzzd[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_cyzyd) == true) // { // Common.crn_cyzyd[crnno - 1] = true; // } // else // { // Common.crn_cyzyd[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_sjsdw) == true) // { // Common.crn_sjsdw[crnno - 1] = true; // } // else // { // Common.crn_sjsdw[crnno - 1] = false; // } // if (Common.fun_ChkAndByte(num, Common.ch_crn_sjxdw) == true) // { // Common.crn_sjxdw[crnno - 1] = true; // } // else // { // Common.crn_sjxdw[crnno - 1] = false; // } // //if (Common.fun_ChkAndByte(num, Common.ch_crn_plcdlbz) == true) // //{ // // Common.crn_zxzdw[crnno - 1] = true; // //} // //else // //{ // // Common.crn_zxzdw[crnno - 1] = false; // //} // if (Common.fun_ChkAndByte(num, Common.ch_crn_sjtycyw) == true) // { // Common.crn_sjtycyw[crnno - 1] = true; // } // else // { // Common.crn_sjtycyw[crnno - 1] = false; // } //} //else if (i == 5) //当前排数 //{ // try // { // Common.crn_dqps[crnno - 1] = num; // } // catch (Exception t) // { // Common.crn_dqps[crnno - 1] = 0; // } //} //else if (i == 6) //当前层数 //{ // try // { // Common.crn_dqcs[crnno - 1] = num; // } // catch (Exception t) // { // Common.crn_dqcs[crnno - 1] = 0; // } //} //else if (i == 7) //设备异常码 //{ // try // { // Common.crn_ycmm[crnno - 1] = num; // } // catch (Exception t) // { // Common.crn_ycmm[crnno - 1] = 0; // } //} ////else if (i == 8) //走型镭射值2 ////{ //// //distance1 = distance1.ToString//num; //// Common.crn_zxlsz2[crnno - 1] = num; //// try //// { //// Common.crn_zxlsz[crnno - 1] = double.Parse(Common.crn_zxlsz1[crnno - 1].ToString() + "." + Common.crn_zxlsz2[crnno - 1].ToString()) * 0.001; //// } //// catch (Exception r) //// { //// Common.crn_zxlsz[crnno - 1] = 0; //// } ////} ////else if (i == 9) //升降镭射值 ////{ //// try //// { //// Common.crn_sjlsz1[crnno - 1] = num; //// } //// catch (Exception t) //// { //// Common.crn_sjlsz1[crnno - 1] = 0; //// } ////} ////else if (i == 10)//距离 ////{ //// Common.crn_sjlsz2[crnno - 1] = num; //// try //// { //// Common.crn_sjlsz[crnno - 1] = double.Parse(Common.crn_sjlsz1[crnno - 1].ToString() + "." + Common.crn_sjlsz2[crnno - 1].ToString()) * 0.001; //// } //// catch (Exception r) //// { //// Common.crn_sjlsz[crnno - 1] = 0; //// } ////} ////else if (i == 23)//运行异常母码 ////{ //// Common.crn_ycmm[crnno-1] = num; ////} ////else if (i == 24)//运行异常子码 ////{ //// Common.crn_yczm[crnno - 1] = num; ////} //Common.crn_dndmz[crnno - 1] = Common.crn_dckx[crnno - 1]; //if (Common.crn_ycmm[crnno - 1] > 0) //{ // Common.crn_i_Errcod[crnno - 1] = Common.ci_CRN_STS_ERROR; // // Class1.crn_i_crn_sts[crnno - 1] = Class1.ci_CRN_STS_ERROR; // //if (Common.crn_ycmm[crnno - 1] > 0) // //{ // Common.crnerrlist[crnno - 1] = crnno.ToString() + "号吊车发生异常,异常号:" + Common.GetErr(Common.crn_ycmm[crnno - 1]); // //} // //else if (Common.crn_ycmm[crnno - 1] == 0) // //{ // // Common.crnerrlist[crnno - 1] = ""; // //} // //else // //{ // // Common.crnerrlist[crnno - 1] = crnno.ToString() + "号吊车指令异常!"; // //} //} //else //{ // Common.crn_i_Errcod[crnno - 1] = Common.ci_CRN_SETTING; // //Class1.crn_i_crn_sts[crnno - 1] = Class1.gi_crn_actsts[crnno - 1]; // Common.crnerrlist[crnno - 1] = ""; // //Common.CrnErrFlag[crnno - 1] = true; //} #endregion //判断原点标志 if (Common.crn_dqps[crnno - 1] == 1 && Common.crn_dqcs[crnno - 1] == 1 && Common.ForkPos[crnno - 1] == 0 && Common.LiftPos[crnno - 1] == 1 && Common.WalkPos[crnno - 1] == 1) { Common.crn_i_onHP[crnno - 1] = 1; } else { Common.crn_i_onHP[crnno - 1] = 0; } //电脑模式、空闲、任务号为0,清空堆垛机状态数组 if (Common.Mode[crnno - 1] == Common.ci_CRN_ONLINE && Common.CrnState[crnno - 1] == Common.ci_CRN_STS_IDLE && Common.TaskNo[crnno - 1] == 0) { Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_IDLE; Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = 0; Common.crn_i_fstn[crnno - 1] = 0; Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = ""; Common.crn_s_commandstr[crnno - 1] = ""; } //处理堆垛机搬运完成后,更新工作档数据 if (Common.TaskFinish[crnno - 1] == 1 && Common.TaskNo[crnno - 1] > 0 && Common.TaskFlag[crnno - 1] == 1) { //WCS收到堆垛机任务完成TaskFinish信号后,处理业务逻辑并置位DB8.0确认位 OperateResult result = siemensTcpNet.Write("DB90.0", (short)1); //更新工作状态3为4,12为14 //2019-04-30修改,考虑到出库状态12->14时,需要下发命令给PLC, //将TaskFinish标记丢给timer1时间中Proc_StoreFinished处理入库, // //UpdateWrkStsByFinish((int)Common.F1TaskNo[crnno - 1]); Common.crn_i_crnno[crnno - 1] = crnno; Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_IDLE; //Common.crn_i_Errcod[crnno - 1] = 0; Common.crn_i_kind[crnno - 1] = 0; Common.crn_i_fstn[crnno - 1] = 0; Common.crn_i_tstn[crnno - 1] = 0; Common.crn_s_Flocno[crnno - 1] = ""; Common.crn_s_Tlocno[crnno - 1] = ""; Common.crn_s_commandstr[crnno - 1] = ""; Common.TaskFlag[crnno - 1] = 0; } DisplayParamInfo(); #region 施耐德 ////判断吊车入出状态 //if (Common.crn_dnms[crnno - 1] == true && Common.crn_dnmsz[crnno - 1] == true && // Common.crn_dckx[crnno - 1] == false && Common.crn_dnmljs[crnno - 1] == true && // (Common.crn_rkz[crnno - 1] == true || Common.crn_ckz[crnno - 1] == true || Common.crn_kdkz[crnno - 1] == true // || Common.crn_zdzz[crnno - 1] == true || Common.crn_yxz[crnno - 1] == true // || Common.crn_yxz[crnno - 1] == true || Common.crn_dcqhjd[crnno - 1] == true)) //{ // //Class1.crn_i_onHP[crnno - 1] = 0; // //Class1.crn_i_Errcod[crnno - 1] = 0; // switch (Common.gi_crn_iotype[crnno - 1]) // { // case 4://回原点 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_MOVING; // break; // case 5://坐标移行 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_MOVING; // break; // case 7://入库 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_STORE_MOVE; // break; // case 8://出库 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_RETRIEVE_MOVE; // break; // case 9://移动至站点 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_MOVING; // break; // case 10://去反原点 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_OHP; // break; // case 11://站到站 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_STNCHG_MOVE; // break; // case 12://库位移转 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_LOCATION_MOVE; // break; // } //} ////根据吊车的完成,更新吊车对应状态,moving---->move over //if (Common.gi_cycle_flag == 0) //{ // Common.gi_cycle_flag = 1; //} //else //{ // if (Common.crn_dnms[crnno - 1] == true && Common.crn_dnmsz[crnno - 1] == true && Common.crn_dckx[crnno - 1] == true // //&& Common.crn_dnmljs[crnno - 1] == true && Common.crn_yxz[crnno - 1] == false) // && Common.crn_dndmz[crnno - 1] == true && Common.crn_yxz[crnno - 1] == false) // //(Class1.crn_sbzc[crnno-1]==true && Class1.crn_dndm[crnno-1]==true && Class1.crn_dnmljs[crnno-1]==true && Class1.crn_dnz[crnno-1]==true) // { // switch (Common.gi_crn_iotype[crnno - 1]) // { // case 7://入库 // if (Common.crn_i_crn_sts[crnno - 1] == 2) // { // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_STORE_OK; // Common.crn_s_commandstr[crnno - 1] = ""; // } // break; // case 8://出库 // if (Common.crn_i_crn_sts[crnno - 1] == 3) // { // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_RETRIEVE_OK; // Common.crn_s_commandstr[crnno - 1] = ""; // } // break; // case 4://回原点 // //if (Common.crn_i_crn_sts[crnno - 1] == 4) // //{ // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_IDLE; // Common.crn_s_commandstr[crnno - 1] = ""; // Common.crn_i_Wrkno[crnno - 1] = 0; // //} // break; // case 5://坐标移行 // if (Common.crn_i_crn_sts[crnno - 1] == 4) // { // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_IDLE; // Common.crn_s_commandstr[crnno - 1] = ""; // } // break; // case 9://移动至站点 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_IDLE; // Common.crn_s_commandstr[crnno - 1] = ""; // break; // case 10://去反原点 // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_IDLE; // Common.crn_s_commandstr[crnno - 1] = ""; // Common.crn_i_Wrkno[crnno - 1] = 0; // break; // case 11://站到站 // if (Common.crn_i_crn_sts[crnno - 1] == Common.ci_CRN_STS_STNCHG_MOVE) // { // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_STNCHG_OK; // Common.crn_s_commandstr[crnno - 1] = ""; // } // break; // case 12://库到库 // if (Common.crn_i_crn_sts[crnno - 1] == Common.ci_CRN_STS_LOCATION_MOVE) // { // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_LOCATION_MOVE_OK; // Common.crn_s_commandstr[crnno - 1] = ""; // } // break; // default: // Common.crn_i_crn_sts[crnno - 1] = Common.ci_CRN_STS_IDLE; // Common.crn_i_Wrkno[crnno - 1] = 0; // Common.crn_i_crnno[crnno - 1] = crnno; // Common.crn_i_fstn[crnno - 1] = 0; // Common.crn_i_tstn[crnno - 1] = 0; // Common.crn_s_Flocno[crnno - 1] = ""; // Common.crn_s_Tlocno[crnno - 1] = ""; // Common.crn_i_kind[crnno - 1] = 0; // Common.crn_s_commandstr[crnno - 1] = ""; // break; // } // } // Common.gi_cycle_flag = 0; //} //int sstn = Common.crn_i_fstn[crnno - 1]; //int dstn = Common.crn_i_tstn[crnno - 1]; //int i_sstnno = Common.GetStnSeqNo(crnno, sstn); //int i_dstnno = Common.GetStnSeqNo(crnno, dstn); #endregion } #endregion /// /// 发送命令到堆垛机 /// /// /// 工作号 /// 类型 /// 源排 /// 源列 /// 源层 /// 目标排 /// 目标列 /// 目标层 private void WriteCrnData(int type, int wrkno, int srow, int sbay, int slev, int drow, int dbay, int dlev) { try { string s_startadd = ""; short[] iArr_value = new short[8]; bool ok = false; int icount = 0; s_startadd = "DB90.2"; iArr_value[0] = (short)wrkno; iArr_value[1] = (short)type; iArr_value[2] = (short)srow; iArr_value[3] = (short)sbay; iArr_value[4] = (short)slev; iArr_value[5] = (short)drow; iArr_value[6] = (short)dbay; iArr_value[7] = (short)dlev; while (ok == false) { try { OperateResult result = siemensTcpNet.Write(s_startadd, iArr_value); result = siemensTcpNet.Write("DB90.18", (ushort)1); if (result.IsSuccess) { ok = true; UpdateWrkStsByCmd(wrkno); Common.gs_crncmd[crnno - 1] = "";//必须赋空 } else { icount++; if (icount > 3) { ok = true; Common.gs_crncmd[crnno - 1] = "";//必须赋空 } else { ok = false; } } Thread.Sleep(Common.ci_CRNtimeInterval); } catch (Exception em) { Common.gs_crncmd[crnno - 1] = "";//必须赋空 // DisplayInfo("写" + crnno.ToString() + "号吊车目标主机无应答,可能原因:吊车ip设定异常或丢失,请检查!");//同步显示 return; } } } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/WriteCrnData--写命令到堆垛机出错:" + em.Message); } } /// /// 给堆垛机下发任务后,更新工作状态 /// /// private void UpdateWrkStsByCmd(int wrk_no) { int wrk_sts = 0, iotype = 0, newwrk_sts = 0; try { DateTime dt1 = System.DateTime.Now; DataView dv = Common.ExecAsrsSelect("select top 1 wrk_sts,io_type from dbo.asr_wrk_mast where wrk_no=" + wrk_no + ""); foreach (System.Data.DataRowView drow in dv) { wrk_sts = Convert.ToInt32(drow[0].ToString()); iotype = Convert.ToInt32(drow[1].ToString()); } if ((wrk_sts == 2 && (iotype == 1 || iotype == 10 || iotype == 53 || iotype == 54 || iotype == 57)) || (wrk_sts == 11 && iotype == 11)) { newwrk_sts = 3; } else if (wrk_sts == 11 && (iotype == 101 || iotype == 110) || iotype == 103 || iotype == 104 || iotype == 107) { newwrk_sts = 12; } if (newwrk_sts > 0) { bool result = Common.ExecAsrsModify("update dbo.asr_wrk_mast set wrk_sts=" + newwrk_sts + " ,crn_str_time ='" + dt1 + "' where wrk_no =" + wrk_no + ""); } } catch (Exception em) { DisplayInfo("【" + DateTime.Now.ToString() + "】" + crnno + "(" + wrk_no + ")#更新工作状态失败: " + em.Message); Common.WriteLogFile("WcsError", "CraneThread/UpdateWrkStsByFinish--更新工作状态失败--工作号" + wrk_no + ":" + em.Message); } } #endregion #region 画面同步显示 /// /// 同步显示主画面堆垛机信息 /// /// 堆垛机号 /// 列 /// 层 /// 距离 /// 高度 /// 异常 private void DisplayCrnstsData(int crn, int bay, int lev, double dist, double height, int err) { if (tb.InvokeRequired) { try { DisplaycrnstsData d = new DisplaycrnstsData(DisplayCrnstsData); tb.Invoke(d, new object[] { crn, bay, lev, dist, height, err }); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/DisplayCrnstsData--同步显示主画面堆垛机信息失败:" + em.Message); } } else { crnstslist.Items.Clear(); ListViewItem lvi = new ListViewItem(); lvi.SubItems[0].Text = crn.ToString(); lvi.SubItems.Add(bay.ToString()); lvi.SubItems.Add(lev.ToString()); lvi.SubItems.Add(dist.ToString()); lvi.SubItems.Add(height.ToString()); lvi.SubItems.Add(err.ToString()); this.crnstslist.Items.Add(lvi); } } /// /// 堆垛机通讯反馈信息 /// /// private void DisplayInfo(string text) { if (tb.InvokeRequired) { try { SetTextCallback d = new SetTextCallback(DisplayInfo); tb.Invoke(d, new object[] { text }); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/DisplayInfo--同步显示堆垛机通讯信息失败:" + em.Message); } } else { if (tb.Items.Count > 30) { tb.Items.Clear(); } tb.Items.Add(text); tb.TopIndex = tb.Items.Count - (int)(tb.Height / tb.ItemHeight); } } /// /// 堆垛机画面状态显示信息 /// private void DisplayParamInfo() { if (form1.InvokeRequired) { try { SetTextCallback1 d = new SetTextCallback1(DisplayParamInfo); form1.Invoke(d, new object[] { }); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/DisplayParamInfo--同步显示堆垛机画面状态信息失败:" + em.Message); } } else { tb_sendtocrn.Text = Common.crn_s_commandstr[crnno - 1]; switch (Common.Mode[crnno - 1]) { case 1: dgv_crnsts["col_Mode", crnno - 1].Value = "手动"; break; case 2: dgv_crnsts["col_Mode", crnno - 1].Value = "自动"; break; case 3: dgv_crnsts["col_Mode", crnno - 1].Value = "连线"; break; case 4: dgv_crnsts["col_Mode", crnno - 1].Value = "手动中"; break; case 5: dgv_crnsts["col_Mode", crnno - 1].Value = "自动中"; break; case 6: dgv_crnsts["col_Mode", crnno - 1].Value = "连线中"; break; } dgv_crnsts["col_Mode", crnno - 1].Style.ForeColor = Common.Mode[crnno - 1] == 6 ? Color.Red : Color.Black; dgv_crnsts["col_ErrorCode", crnno - 1].Value = Common.AlarmCode[crnno - 1]; dgv_crnsts["col_ErrorCode", crnno - 1].Style.ForeColor = Common.AlarmCode[crnno - 1] > 0 ? Color.Red : Color.Black; if (Common.Loaded[crnno - 1] == 1) { dgv_crnsts["col_Loaded", crnno - 1].Value = "有物"; } else { dgv_crnsts["col_Loaded", crnno - 1].Value = "无物"; } dgv_crnsts["col_Loaded", crnno - 1].Style.ForeColor = Common.Loaded[crnno - 1] == 1 ? Color.Red : Color.Black; dgv_crnsts["col_Bay", crnno - 1].Value = Common.crn_dqps[crnno - 1]; dgv_crnsts["col_Lev", crnno - 1].Value = Common.crn_dqcs[crnno - 1]; dgv_crnsts["col_Status", crnno - 1].Value = Common.GetCrnStsName(Common.CrnState[crnno - 1]); dgv_crnsts["col_Status", crnno - 1].Style.ForeColor = Common.CrnState[crnno - 1] == 99 ? Color.Red : Color.Black; if (Common.crn_dqps[crnno - 1] == 1) { dgv_crnsts["col_Zxyd", crnno - 1].Value = "是"; } else { dgv_crnsts["col_Zxyd", crnno - 1].Value = "否"; } if (Common.crn_dqcs[crnno - 1] == 1) { dgv_crnsts["col_Sjyd", crnno - 1].Value = "是"; } else { dgv_crnsts["col_Sjyd", crnno - 1].Value = "否"; } if (Common.ForkPos[crnno - 1] == 0) { dgv_crnsts["col_Chaya", crnno - 1].Value = "中间"; } else if (Common.ForkPos[crnno - 1] == 1) { dgv_crnsts["col_Chaya", crnno - 1].Value = "左端"; } else if (Common.ForkPos[crnno - 1] == 2) { dgv_crnsts["col_Chaya", crnno - 1].Value = "右端"; } else { dgv_crnsts["col_Chaya", crnno - 1].Value = ""; } if (Common.WalkPos[crnno - 1] == 1) { dgv_crnsts["col_Zxdw", crnno - 1].Value = "是"; } else { dgv_crnsts["col_Zxdw", crnno - 1].Value = "否"; } if (Common.LiftPos[crnno - 1] == 1) { dgv_crnsts["col_Sjdw", crnno - 1].Value = "下定位"; } else if (Common.LiftPos[crnno - 1] == 2) { dgv_crnsts["col_Sjdw", crnno - 1].Value = "上定位"; } else { dgv_crnsts["col_Sjdw", crnno - 1].Value = "否"; } if (Common.crn_jt[crnno - 1]) { dgv_crnsts["col_Jiting", crnno - 1].Value = "是"; dgv_crnsts["col_Jiting", crnno - 1].Style.ForeColor = Color.Red; } else { dgv_crnsts["col_Jiting", crnno - 1].Value = ""; } dgv_crnsts["col_Xspeed", crnno - 1].Value = Common.WalkSpeed[crnno - 1].ToString("#0.00"); dgv_crnsts["col_Yspeed", crnno - 1].Value = Common.LiftSpeed[crnno - 1].ToString("#0.00"); dgv_crnsts["col_Zspeed", crnno - 1].Value = Common.ForkSpeed[crnno - 1].ToString("#0.00"); dgv_crnsts["col_Xdistance", crnno - 1].Value = Common.XDistance[crnno - 1].ToString("#0.00"); dgv_crnsts["col_Ydistance", crnno - 1].Value = Common.YDistance[crnno - 1].ToString("#0.00"); dgv_crnsts["col_Xduration", crnno - 1].Value = Common.XDuration[crnno - 1].ToString("#0.00"); dgv_crnsts["col_Yduration", crnno - 1].Value = Common.YDuration[crnno - 1].ToString("#0.00"); } } #endregion ///// ///// 通讯失败后初始化堆垛机状态 ///// //private void InitCrn() //{ // try // { // //Common.crn_i_crnno[crnno - 1] = i; // //crn_i_crn_sts[i - 1] = ci_CRN_SETTING; // //Common.gs_crncmd[crnno - 1] = ""; // //Common.crn_i_kind[crnno - 1] = 0; // //Common.crn_i_Wrkno[crnno - 1] = 0; // //Common.crn_i_fstn[crnno - 1] = 0; // //Common.crn_i_tstn[crnno - 1] = 0; // //Common.crn_s_Flocno[crnno - 1] = ""; // //Common.crn_s_Tlocno[crnno - 1] = ""; // //crn_i_Errcod[i - 1] = 0; // //Common.crn_i_onHP[crnno - 1] = 0; // //Common.gs_crnlastio[crnno - 1] = "I"; // //Common.gs_crncmd[crnno - 1] = ""; // //Common.crn_s_commandstr[crnno - 1] = ""; // //Common.gi_crn_iotype[crnno - 1] = 0; // //Common.gb_crn_status[crnno - 1] = true; // //Common.AlarmCode[crnno - 1] = 0; // //Common.F1TaskFlag[crnno - 1] = 0; // Common.F1State[crnno - 1] = -1; // } // catch (Exception) // { } //} /// /// 堆垛机主线程 /// private void ThreadTask() { i_crn_step = 6; connect = siemensTcpNet.ConnectServer(); while (true) { try { if (!connect.IsSuccess) { if (!ReConnect()) { Thread.Sleep(Common.ci_CRNtimeInterval); continue; } } ///gs_crncmd长度28: 12 0001 0100101 0200202 ///对应含义: 入出类型 工作号 源库位 目标库位 if (Common.gs_crncmd[crnno - 1].Length == 20) //|| Common.gs_crncmd[crnno - 1].Length == 21 //|| Common.gs_crncmd[crnno - 1].Length == 24) { ChkCrnCmd(Common.gs_crncmd[crnno - 1]); Common.crn_s_commandstr[crnno - 1] = Common.gs_crncmd[crnno - 1]; } switch (i_crn_step) { case 3: Proc_ClearCmd(Common.crn_s_commandstr[crnno - 1]);//清命令操作 break; case 4: Proc_RebackHp(Common.crn_s_commandstr[crnno - 1]);//召回,回原点操作 break; case 5: Proc_CoordMove(Common.crn_s_commandstr[crnno - 1]);//坐标移行操作 break; case 6: ReadCrnData();//执行查询指令操作 break; case 7: Proc_Store(Common.crn_s_commandstr[crnno - 1]);//入库操作 break; case 8: Proc_Retrieve(Common.crn_s_commandstr[crnno - 1]);//出库操作 break; case 9: Proc_MoveToStn(Common.crn_s_commandstr[crnno - 1]);//行至站操作 break; case 10: Proc_RebackOHP(Common.crn_s_commandstr[crnno - 1]);//去反原点操作 break; case 11: Proc_StnToStn(Common.crn_s_commandstr[crnno - 1]);//站到站操作 break; case 12: Proc_LocationMove(Common.crn_s_commandstr[crnno - 1]);//库位移载操作 break; //case 99: //任务完成复位信号 // string cmd = Common.crn_s_commandstr[crnno - 1]; // int value = Int32.Parse(cmd.Substring(2, 4)); // OperateResult result = siemensTcpNet.Write("DB8.0", value); // break; default: break; } i_crn_step = 6; //Thread.Sleep(Common.ci_CRNtimeInterval); } catch (Exception em) { Common.WriteLogFile("WcsError", "CraneThread/ThreadTask--堆垛机主线程循环出错:" + em.Message); } } } } }