private void btnPLCRun_Click(object sender, EventArgs e) { //plc地址不是 02,验证失败 PPIReadWritePara para = new PPIReadWritePara(); para.PlcAddress = int.Parse(txtPLC.Text); if (PPIHelper.PLCRun(para)) { btnStop.Enabled = true; btnPLCRun.Enabled = false; } }
private void btnWrite_Click(object sender, EventArgs e) { if (!PPIHelper.serialPort1.IsOpen) { MessageBox.Show("请设置串口参数后打开", "警告"); return; } int wValue = 0; if (txtWriteValue.Text.Length == 0) { MessageBox.Show("请输数值"); } else { //if ((Enums.StorageType)Enum.Parse(typeof(Enums.StorageType), comStore.Text)==Enums.StorageType.C|| (Enums.StorageType)Enum.Parse(typeof(Enums.StorageType), comStore.Text)==Enums.StorageType.T) //{ // MessageBox.Show("T,C寄存器等不能用写命令写入"); // return; //} PPIReadWritePara para = new PPIReadWritePara(); bool flag = false; para.ByteAddress = int.Parse(txtAddress.Text); para.Bitnumber = Int32.Parse(txtBit.Text); para.StorageType = (Enums.StorageType) Enum.Parse(typeof(Enums.StorageType), comStore.Text); para.PlcAddress = int.Parse(txtPLC.Text); if (int.TryParse(txtWriteValue.Text, out wValue)) { para.WriteValue = int.Parse(txtWriteValue.Text); if ((Enums.StorageType)Enum.Parse(typeof(Enums.StorageType), comStore.Text) == Enums.StorageType.T) { if (PPIHelper.TwriteDWord(para)) { txtSendCmd.Text = (PPIHelper.sendCmd); flag = true; } } else if ((Enums.StorageType)Enum.Parse(typeof(Enums.StorageType), comStore.Text) == Enums.StorageType.C) { if (PPIHelper.CWriteWord(para)) { txtSendCmd.Text = PPIHelper.sendCmd; flag = true; } } else { #region switch switch (comWrite.Text) { case "Bit": if (PPIHelper.WriteBit(para)) { txtSendCmd.Text = (PPIHelper.sendCmd); flag = true; } break; case "Byte": if (PPIHelper.Writebyte(para)) { txtSendCmd.Text = PPIHelper.sendCmd; flag = true; } break; case "Word": if (PPIHelper.WriteWord(para)) { txtSendCmd.Text = (PPIHelper.sendCmd); flag = true; } break; case "DWord": if (PPIHelper.WriteDWord(para)) { txtSendCmd.Text = PPIHelper.sendCmd; flag = true; } break; default: break; } #endregion } if (flag) { txtReceive.Text = PPIHelper.receiveByte; } else { txtReceive.Text = "写入失败"; } } else { MessageBox.Show("请输入正确数值"); } } }
private void btnRead_Click(object sender, EventArgs e) { if (!PPIHelper.serialPort1.IsOpen) { MessageBox.Show("请设置串口参数后打开", "警告"); return; } PPIReadWritePara para = new PPIReadWritePara(); PPIReadWritePara readResult = new PPIReadWritePara(); para.ByteAddress = int.Parse(txtAddress.Text); para.Bitnumber = Int32.Parse(txtBit.Text); para.StorageType = (Enums.StorageType) Enum.Parse(typeof(Enums.StorageType), comStore.Text); para.PlcAddress = int.Parse(txtPLC.Text); if (para.StorageType == Enums.StorageType.T) { readResult = PPIHelper.TReadDword(para); txtSendCmd.Text = (PPIHelper.sendCmd); } else { #region switch switch (comRead.Text) { case "Bit": readResult = PPIHelper.Readbit(para); txtSendCmd.Text = (PPIHelper.sendCmd); break; case "Byte": readResult = PPIHelper.Readbytes(para); txtSendCmd.Text = (PPIHelper.sendCmd); break; case "Word": readResult = PPIHelper.ReadWords(para); txtSendCmd.Text = (PPIHelper.sendCmd); break; case "DWord": readResult = PPIHelper.ReadDWord(para); txtSendCmd.Text = (PPIHelper.sendCmd); break; default: break; } #endregion } if (readResult.IsSuceess) { txtReceive.Text = (PPIHelper.receiveByte); txtValue.Text = ByteToString(readResult.ReadValue); } else { txtReceive.Text = "读取失败"; } }