在C#中使用SerialPort的System.TimeoutException

时间:2013-07-09 05:54:23

标签: c# .net serial-port

我有一个系统,它将命令发送到串行端口列表,并在RichTextBox中显示返回值。 当我是10个串口时,程序运行良好,现在我正在使用大约60,它返回以下错误:

Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01:   jporttestot.exe
Problem Signature 02:   1.0.0.0
Problem Signature 03:   51db05c7
Problem Signature 04:   System
Problem Signature 05:   4.0.0.0
Problem Signature 06:   4ba1dff4
Problem Signature 07:   3140
Problem Signature 08:   b3
Problem Signature 09:   System.TimeoutException
OS Version: 6.1.7601.2.1.0.256.1
Locale ID:  1046
Additional Information 1:   0a9e
Additional Information 2:   0a9e372d3b4ad19135b953a78882e789
Additional Information 3:   0a9e
Additional Information 4:   0a9e372d3b4ad19135b953a78882e789
Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409
If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt

以下是我的代码:

 public partial class Form1 : Form
        {
            List<SerialPort> serialPort = new List<SerialPort>();


            private delegate void SetTextDeleg(string text);

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                int baudRate = Convert.ToInt32(txtBaudRate.Text);
                var portNames = SerialPort.GetPortNames();
                foreach (var port in portNames)
                {
                    SerialPort sp;
                    sp = new SerialPort(port, baudRate, Parity.None, 8, StopBits.One);
                    sp.Handshake = Handshake.None;
                    sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                    sp.ReadTimeout = 500;
                    sp.WriteTimeout = 500;

                    serialPort.Add(sp);
                    listPorts.Items.Add(port);
                }
            }

            private void listPorts_SelectedIndexChanged(object sender, EventArgs e)
            {

            }

            private void button1_Click_1(object sender, EventArgs e)
            {
                boxLogs.Text = "";
                String comando = txtComando.Text.Trim();

                foreach (var sp in serialPort)
                {
                    // Open port
                    try
                    {
                        if (!sp.IsOpen)
                            sp.Open();

                        //sp.Write("at\r\n");
                        sp.Write("at\r\n");
                    }
                    catch (Exception ex)
                    {
                        boxLogs.Text = "Erro ao abrir/escrever na porta serial :: " + ex.Message +"\n";
                    }
                }
            }

            void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                Thread.Sleep(500);
                SerialPort sp = (SerialPort)sender;
                string data = sp.ReadLine();
                data = sp.ReadLine();
                this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { sp.PortName + " - Retorno: " + data });
            }

            private void si_DataReceived(string data)
            {
                String retorno = data.Trim();
                boxLogs.Text += retorno + "\n";
            }

            private void txtComando_TextChanged(object sender, EventArgs e)
            {

            }

            private void txtBaudRate_TextChanged(object sender, EventArgs e)
            {

            }

        }

1 个答案:

答案 0 :(得分:0)

如果你有很多SerialPort对象,当你在它上面调用Write()时,你的端口可能仍然没有打开。系统需要一些时间来实际完成操作。在Open()之后尝试Thread.Sleep()一段时间,看看会发生什么(尽管你的UI会冻结)。也许是这样的?

 if (!sp.IsOpen)  
{
    sp.Open();
    Thread.Sleep(200);
    sp.Write("at\r\n");  
}

顺便问一下,你在哪里调用Close()? MSDN声明:

  

“任何应用程序的最佳实践是等待一些   在尝试调用Open之前调用Close方法之后的时间   方法,因为端口可能不会立即关闭。“