在重新启动应用程序之前,串行端口不刷新接收的数据

时间:2013-06-03 10:45:36

标签: c# serial-port

我上周写了一条消息,但想再次提出另一个更简单的问题来澄清。

我有一个串口控制台应用程序。有20个虚拟端口。我的应用程序成功运行但在重新启动应用程序之前不会更改接收的数据值。我不确定为什么会这样。

FIFO启用属性设置为true后出现此问题。 如何刷新串口?

由于 ---这是代码:

        static void serialportinstances()
    {
            for (int ix =1; ix < 20; ix++)
            {
                if (sportlar.ContainsValue("COM"+ix.ToString()) ==false)
                {
                    try
                    {
                        SerialPort ekle = new SerialPort("COM" + ix.ToString(), 1200, Parity.None, 8, StopBits.One);
                        //ekle.DtrEnable = true;
                        //ekle.RtsEnable = true;

                        try
                        {
                            ekle.NewLine = "\r";
                            ekle.Open();
                            ekle.DataReceived += new SerialDataReceivedEventHandler(datareceived);
                            Console.WriteLine(String.Format("Dinamik SP {0} başarıyla açıldı", ekle.PortName));
                        }
                        catch (Exception eu)
                        {
                            Console.WriteLine(String.Format("Dynamic SP {0} açılamadı!", ekle.PortName));
                        }
                        sportlar.Add(ekle, "COM" + ix.ToString());
                    }
                    catch (Exception ee)
                    {
                        Console.WriteLine("port could not be created");
                    }
                }
            }
    }

和datareceiving处理程序;

        static void datareceived(object sender, SerialDataReceivedEventArgs e)
    {
        lock (mylockobject)
        {
            SerialPort spn = (SerialPort)sender;
            try
            {
                string s = spn.ReadLine();
                GetWeightPWI(spn.PortName, s);
                Thread.Sleep(1);
            }
            catch (Exception ee)
            {
                Console.WriteLine("error : " + ee.ToString());
            }
        }
    }

并在此处解析收到的数据:

    private static void GetWeightPWI(string portadi, string alinan)
    {
       Console.WriteLine("PORT NAME : "+portadi+" and received data : "+alinan.toString());
    }

0 个答案:

没有答案