/// <summary> /// 比例阀2设定 /// </summary> /// <param name="val"></param> public bool ProportionalVal2Set(float val) { try { PLC_NK1 NK1 = new PLC_NK1(); if (NK1.PLCInitil(5, MainForm.PLC_IP)) { int proportionalVal2 = (int)(val * 4096 / 10); ushort[] buf_Write_DO = new ushort[1]; buf_Write_DO[0] = Convert.ToUInt16(proportionalVal2); string strRegAdr_DO = "37011"; NK1.SetRegAdr('R', strRegAdr_DO); short shRet = ECOMModbusTCP.emtWrite_PLC(ref NK1.PParam, buf_Write_DO, 1); if (shRet == 0) { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } } ECOMModbusTCP.emtClose(ref NK1.PParam); return(false); } catch (Exception ex) { AlarmRecord(true, "其他", ex.Message); return(false); } }
private bool GetPLCInStatus_I20ToI27(string position) { try { PLC_NK1 NK1 = new PLC_NK1(); if (NK1.PLCInitil(3, MainForm.PLC_IP)) { //输入信号 ushort[] buf_Read_DI = new ushort[1024]; string strRegAdr_DI = "40401";//I20-I27 NK1.SetRegAdr('R', strRegAdr_DI); short shRet = ECOMModbusTCP.emtRead_PLC(ref NK1.PParam, buf_Read_DI, 4); if (shRet == 0) { switch (position) { case "流水线前段送板": if ((buf_Read_DI[0] & (1 << 0)) != 0) //流水线前段送板 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "流水线后段要板": if ((buf_Read_DI[0] & (1 << 1)) != 0) //流水线后段要板 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; default: ECOMModbusTCP.emtClose(ref NK1.PParam); return(false); } } } ECOMModbusTCP.emtClose(ref NK1.PParam); return(false); } catch (Exception ex) { AlarmRecord(true, "其他", ex.Message); return(false); } }
private void backgroundWorker_CheckPLC_DoWork(object sender, DoWorkEventArgs e) { try { while (true) { if (this.Disposing) { break; } try { //输出信号 PLC_NK1 NK1_UpdateTabPLC_IO = new PLC_NK1(); if (NK1_UpdateTabPLC_IO.PLCInitil(8, MainForm.PLC_IP)) { ushort[] buf_Read_DO = new ushort[1024]; string strRegAdr_DO = "40500"; NK1_UpdateTabPLC_IO.SetRegAdr('R', strRegAdr_DO); short shRet = ECOMModbusTCP.emtRead_PLC(ref NK1_UpdateTabPLC_IO.PParam, buf_Read_DO, 8); if (shRet == 0) { if ((buf_Read_DO[0] & (1 << 0)) != 0) // Q0 { } else { } if ((buf_Read_DO[0] & (1 << 1)) != 0) // Q1 { } else { } if ((buf_Read_DO[0] & (1 << 2)) != 0) // Q2 { } else { } if ((buf_Read_DO[0] & (1 << 3)) != 0) // Q3 { } else { } if ((buf_Read_DO[0] & (1 << 4)) != 0) // Q4 { } else { } if ((buf_Read_DO[0] & (1 << 5)) != 0) // Q5 { } else { } if ((buf_Read_DO[0] & (1 << 6)) != 0) // 夹紧气缸Q6 { } else { } if ((buf_Read_DO[0] & (1 << 7)) != 0) // 探针下压气缸Q7 { } else { } if ((buf_Read_DO[0] & (1 << 8)) != 0) // Q10 { } else { } if ((buf_Read_DO[0] & (1 << 9)) != 0) // Q11 { } else { } if ((buf_Read_DO[0] & (1 << 10)) != 0) // Q12 正转 { led_Motor_Positive.GridentColor = Color.Green; } else { led_Motor_Positive.GridentColor = Color.Gray; } if ((buf_Read_DO[0] & (1 << 11)) != 0) // Q13 反转 { led_Motor_Reverse.GridentColor = Color.Green; } else { led_Motor_Reverse.GridentColor = Color.Gray; } if ((buf_Read_DO[0] & (1 << 12)) != 0) // Q14 { } else { } if ((buf_Read_DO[0] & (1 << 13)) != 0) // Q15 { } else { } if ((buf_Read_DO[0] & (1 << 14)) != 0) // Q16 { } else { } if ((buf_Read_DO[0] & (1 << 15)) != 0) // Q17 { } else { } } } ECOMModbusTCP.emtClose(ref NK1_UpdateTabPLC_IO.PParam); //输入信号 PLC_NK1 NK1_UpdateTabPLC_IO3 = new PLC_NK1(); if (NK1_UpdateTabPLC_IO3.PLCInitil(20, MainForm.PLC_IP)) { ushort[] buf_Read_DI = new ushort[1024]; string strRegAdr_DI = "40400";//I0-I17 NK1_UpdateTabPLC_IO3.SetRegAdr('R', strRegAdr_DI); short shRet1 = ECOMModbusTCP.emtRead_PLC(ref NK1_UpdateTabPLC_IO3.PParam, buf_Read_DI, 10); if (shRet1 == 0) { if ((buf_Read_DI[0] & (1 << 0)) != 0) //I0 { } else { } if ((buf_Read_DI[0] & (1 << 1)) != 0) //I1 { } else { } if ((buf_Read_DI[0] & (1 << 2)) != 0) //I2 { } else { } if ((buf_Read_DI[0] & (1 << 3)) != 0) //I3 { } else { } if ((buf_Read_DI[0] & (1 << 4)) != 0) //I4 { } else { } if ((buf_Read_DI[0] & (1 << 5)) != 0) //I5 { } else { } if ((buf_Read_DI[0] & (1 << 6)) != 0) //I6 { } else { } if ((buf_Read_DI[0] & (1 << 7)) != 0) //I7 { } else { } if ((buf_Read_DI[0] & (1 << 8)) != 0) //开始按钮灯I10 { } else { } if ((buf_Read_DI[0] & (1 << 9)) != 0) //停止按钮灯I11 { } else { } if ((buf_Read_DI[0] & (1 << 10)) != 0) //I12 { } else { } if ((buf_Read_DI[0] & (1 << 11)) != 0) //I13 { } else { } if ((buf_Read_DI[0] & (1 << 12)) != 0) //夹紧气缸前位I14 { } else { } if ((buf_Read_DI[0] & (1 << 13)) != 0) //夹紧气缸后位I15 { } else { } if ((buf_Read_DI[0] & (1 << 14)) != 0) //探针气缸上位I16 { } else { } if ((buf_Read_DI[0] & (1 << 15)) != 0) //探针气缸下位I17 { } else { } } } ECOMModbusTCP.emtClose(ref NK1_UpdateTabPLC_IO3.PParam); PLC_NK1 NK1_UpdateTabPLC_IO2 = new PLC_NK1(); if (NK1_UpdateTabPLC_IO2.PLCInitil(9, MainForm.PLC_IP)) { ushort[] buf_Read_DI2 = new ushort[1024]; string strRegAdr_DI2 = "40401";//I20-I27 NK1_UpdateTabPLC_IO2.SetRegAdr('R', strRegAdr_DI2); short shRet2 = ECOMModbusTCP.emtRead_PLC(ref NK1_UpdateTabPLC_IO2.PParam, buf_Read_DI2, 4); if (shRet2 == 0) { if ((buf_Read_DI2[0] & (1 << 0)) != 0) //I20 { } else { } if ((buf_Read_DI2[0] & (1 << 1)) != 0) //I21 { } else { } if ((buf_Read_DI2[0] & (1 << 2)) != 0) //I22 { } else { } if ((buf_Read_DI2[0] & (1 << 3)) != 0) //I23 { } else { } if ((buf_Read_DI2[0] & (1 << 4)) != 0) //I24 { } else { } if ((buf_Read_DI2[0] & (1 << 5)) != 0) //I25 { } else { } } } ECOMModbusTCP.emtClose(ref NK1_UpdateTabPLC_IO2.PParam); } catch (Exception) { } Thread.Sleep(2000); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private bool GetPLCInStatus_I0ToI17(string position) { try { PLC_NK1 NK1 = new PLC_NK1(); if (NK1.PLCInitil(2, MainForm.PLC_IP)) { //输入信号 ushort[] buf_Read_DI = new ushort[1024]; string strRegAdr_DI = "40400";//I0-I17 NK1.SetRegAdr('R', strRegAdr_DI); short shRet = ECOMModbusTCP.emtRead_PLC(ref NK1.PParam, buf_Read_DI, 4); if (shRet == 0) { switch (position) { case "流水线前位": if ((buf_Read_DI[0] & (1 << 0)) != 0) //流水线前位I0 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "流水线中位": if ((buf_Read_DI[0] & (1 << 1)) != 0) //流水线中位I1 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "流水线后位": if ((buf_Read_DI[0] & (1 << 2)) != 0) //流水线后位I2 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "阻挡气缸后位": if ((buf_Read_DI[0] & (1 << 3)) != 0) //阻挡气缸后位I3 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "阻挡气缸前位": if ((buf_Read_DI[0] & (1 << 4)) != 0) //阻挡气缸前位I4 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "夹板气缸下位": if ((buf_Read_DI[0] & (1 << 5)) != 0) //夹板气缸下位I5 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "夹板气缸上位": if ((buf_Read_DI[0] & (1 << 6)) != 0) //夹板气缸上位I6 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "浸泡气缸前位": if ((buf_Read_DI[0] & (1 << 12)) == 0) //浸泡气缸前位I14 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; case "浸泡气缸后位": if ((buf_Read_DI[0] & (1 << 13)) == 0) //浸泡气缸后位I15 { ECOMModbusTCP.emtClose(ref NK1.PParam); return(true); } break; default: ECOMModbusTCP.emtClose(ref NK1.PParam); return(false); } } } ECOMModbusTCP.emtClose(ref NK1.PParam); return(false); } catch (Exception ex) { AlarmRecord(true, "其他", ex.Message); return(false); } }
/// <summary> /// Loading界面呈现后事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Loading_Shown(object sender, EventArgs e) { //先检测运动板卡连接及初始化,再检测PLC连接及初始化 try { this.richTextBox_Load.AppendText("欢迎使用TRD-KEM试验器" + "\n"); this.progressBar_Load.Value = 0; this.progressBar_Load.Minimum = 0; this.progressBar_Load.Maximum = 100; //开始检测数据库连接 this.richTextBox_Load.AppendText(DateTime.Now + " 数据库连接..." + "\n"); ChangeProgressBar(2); if (CheackDBLink()) { this.richTextBox_Load.AppendText(DateTime.Now + " 数据库连接成功。" + "\n"); //以上数据库检测通过,以下开始PLC检测 this.richTextBox_Load.AppendText(DateTime.Now + " PLC连接..." + "\n"); ChangeProgressBar(1.5); NK1.PLCInitil(1, MainForm.PLC_IP); if (PLC_NK1.ShRet != 0) { switch (PLC_NK1.ShRet) { case 1001: this.richTextBox_Load.AppendText(DateTime.Now + " 1001,通信时间溢出!" + "\n"); break; case 1002: this.richTextBox_Load.AppendText(DateTime.Now + " 1002,PLC寄存器类型错误!" + "\n"); break; case 1003: this.richTextBox_Load.AppendText(DateTime.Now + " 1003, PLC寄存器地址错误!" + "\n"); break; case 1009: this.richTextBox_Load.AppendText(DateTime.Now + " 1009, 从PLC接收到错误的功能码!" + "\n"); break; case 1011: this.richTextBox_Load.AppendText(DateTime.Now + " 1011, 通信内部参数错误!" + "\n"); break; case 1014: this.richTextBox_Load.AppendText(DateTime.Now + " 1014, 通信内部传送ID错误!" + "\n"); break; case 1016: this.richTextBox_Load.AppendText(DateTime.Now + " 1016, 通信数据长度错误!" + "\n"); break; case -1: this.richTextBox_Load.AppendText(DateTime.Now + " PLC连接异常,请联系管理员!" + "\n"); break; } this.richTextBox_Load.ScrollToCaret(); MessageBox.Show("关闭程序"); this.Close(); } else { this.richTextBox_Load.AppendText(DateTime.Now + " PLC连接成功。" + "\n"); ChangeProgressBar(1.5); this.DialogResult = DialogResult.OK; this.Close(); } } else { this.richTextBox_Load.AppendText(DateTime.Now + " 数据库连接失败!" + "\n"); this.richTextBox_Load.ScrollToCaret(); MessageBox.Show("关闭程序"); this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); this.Close(); } }