//机械手运行到指定脉冲 private void btRunAuto_Click(object sender, RoutedEventArgs e) { ShowKey(false); //csKey.Close(); Cursor = Cursors.Wait; PLC_Tcp_CP.ChangeAdd(1); string x = tbTargetX.Text.Trim(); string z = tbTargetZ.Text.Trim(); if (string.IsNullOrEmpty(x)) { csMsg.ShowWarning("X轴脉冲不能为空", false); Cursor = null; return; } if (string.IsNullOrEmpty(z)) { csMsg.ShowWarning("Z轴脉冲不能为空", false); Cursor = null; return; } float xp; float zp; if (float.TryParse(x, out xp) && float.TryParse(z, out zp)) { PLC_Tcp_CP.ExtramanAutoMoveToPulse(xp, zp); DateTime timeBegin = DateTime.Now; Thread.Sleep(200); while (!PLC_Tcp_CP.ExtramanAutoMoveIsOK()) { if (DateTime.Now > timeBegin.AddSeconds(Config.Mac_C.WaitTime_Auto_Extraman)) { break; } Thread.Sleep(200); } if (DateTime.Now <= timeBegin.AddSeconds(Config.Mac_C.WaitTime_Auto_Extraman)) { tbNowX.Text = PLC_Tcp_CP.ReadExtramanPulseX().ToString(); tbNowZ.Text = PLC_Tcp_CP.ReadExtramanPulseZ().ToString(); } else { csMsg.ShowWarning("机械手未运行到指定位置", false); } } else { csMsg.ShowWarning("脉冲值格式不正确", false); } Cursor = null; }
//指定储位加药 private int Add(string posCode, string dir, int num, string batch, bool qz, ref bool stopAdd) { int result = 0; string sql = "select pulsex,pulsez from pos_pulse where maccode='{0}' and poscode='{1}' and pulselr='{2}' and pulsetype='A'"; sql = string.Format(sql, Config.Soft.MacCode, posCode, dir); DataTable dt; csSql.ExecuteSelect(sql, Config.Soft.ConnString, out dt); if (dt.Rows.Count <= 0) { return(0); } float targetX = float.Parse(dt.Rows[0][0].ToString()); float targetZ = float.Parse(dt.Rows[0][1].ToString()); //PLC.ChangeAdd(1); //运行到加药位置 if (PLC_Tcp_CP.ExtramanAutoMoveToPulse(targetX, targetZ)) { DateTime timeBegin = DateTime.Now; Thread.Sleep(200); while (!PLC_Tcp_CP.ExtramanAutoMoveIsOK()) { if (DateTime.Now > timeBegin.AddSeconds(Config.Mac_C.WaitTime_Auto_Extraman)) { csMsg.ShowWarning("加药定位失败", false); return(0); } Thread.Sleep(200); } } else { csMsg.ShowWarning("加药定位指令发送失败", false); return(0); } float maxPulse = float.Parse(Config.Mac_C.Pulse_Plate_Max_Left); PLC_Tcp_AP.PlateType type = PLC_Tcp_AP.PlateType.Left; if (dir == "R") { type = PLC_Tcp_AP.PlateType.Right; maxPulse = float.Parse(Config.Mac_C.Pulse_Plate_Max_Right); } float nowPulse = PLC_Tcp_CP.ReadPlatePulse(type); //计数清零 PLC_Tcp_CP.ResetPlateRecord(type); int oldRecord = 0, newRecord = 0; stopAdd = false; //获取药盒厚度 float drugThickness = 10; sql = "select height from drug_infoannex where drugonlycode in(select drugonlycode from drug_pos where poscode='{0}')"; sql = string.Format(sql, posCode); string s; csSql.ExecuteScalar(sql, Config.Soft.ConnString, out s); if (!string.IsNullOrEmpty(s)) { drugThickness = float.Parse(s); } //上推脉冲 float upPulse = Convert.ToInt32(drugThickness * Config.Mac_C.OneMMPulse_Plate); //补偿脉冲值 //int bcPulse = 500; for (int i = 0; i < num;) { //有障碍物,暂停 //int z = PLC.PlateErrorZHA(); //while (z == 1 || z == 2) //{ // Thread.Sleep(200); // if (z == 1) // ThrowMsg("左侧加药手有障碍物"); // else if (z == 1) // ThrowMsg("右侧加药手有障碍物"); // z = PLC.PlateErrorZHA(); //} //加药+补偿 for (int j = 1; j <= (Config.Mac_C.Count_BC + 1); j++) { if (j == 1) { nowPulse += upPulse; } else { nowPulse += Config.Mac_C.OneMMPulse_Plate * (drugThickness * Config.Mac_C.Height_BC); } if (nowPulse > maxPulse) { nowPulse = maxPulse; } //上推 PLC_Tcp_CP.PlateAutoMoveToPulse(type, nowPulse); DateTime timeBegin = DateTime.Now; Thread.Sleep(100); while (!PLC_Tcp_CP.PlateAutoMoveToPulseIsOK(type)) { if (DateTime.Now > timeBegin.AddSeconds(Config.Mac_C.WaitTime_Auto_Plate)) { csMsg.ShowWarning("推板定位失败", false); break; } Thread.Sleep(100); } //上推完成,判断计数 if (DateTime.Now <= timeBegin.AddSeconds(Config.Mac_C.WaitTime_Auto_Plate)) { DateTime t1 = DateTime.Now; Thread.Sleep(200); while (true) { newRecord = PLC_Tcp_CP.ReadPlateRecord(type); if (newRecord > oldRecord) { break; } else if (DateTime.Now > t1.AddSeconds(2)) { break; } Thread.Sleep(200); } if (newRecord > oldRecord) { oldRecord = newRecord; if (dir == "L") { SetNumL(plate_Left.Num - newRecord); } else { SetNumR(plate_Right.Num - newRecord); } break; } else { if (j < (Config.Mac_C.Count_BC + 1)) { continue; } else { //故障 SetErrorVisibility(Visibility.Visible); stopAdd = true; } } } else { break; } } i = newRecord; if (nowPulse >= maxPulse) { break; } if (stopAdd) { break; } } result = newRecord; string drugBatch = ""; if (qz) { drugBatch = batch; } else { sql = "select * from drug_pos where (drugbatch<'{0}' or drugbatch is null) and maccode='{1}' and poscode='{2}' order by drugbatch"; sql = string.Format(sql, batch, Config.Soft.MacCode, posCode); csSql.ExecuteSelect(sql, Config.Soft.ConnString, out dt); if (dt.Rows.Count > 0) { drugBatch = dt.Rows[0]["drugbatch"].ToString().Trim(); } else { drugBatch = batch; } } //更新储位库存 s = "update drug_pos set drugnum=(drugnum+{0}),drugbatch='{1}' where maccode='{2}' and poscode='{3}';"; sql = string.Format(s, result, drugBatch, Config.Soft.MacCode, posCode); //增加加药明细 s = "insert into drug_import select '{0}',drugonlycode,'{1}','I','{2}',drugunit,'{3}',getdate(),'' from drug_pos where maccode='{0}' and poscode='{4}';"; sql += string.Format(s, Config.Soft.MacCode, batch, result, Config.Soft.UserCode, posCode); csSql.ExecuteSql(sql, Config.Soft.ConnString); return(result); }