private void btnEnter_Click(object sender, EventArgs e) { OleDbConnection con = new OleDbConnection(strAddress()); con.Open(); OleDbCommand cmd = new OleDbCommand("Select * From 用户名和密码", con); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { user_name = reader[1].ToString(); password = reader[2].ToString(); if (cobUserName.Text == user_name) { if (txtPassword.Text == password) { if (MoniIntFaceForm == null || MoniIntFaceForm.IsDisposed) { MoniIntFaceForm = new MonitoringInterface(); } MoniIntFaceForm.Owner = this; MoniIntFaceForm.Show(); this.Hide(); } } } }
public void setSendData() { MonitoringInterface frmMonitoringInterface = (MonitoringInterface)this.Owner; // 参数设置查空,主机号,从机号,电流,时间 if (txtMasterNum.Text == "") { MessageBox.Show("请输入主机号,有效值范围为0~20!");// 0为所有主机 txtMasterNum.Focus(); return; } if (txtSlaveNum.Text == "") { MessageBox.Show("请输入从机号,有效值范围为0~6!");// 0为所有从机 txtSlaveNum.Focus(); return; } if (txtCurrent.Text == "") { MessageBox.Show("请输入电流值,有效值范围为100~5000!"); txtCurrent.Focus(); return; } if (txtTime.Text == "") { MessageBox.Show("请输入时间值,有效值范围为1~999!"); txtTime.Focus(); return; } // 获得四个参数值 int hostNum = Convert.ToInt32(txtMasterNum.Text); int slaveNum = Convert.ToInt32(txtSlaveNum.Text); int current = Convert.ToInt32(txtCurrent.Text); int time = Convert.ToInt32(txtTime.Text); UInt16 uwCrcResult; if (hostNum > 20 || hostNum < 0) // 参数检查部分 { MessageBox.Show("主机号输入有误,请重新输入!"); txtMasterNum.Focus(); txtMasterNum.SelectAll();//全部选中,以便可以更正输入 return; } if (slaveNum > 6 || slaveNum < 0) { MessageBox.Show("从机号输入有误,请重新输入!"); txtSlaveNum.Focus(); txtSlaveNum.SelectAll();//全部选中,以便可以更正输入 return; } if (current > 5000 || current < 100) // 电流单位是mA { MessageBox.Show("电流值输入有误,请重新输入!"); txtCurrent.Focus(); txtCurrent.SelectAll();//全部选中,以便可以更正输入 return; } if (time > 999 || time < 1) // 时间单位是min { MessageBox.Show("时间输入有误,请重新输入!"); txtTime.Focus(); txtTime.SelectAll();//全部选中,以便可以更正输入 return; } for (int i = 0; i < 14; i++) // 数组初始化 { bSetCmd[i] = 0x00; } bSetCmd[0] = 0xaa; // 起始字符高位 bSetCmd[1] = 0x55; // 起始字符低位 bSetCmd[2] = 0x0c; // 数据长度(以下字符总长度,包括本身) bSetCmd[3] = 0xff; // 源地址 bSetCmd[4] = (byte)hostNum; // 主机号 bSetCmd[5] = 0x07; // 功能码-开机并设置数据 bSetCmd[6] = (byte)(current >> 8); // 设置电流高位 bSetCmd[7] = (byte)current; // 设置电流低位 bSetCmd[8] = (byte)(time >> 8); // 设置时间高位 bSetCmd[9] = (byte)time; // 设置时间低位 frmMonitoringInterface.CRC_16(bSetCmd, 10, out uwCrcResult); bSetCmd[10] = (byte)(uwCrcResult >> 8); // 校验码高位 bSetCmd[11] = (byte)uwCrcResult; // 校验码低位 if (true == frmMonitoringInterface.serialPort1.IsOpen) { frmMonitoringInterface.serialPort1.Write(bSetCmd, 0, 12); } else { MessageBox.Show("串口未打开,请打开串口"); } }