//Button need DataRecieved event handler
private void LoggedDataBtn_Click(object sender, EventArgs e)
{
//When LoggDataBtn is pressed it Send the command LogToShow to device
string LoggedDataTOshow = "?R\r\n";
string commForMeter = string.Format(LoggedDataTOshow);
try
{
if (serialPortForApp.IsOpen)
{
Thread.Sleep(2000);
serialPortForApp.Write(LoggedDataTOshow);
}
}
catch (Exception)
{
ShowDataInScreenTxtb.Text = "TimeOUT Exception: Error for requested command";
}
}
//button does not need DataRecieved handler to display but create conflict
//for those that need it
private void GLPBtn_Click(object sender, EventArgs e)
{
//send command to instrument
string GLPCommand = "?G\r\n";
string GLPToShow = String.Format(GLPCommand);
try
{
if (serialPortForApp.IsOpen)
{
Thread.Sleep(2000);
//write command to port
serialPortForApp.Write(GLPToShow);
}
}
catch
{
//MessageBox error if data reading is not posible
ShowDataInScreenTxtb.Text = "TimeOUT Exception";
}
while (glpShowInMainForm.DataToSetandGet != "ENDS\r")
{
serialPortForApp.Write("\r");
DataToSetandGet = serialPortForApp.ReadExisting();
ShowDataInScreenTxtb.AppendText(DataToSetandGet.Replace("\r", "\r\n"));
}
}
//如何创建开关盒或定义不同的按钮值以读取和显示。一些按钮与 datarecived 事件一起工作,而其他按钮不需要它,但会为那些需要在 datarecieved 事件处理程序(如我的 LoggedDataBtn_Click )中读取退出的按钮产生冲突,其他按钮需要将 ReadExisted 放入 button_click 方法中,而不是像我的 GLPBtn_Click 那样使用 datarecieved 事件.
相关分类