串口连续数据读取

问题描述 投票:2回答:1

我已经使用WPF进行了应用,我必须实现串行通信。在下面,我有DataReceived函数。

主要问题是该函数仅在某些时候触发,有人对WPF中的DataReceived触发有更好的解决方案吗?

下面是与ComPort的连接连接

  public static void connnect(string recPort)
        {
            System.Windows.Threading.DispatcherTimer MyTimer = null;
            bool error = false;
                String serialpname = recPort;
                ComPort = new SerialPort();
                if (ComPort.IsOpen == false)
                {
                ComPort.PortName = serialpname;
                ComPort.BaudRate = 38400;  
                ComPort.Parity = Parity.None; 
                ComPort.DataBits = 8;
                ComPort.StopBits = StopBits.One;
                ComPort.DataReceived += new SerialDataReceivedEventHandler(OnSerialDataReceived);
                System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                dispatcherTimer.Tick += dispatcherTimer_Tick;
                dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
                try
                {
                    if (ComPort.IsOpen == false)
                    {
                        //Open Port
                        ComPort.Open();
                        dispatcherTimer.Start();
                    }
                }              
                catch (System.IO.IOException) { error = true; }              
            }
        }

下面是用于端口的计时器

  private static void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                ComPort.Write(eot + "11" + STX + "2170" + ENQ); // get pos 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

数据接收事件


public static void OnSerialDataReceived(object sender,
        SerialDataReceivedEventArgs args)
{           
    bool isString = true;
    try
    {
      Thread.Sleep(100);// this solves the problem
        byte[] readBuffer = new byte[ComPort.ReadBufferSize];

        int readLen = ComPort.Read(readBuffer, 0, readBuffer.Length);

        string readStr = string.Empty;
        if (isString)
        {
            readStr = Encoding.Default.GetString(readBuffer, 0, readLen);
            if(readStr.Length > 10)
            {
                if (readStr.StartsWith("\u0002"))
                {
                    Position = (Mid(readStr, 2, 5));
                    Messenger.Default.Send("NewPos");
                }
                else
                {
                    Position = (Mid(readStr, 31, 4));
                    Messenger.Default.Send("NewPos");
                }
              }
        }
        else
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < readLen; i++)
            {
                stringBuilder.Append(readBuffer[i].ToString("X2") + " ");
            }
            readStr = stringBuilder.ToString();
        }
    }
    catch (Exception ex)
    {
        Trace.TraceError(ex.Message);
        Console.WriteLine(ex);
    }
}
c# wpf serial-port communication
1个回答
0
投票

问题已解决,我添加了一个Thread.Sleep(100);在数据接收功能中。缓冲区未填充传入的数据,这似乎是原因。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.