在串口写入时需要显示新表单

时间:2012-10-16 15:30:13

标签: c# serial-port

当IDTECH卡片编写者正在等待滑动时,尝试显示“请刷卡”的新表格。

private void writecard(string track1) //string track2)
    {

        SerialPort sp1 = new SerialPort(encoderport, 9600, Parity.None, 8, StopBits.One);
        sp1.ReadTimeout = 10000;

        if (sp1.IsOpen == true)
        {
            sp1.Close();
        }


        sp1.Open();
        sp1.Write(new byte[] { 0x1B, 0x77 }, 0, 2);
        Thread.Sleep(100);
        sp1.Write(new byte[] { 0x1B, 0x73 }, 0, 2);
        Thread.Sleep(100);
        sp1.Write(new byte[] { 0x1B, 0x01 }, 0, 2);
        Thread.Sleep(100);
        sp1.Write(track1);                              // Track 1
        Thread.Sleep(100);


        sp1.Write(new byte[] { 0x3f, 0x1c }, 0, 2);
        swipeform swipepop = new swipeform();
        swipepop.ShowDialog();                        // **Problem**



        Thread.Sleep(100);

        try
        {
            swipepop.Close();                         // **Problem**
            int firstChar = sp1.ReadChar();
            Thread.Sleep(500);
            string data = String.Concat(Convert.ToChar(firstChar), sp1.ReadExisting());
            //textBox3.Text = String.Format(data);
            if (data != "")
            {
                char result = data[1];
                if (result == '0')
                    MessageBox.Show("Encoding Successful");

                else
                    MessageBox.Show("Encoding Failed - Please try again");
            }
        }
        catch (System.TimeoutException)
        {
            MessageBox.Show("Timed out.  Please try again.");
            sp1.Write(new byte[2] { 0x1b, 0x61 }, 0, 2);
        }
        sp1.Close();
    }

我遇到的问题是,一旦调用了writecard方法,弹出的swipeform就会弹出并且设备要你滑动。但是,一旦您滑动表单,就会停止并且不会关闭,并且在关闭滑动窗体之前该方法不会继续。

1 个答案:

答案 0 :(得分:1)

Form.ShowDialog()正在阻止。这意味着它不会在表单关闭之前返回。

其中一个解决方案是:检查swipepop表单中的串口。您可以通过表单的构造函数传递SerialPort对象。