在Windows嵌入式应用程序中获取毫秒分辨率(周期时间)

本文关键字:分辨率 周期 时间 嵌入式 Windows 应用程序 获取 | 更新日期: 2023-09-27 18:12:01

我正在用c#编写一个应用程序,它可以读取CAN(控制局域网)消息并发送它们。我需要在10毫秒内完成。我使用的操作系统是Windows Embedded 7 Pro。

    public void ID0008Update10ms(DataTable supportPoints, int a)
    { 
        System.TimeSpan timer10ms = System.TimeSpan.FromMilliseconds(10); 
        intialiseCAN();
        while (a==1)
        {
            Stopwatch t = Stopwatch.StartNew();
            sendCAN();
            getCAN();
            ID0006NavComStatus(supportPoints);
            string state = Convert.ToString(gNavStatus);

            while (t.Elapsed < timer10ms) 
            { /*nothing*/}
        }
    }

问题是sendCAN()和reciveCAN()动态加载。dll文件

    public int sendCAN(ref can_msg msg, IntPtr pDll)
    {
        if (pDll == IntPtr.Zero)
        {
            MessageBox.Show("Loading Failed");
        }
        IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "CAN_Transmission");
        CAN_Transmission sendCAN = (CAN_Transmission)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(CAN_Transmission));
        int result = sendCAN( ref msg);
        return result;
    }

这使得周期变慢,我无法在10ms内发送消息。有人能提出更好的方法吗?请记住。

在Windows嵌入式应用程序中获取毫秒分辨率(周期时间)

应该尽可能地移到循环之外。如果它不是绝对必须在那里,那就移动它。沿着....

的行
private CAN_TransmissionMethod CAN_Transmission;
//Cache the delegate outside the loop.
private bool InitialiseCAN2(IntPtr pDll)
{
    if (pDll == IntPtr.Zero)
    {
        Log("Loading Failed");
        return false;
    }
    IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "CAN_Transmission");
    CAN_Transmission = (CAN_TransmissionMethod)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(CAN_Transmission));
    return true;     
}
public int sendCAN(ref can_msg msg)
{
    if (CAN_Transmission == null)
        return -1;//Can't send, no delegate, Log, Fail, Explode... make a cup of tea.
    int result = CAN_Transmission( ref msg);
    return result;
}
public void ID0008Update10ms(DataTable supportPoints, int a)
{ 
    System.TimeSpan timer10ms = System.TimeSpan.FromMilliseconds(10); 
    intialiseCAN();
    initialiseCAN2(pDll)
    while (a==1)
    {
        Stopwatch t = Stopwatch.StartNew();
        sendCAN(ref thereIsSupposedToBeAMessageHere);
        getCAN(ref probablySupposedToBeSomethingHereToo);
        ID0006NavComStatus(supportPoints);
        string state = Convert.ToString(gNavStatus);

        while (t.Elapsed < timer10ms) 
        { /*nothing*/}
    }
}