c# - FTDI 写函数永无止境

标签 c# .net-4.0 .net-3.5 ftdi

我有一个 FTDI 写入函数,当程序是 .net3.5 时它可以工作,但我一直在更新它,所以它现在可以在 .net 4.0 上运行,现在我的 FTDI 写入函数在输入后没有返回。

private FT_HANDLE portHandle = 0;

public unsafe int Write(byte[] buffer, int offset, int count)
{
    if (portHandle == 0)
    {
        throw new IOException("Port Closed");
    }

    try
    {
        uint bytesWritten = 0;
        Status status = Status.FT_OTHER_ERROR;
        byte[] data = new byte[count];
        for (int i = 0; i < count; i++)
        {
            data[i] = buffer[i + offset];
        }
        fixed (byte* pBuffer = data)
        {
            status = FT_Write(portHandle, pBuffer, (uint)count, ref bytesWritten);// the line that never returns
        }
        if (status == Status.FT_OK)
        {
            return (int)bytesWritten;
        }
        throw new IOException("Failed To Write: " + status.ToString());
    }
    catch (DllNotFoundException ex)
    {
        throw new NotSupportedException("Driver not installed", ex);
    }
}

portHandle 不会陷入等同于 0 if check。 我的问题是可能是什么原因造成的,我该如何解决。

最佳答案

在 dot net3.5 到 4.0 中,他们更改了此类设备的计时器和设置,首先将计时器设置为您想要的,但是不要使用变量、getter 和 setter,因为它们将无法工作,因为 Windows 使用它们的方式使用常量和 uints 并确保你为读取和写入提供了足够的时间(读取通常比写入长)记住时间以毫秒为单位完成这应该可以工作,因为我在进行切换时遇到了同样的问题

所以要点设置读写超时

不要使用 getter 和 setter

确保它们是常量

关于c# - FTDI 写函数永无止境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28317284/

相关文章:

c# - 组合框选择

c# - 如何正确设置 HttpClient 的延续?

c# - 服务在 StackOverflowException 上继续

c# - 在 web.config 中定义颜色并在 CSS 文件中使用它

c# - 以编程方式设置 Segoe UI Symbol 字体

c# - 无法在 C# 中将 BsonArray 转换为 BsonDocument

c# - WPF Popup 有一个黑色边框而不是透明边框

sockets - 我在 Windows Server 2003 中可以拥有的打开套接字数的上限是多少

c# - 测试一个值是否包含在 Dictionary<TKey, List<TValue>> 中

c# - LINQ,在映射中实现接口(interface)和异常的实体