按钮发送命令 SerialPort1_DataReceived (EventArgs e) 发送和接收响应,与那些不需要它的人产生冲突
Buttons sent commands SerialPort1_DataReceived (EventArgs e)to send and receive responds,create conflict with those that don need it
//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"));
}
}
//event handler
private void SerialPortInApp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
// SerialPort1_DataReceived handles to recieve data from device connected to Port
Thread.Sleep(500);
DataToSetandGet = serialPortForApp.ReadExisting();
// Invokes the delegate on the UI thread, and sends the data that was received to the invoked method.
// ---- The "SerialPortInApp_DataReceived" method will be executed on the UI thread which allows populating of the textbox.
if (this.InvokeRequired)
{
Invoke(new MethodInvoker(delegate {
ShowDataInScreenTxtb.AppendText(DataToSetandGet.Replace("\r", "\r\n"));
}));
}
//The requested command is responded by the invoke method sending back the answer requested to display into the Textbox( DisplayDataTxB)
ShowDataInScreenTxtb.AppendText(DataToSetandGet.Replace("\r", "\r\n"));
//////display message(DataReadingStatusLbl) everytime data is retreiving from through the port
// DataReadingStatusLbl.Text = "Retrieving Data from TPS Instrument -->>";
}
catch (Exception)
{
ShowDataInScreenTxtb.Text = "TimeOUT Exception: Error Port ";
}
}
private void OpenPortBtn_Click(object sender, EventArgs e)
{
//Connect Port Button allows to connect the current device base on Port and Raud settings
try
{
if (PortIndexCbx.Text == "" || BaudRateIndexCbx.Text == "")
{
ShowDataInScreenTxtb.Text = "Set Up your Port Settings";
//Error message to user to set up port connection if empty selection
}
else
{
//Get Serial Port index settings to display for users
serialPortForApp.PortName = PortIndexCbx.Text;
serialPortForApp.BaudRate = Convert.ToInt32(BaudRateIndexCbx.Text);
serialPortForApp.DataBits = 8;
serialPortForApp.Parity = Parity.None;
serialPortForApp.StopBits = StopBits.One;
serialPortForApp.Handshake = Handshake.XOnXOff;
//Open serial Port to allow communication between PC and device
//DataReceived Implemented to read from Port sending and recieving the
buttons command requested
//To receive data, we will need to create an EventHandler for the
// "SerialDataReceivedEventHandler"
//I delete this and GLPBtn_Click responds and work without it, but I need
//both buttons to work because LoggedDataBtn_Click need this event to
//display responds
serialPortForApp.DataReceived += new
SerialDataReceivedEventHandler(SerialPortInApp_DataReceived);
serialPortForApp.ReadTimeout = 500;
serialPortForApp.WriteTimeout = 500;
//serialPortForApp.RtsEnable = true;
//serialPortForApp.DtrEnable = true;
//Once Serial port and baud rate setting are set,
//connection is ready to use
//Open serial port for communication between device and PC
serialPortForApp.Open();
//Connect and close connection buttons Behaviour
progressBarPortAccess.Value = 100;
OpenPortBtn.Enabled = false;
ClosePortBtn.Enabled = true;
ShowDataInScreenTxtb.Enabled = true;
ShowDataInScreenTxtb.Text = "";
DataReadingStatusLbl.Text = "Port is Connected";
}
}
catch (UnauthorizedAccessException)
{
ShowDataInScreenTxtb.Text = "Unauthorized Port Access";
}
}
//如何创建switch case或者定义按钮值不同
阅读和展示。
一些按钮与 datarecived 事件一起工作,而另一些按钮不需要它,但会为那些需要在 datarecieved 事件处理程序中进行 ReadExiting 的按钮创建冲突,例如我的 LoggedDataBtn_Click
其他按钮需要 ReadExisted 到 button_click 方法中,而不是像我的 GLPBtn_Click.
那样使用 datarecieved 事件
请问如何解决这个冲突谢谢
为了正确执行此操作,您确实需要一个状态机并在您的数据接收事件中委托(回调)运行 根据您所处的状态正确的函数。在您的情况下,按gui 上的按钮可以将其置于某种状态。我在这里写得很好:
//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"));
}
}
//event handler
private void SerialPortInApp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
// SerialPort1_DataReceived handles to recieve data from device connected to Port
Thread.Sleep(500);
DataToSetandGet = serialPortForApp.ReadExisting();
// Invokes the delegate on the UI thread, and sends the data that was received to the invoked method.
// ---- The "SerialPortInApp_DataReceived" method will be executed on the UI thread which allows populating of the textbox.
if (this.InvokeRequired)
{
Invoke(new MethodInvoker(delegate {
ShowDataInScreenTxtb.AppendText(DataToSetandGet.Replace("\r", "\r\n"));
}));
}
//The requested command is responded by the invoke method sending back the answer requested to display into the Textbox( DisplayDataTxB)
ShowDataInScreenTxtb.AppendText(DataToSetandGet.Replace("\r", "\r\n"));
//////display message(DataReadingStatusLbl) everytime data is retreiving from through the port
// DataReadingStatusLbl.Text = "Retrieving Data from TPS Instrument -->>";
}
catch (Exception)
{
ShowDataInScreenTxtb.Text = "TimeOUT Exception: Error Port ";
}
}
private void OpenPortBtn_Click(object sender, EventArgs e)
{
//Connect Port Button allows to connect the current device base on Port and Raud settings
try
{
if (PortIndexCbx.Text == "" || BaudRateIndexCbx.Text == "")
{
ShowDataInScreenTxtb.Text = "Set Up your Port Settings";
//Error message to user to set up port connection if empty selection
}
else
{
//Get Serial Port index settings to display for users
serialPortForApp.PortName = PortIndexCbx.Text;
serialPortForApp.BaudRate = Convert.ToInt32(BaudRateIndexCbx.Text);
serialPortForApp.DataBits = 8;
serialPortForApp.Parity = Parity.None;
serialPortForApp.StopBits = StopBits.One;
serialPortForApp.Handshake = Handshake.XOnXOff;
//Open serial Port to allow communication between PC and device
//DataReceived Implemented to read from Port sending and recieving the
buttons command requested
//To receive data, we will need to create an EventHandler for the
// "SerialDataReceivedEventHandler"
//I delete this and GLPBtn_Click responds and work without it, but I need
//both buttons to work because LoggedDataBtn_Click need this event to
//display responds
serialPortForApp.DataReceived += new
SerialDataReceivedEventHandler(SerialPortInApp_DataReceived);
serialPortForApp.ReadTimeout = 500;
serialPortForApp.WriteTimeout = 500;
//serialPortForApp.RtsEnable = true;
//serialPortForApp.DtrEnable = true;
//Once Serial port and baud rate setting are set,
//connection is ready to use
//Open serial port for communication between device and PC
serialPortForApp.Open();
//Connect and close connection buttons Behaviour
progressBarPortAccess.Value = 100;
OpenPortBtn.Enabled = false;
ClosePortBtn.Enabled = true;
ShowDataInScreenTxtb.Enabled = true;
ShowDataInScreenTxtb.Text = "";
DataReadingStatusLbl.Text = "Port is Connected";
}
}
catch (UnauthorizedAccessException)
{
ShowDataInScreenTxtb.Text = "Unauthorized Port Access";
}
}
//如何创建switch case或者定义按钮值不同 阅读和展示。 一些按钮与 datarecived 事件一起工作,而另一些按钮不需要它,但会为那些需要在 datarecieved 事件处理程序中进行 ReadExiting 的按钮创建冲突,例如我的 LoggedDataBtn_Click 其他按钮需要 ReadExisted 到 button_click 方法中,而不是像我的 GLPBtn_Click.
那样使用 datarecieved 事件请问如何解决这个冲突谢谢
为了正确执行此操作,您确实需要一个状态机并在您的数据接收事件中委托(回调)运行 根据您所处的状态正确的函数。在您的情况下,按gui 上的按钮可以将其置于某种状态。我在这里写得很好: