【下载D2XX和LibFT4222H的库文件】
1. 下载D2XX(Windows版本)
D2XX是FTDI提供的可以直接通过一个DLL访问USB设备的驱动程序。
http://www.ftdichip.com/Drivers/D2XX.htm
2. 解压D2XX后文件夹内有对应的USB驱动文件、ftd2xx.h和对应的DLL或者静态库。C#只能直接使用DLL的方式。文件夹amd64对应64位系统,而i386对应32位系统。
3. 下载LibFT4222H库文件
https://www.ftdichip.com/Support/SoftwareExamples/FT4222H.htm界面下下载库文件。同样根据系统选择不同的DLL文件。
4. 在运行目录内拷贝对应的文件
【新建一个FT4222H的类,文件为FT4222H.cs。】
【根据ftd2xx.dll的哈希值判断dll文件是否符合当前系统】
private bool checkDllMatchSystem()
{
string file1, file2;
file1 = "ftd2xx.dll";
if (File.Exists(file1) == false)
return false;
if (Environment.Is64BitOperatingSystem == true)
{
file2 = ".\\dll\\amd64\\ftd2xx64.dll";
}
else
{
file2 = ".\\dll\\i386\\ftd2xx.dll";
}
//计算第一个文件的哈希值
var hash = System.Security.Cryptography.HashAlgorithm.Create();
var stream_1 = new System.IO.FileStream(file1, System.IO.FileMode.Open);
byte[] hashByte_1 = hash.ComputeHash(stream_1);
stream_1.Close();
//计算第二个文件的哈希值
var stream_2 = new System.IO.FileStream(file2, System.IO.FileMode.Open);
byte[] hashByte_2 = hash.ComputeHash(stream_2);
stream_2.Close();
if (BitConverter.ToString(hashByte_1) == BitConverter.ToString(hashByte_2))
return true;
else
return false;
}
【根据系统拷贝对应的dll文件】
public FT4222H()
{
if (checkDllMatchSystem() == false)
{
if (Environment.Is64BitOperatingSystem == true)
{
File.Copy(".\\dll\\amd64\\ftbusui.dll", ".\\ftbusui.dll", true);
File.Copy(".\\dll\\amd64\\ftcserco.dll", ".\\ftcserco.dll", true);
File.Copy(".\\dll\\amd64\\ftlang.dll", ".\\ftlang.dll", true);
File.Copy(".\\dll\\amd64\\ftserui2.dll", ".\\ftserui2.dll", true);
File.Copy(".\\dll\\amd64\\ftd2xx64.dll", ".\\ftd2xx.dll", true);
File.Copy(".\\dll\\amd64\\LibFT4222-64.dll", ".\\LibFT4222.dll", true);
}
else
{
File.Copy(".\\dll\\i386\\ftbusui.dll", ".\\ftbusui.dll", true);
File.Copy(".\\dll\\i386\\ftcserco.dll", ".\\ftcserco.dll", true);
File.Copy(".\\dll\\i386\\ftlang.dll", ".\\ftlang.dll", true);
File.Copy(".\\dll\\i386\\ftserui2.dll", ".\\ftserui2.dll", true);
File.Copy(".\\dll\\i386\\ftd2xx.dll", ".\\ftd2xx.dll", true);
File.Copy(".\\dll\\i386\\LibFT4222-32.dll", ".\\LibFT4222.dll", true);
}
}
}