0

我正在使用 C#、.Net 2.0 和 VS2008 处理我的项目。简而言之,我的应用程序通过 USB 与设备通信。而且我在处理来自设备的传入消息时遇到问题。

这是事件“我们从设备得到一些东西”的处理程序:

private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)

它从不调用同一类的方法(我使用部分类):

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 = "";
        }

如何使用事件处理程序解决此问题?

PS 当我通过表单的按钮和文本框发送/接收消息时它工作正常:

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;
            }
        }
4

0 回答 0