C# 同一类的方法中的事件处理程序
我正在使用C#、.NET2.0和VS2008进行我的项目。 简单地说,我的应用程序通过USB与设备通信。我在处理来自设备的传入消息时遇到问题 这是事件“我们从设备获得了一些东西”的处理程序: 它从未在同一类的方法中调用(我使用分部类): 如何使用事件处理程序解决此问题 另外,当我通过表单按钮和文本框发送/接收消息时,它工作正常:C# 同一类的方法中的事件处理程序,c#,C#,我正在使用C#、.NET2.0和VS2008进行我的项目。 简单地说,我的应用程序通过USB与设备通信。我在处理来自设备的传入消息时遇到问题 这是事件“我们从设备获得了一些东西”的处理程序: 它从未在同一类的方法中调用(我使用分部类): 如何使用事件处理程序解决此问题 另外,当我通过表单按钮和文本框发送/接收消息时,它工作正常: private void btn_send_Click(object sender, EventArgs e) { try
private void btn_send_Click(object sender, EventArgs e)
{
try
{
string text = this.tb_send.Text + " ";
text.Trim();
string[] arrText = text.Split(' ');
byte[] data = new byte[arrText.Length];
for (int i = 0; i < arrText.Length; i++)
{
if (arrText[i] != "")
{
int value = Int32.Parse(arrText[i], System.Globalization.NumberStyles.HexNumber);
data[i] = (byte)Convert.ToByte(value);
}
}
if (this.usb.SpecifiedDevice != null)
{
this.usb.SpecifiedDevice.SendData(data);
}
else
{
MessageBox.Show("Device not found, plug it");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
{
if (InvokeRequired)
{
try
{
Invoke(new DataRecievedEventHandler(usb_OnDataRecieved), new object[] { sender, args });
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
else
{
string rec_data = "Data: ";
foreach (byte myData in args.data)
{
if (myData.ToString("X").Length == 1)
{
rec_data += "0";
}
rec_data += myData.ToString("X") + " ";
}
this.lb_read.Items.Insert(0, rec_data);
lastMessageFromUsb = rec_data;
newMessageArrived = true;
}
}
private void btn\u发送\u单击(对象发送者,事件参数e)
{
尝试
{
字符串text=this.tb_send.text+“”;
text.Trim();
string[]arrText=text.Split(“”);
字节[]数据=新字节[arrText.Length];
for(int i=0;i
您遗漏了最重要的信息:您如何设置要调用的事件?您是说当处理程序在classCodeIgnoto表单之外写入时,usb_OnDataReceived未被调用,否,这是一个完整的部分class@spyder:你能不能再清楚一点,它什么时候起作用,什么时候不起作用。请把整个班级的代码都写出来。
private void btn_indication_start_Click(object sender, EventArgs e)
{
currentTest = "indication";
this.ControlBox = false;
bool isTestPassed = false;
int timeout = 1000;
sendMessageToDevice("A9 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"); //device should respond to this message
System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
while (true)
{
if (stopwatch.ElapsedMilliseconds >= timeout)
break;
Thread.Sleep(1);
}
if (!newMessageArrived) //this bool variable always false :(
{
MessageBox.Show("Timeout!"); //usb_OnDataRecieved called only in this block! And newMessageArrived become true
//and when we remove MessageBox, it don't called ever!
this.ControlBox = true;
currentTest = "";
return;
}
//do something else with device...
this.ControlBox = true;
currentTest = "";
}
private void btn_send_Click(object sender, EventArgs e)
{
try
{
string text = this.tb_send.Text + " ";
text.Trim();
string[] arrText = text.Split(' ');
byte[] data = new byte[arrText.Length];
for (int i = 0; i < arrText.Length; i++)
{
if (arrText[i] != "")
{
int value = Int32.Parse(arrText[i], System.Globalization.NumberStyles.HexNumber);
data[i] = (byte)Convert.ToByte(value);
}
}
if (this.usb.SpecifiedDevice != null)
{
this.usb.SpecifiedDevice.SendData(data);
}
else
{
MessageBox.Show("Device not found, plug it");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
{
if (InvokeRequired)
{
try
{
Invoke(new DataRecievedEventHandler(usb_OnDataRecieved), new object[] { sender, args });
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
else
{
string rec_data = "Data: ";
foreach (byte myData in args.data)
{
if (myData.ToString("X").Length == 1)
{
rec_data += "0";
}
rec_data += myData.ToString("X") + " ";
}
this.lb_read.Items.Insert(0, rec_data);
lastMessageFromUsb = rec_data;
newMessageArrived = true;
}
}