2017-01-16 19 views
1

我试图测量的往返时间,当我走出发送一个字节和发送另一个字节之前等待返回的字节。往返时间慢了我的申请,约15毫秒的持续,在数千个数据点。然而,当我刚刚发送的字节之前实现20毫秒的延迟,往返时间1毫秒,每20个左右样品17毫秒之间振荡。我在代码中做错了什么,导致数据传输效率低下并且变化很大?C#串行端口提高性能降低

EDIT .. 波特率是9600。未指定的其它值,它们是在默认值。其实我有一个具有发送和在单独的线程接收,但我得到同样的结果不同的代码。我没有在调试模式下运行。

这里是代码中的相关片段:

public void serserData() 
     { 

     int[] numarray = Enumerable.Range(1, 255).ToArray();//new int[] {70, 80, 90, 100, 60, 50, 40, 30 }; 

     byte[] recvbyte = new byte[1]; //1 byte of data coming in 
     for (int repeat = 0; repeat <= datapoints; repeat++) 
     { 

      int rnd1 = rndseed.Next(0, numarray.Length); 
      sentnum = numarray[rnd1]; 

      byte[] writebyte = new byte[] { BitConverter.GetBytes(numarray[rnd1])[0] }; 
      Array.Reverse(writebyte); 
      Thread.Sleep(sleeptime); 
      serialPortOut.Write(writebyte, 0, 1); //Send the byte 
                //stopwatch.Reset(); 



      sentelapsmil = mytimer.Duration * 1000; 


      while(serialPortIn.BytesToRead==0) 
      { 

      } 
      recvelapsmil = mytimer.Duration * 1000; 
      serialPortIn.Read(recvbyte, 0, 1); 



       int numback = BitConverter.ToInt16(new byte[] { recvbyte[0], 0x00 }, 0); 

       if (numback == sentnum) 
       { 
        correctbyte = 1; 
       } 
       else 
       { 
        correctbyte = 0; 
       } 

       double elapsmil = recvelapsmil - sentelapsmil; 
       sb.AppendLine(elapsmil + "\t " + correctbyte + "\n"); 

      } 
      } 

这里是mytimer,基于QueryPerformanceCounter的

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Runtime.InteropServices; 
using System.ComponentModel; 


namespace HighResTimer 
{ 
    public class Timing 
    { 
    [DllImport("Kernel32.dll")] 
    private static extern bool QueryPerformanceCounter(out ulong lpPerformanceCount); 

    [DllImport("Kernel32.dll")] 
    private static extern bool QueryPerformanceFrequency(out ulong lpFrequency); 

    public Timing() 
    { 
     if (QueryPerformanceFrequency(out freq) == false) 
     { 
      // high-performance counter not supported 

      //throw new Win32Exception(); 
     } 
     else 
     { 
      QueryPerformanceCounter(out startTime); 
     } 

    } 

    private ulong startTime, curTime; 
    private ulong freq; 

    private bool started = false; 

    public void Start() 
    { 
     // record start time 
     started = true; 
     QueryPerformanceCounter(out startTime); 
    } 

    public double Duration 
    {    
     get 
     { 
      if (started == false) return 0; 
      QueryPerformanceCounter(out curTime); 
      return (double)(curTime - startTime)/(double)freq; 
     } 
    } 

    public ulong GetStartCode() 
    { 
     return startTime; 
    } 
    public ulong GetFrequency() 
    { 
     return freq; 
    } 
    } 
} 
+0

什么是mytimer?它有多准确?你在调试中运行吗?为什么你的读代码和写代码在同一个线程上(理想的情况是它会在发送字节之前等待接收)。你使用什么波特率,停止位等? –

+0

这里有一些有趣的信息:https://social.msdn.microsoft.com/Forums/vstudio/en-US/e36193cd-a708-42b3-86b7-adff82b19e5e/how-does-serialport-handle-datareceived?forum=netfxbcl – PaulF

+0

我已经更新了我的问题,并回答了评论中的一些问题。 – nt387

回答

0

所以我认为,我想通了,真正的问题,-the USB转串口转换器与FTDI芯片。我换出来的一个多产的USB转串口,并且改变了往返时间以4-5毫秒一致。然后,我卸载并重新安装了FTDI USB串口驱动程序,并将时间一直缩短到4-5毫秒。