C# 使用FTD2XX_NET托管的.NET包装类读取实时温度数据
从DLPIO20设备上的DS18B20+传感器读取实时温度数据时出现问题。我正在Windows平台上使用ftd2x.DLL.NET包装器(版本1.0.14)(根据链接:) 测试应用程序以证明问题如下 这就是我所说的实时性 2.1在相当恒定的环境温度(比如20°C)下,进行少量温度读数,以便在调试模式下看到一致的结果(第55行温度读数循环结束时的断点) 2.2.然后,当处于断点时,只需在传感器上呼吸或使用其他方式将其加热到高于环境温度几度的温度 2.3。再运行两个或三个温度读数循环 在传感器加热后,我得到的第一个读数是20°C的“旧”温度。只有在第二次或第三次读取循环中,我才得到28°C左右的实际结果 有趣的是,使用测试应用程序IO20Demo(购买DLP-IO20板时提供),如果我在读取之前发出Convert命令,我会立即得到28°C的实时结果。对于这个程序和托管的.NET包装类,我也在读取之前发送Convert命令,但没有用。最大的区别是IO20Demo不使用托管包装器类,而是直接使用ftd2x.lib 你能帮我理解我做错了什么吗?如何使用托管的.NET包装类获取实时温度数据 非常感谢你的帮助C# 使用FTD2XX_NET托管的.NET包装类读取实时温度数据,c#,real-time,ftdi,C#,Real Time,Ftdi,从DLPIO20设备上的DS18B20+传感器读取实时温度数据时出现问题。我正在Windows平台上使用ftd2x.DLL.NET包装器(版本1.0.14)(根据链接:) 测试应用程序以证明问题如下 这就是我所说的实时性 2.1在相当恒定的环境温度(比如20°C)下,进行少量温度读数,以便在调试模式下看到一致的结果(第55行温度读数循环结束时的断点) 2.2.然后,当处于断点时,只需在传感器上呼吸或使用其他方式将其加热到高于环境温度几度的温度 2.3。再运行两个或三个温度读数循环 在传感器加热后
/// <summary>
/// Program for DLPIO20 device to read temperature data from DS18B20+
/// digital temperature sensor attached to one of its channels.
/// </summary>
class Program
{
static void Main(string[] args)
{
FTDI FtdiWrapper = null;
// DLPIO20 channel where the DS18B20+ sensor is attached
byte sensorChannel = 0x06;
try
{
// create new instance of the FTDI device class
FtdiWrapper = ConnectToFirstFtdiDevice();
if (FtdiWrapper == null || !FtdiWrapper.IsOpen)
{
throw new Exception("Error connection to FTDI device.");
}
// didn't helped at all for 85dC issue
//PurgeRxBuffer(FtdiWrapper);
// helped avoid 85dC issue at first read
ConvertSensorData(FtdiWrapper, sensorChannel);
// send read sensor command
float? degreesC = null;
for (int i = 0; i < 100; i++)
{
// calling Convert just before ReadTemperatureSensor causes
// IO20Demo (using FTD2XX.lib) to return real temperature
// but it doesn't help when using .NET wrapper
ConvertSensorData(FtdiWrapper, sensorChannel);
// other failed attempts to get real time sensor data
// previous value returned:
//PurgeRxBuffer(FtdiWrapper);
// read but only initiate conversion on success
degreesC = ReadTemperatureSensor(FtdiWrapper, sensorChannel);
if (degreesC == null)
{
throw new Exception("Error converting raw data to Celsius");
}
var message = string.Format("Success! {0}° Celsius.", degreesC);
Console.WriteLine(message);
}
Console.WriteLine("Press any key to exit ...");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine("Press any key to exit ...");
Console.ReadKey();
}
finally
{
if (FtdiWrapper != null && FtdiWrapper.IsOpen)
{
FtdiWrapper.Close();
}
}
}
转换原始传感器数据的方法
static private void ConvertSensorData(FTDI FtdiWrapper, byte sensorChannel)
{
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OTHER_ERROR;
UInt32 numBytesWritten = 0;
byte[] convertCommand = new byte[] { 0x03, 0x40, sensorChannel };
ftStatus = FtdiWrapper.Write(convertCommand, convertCommand.Length, ref numBytesWritten);
bool isAllBytesWritten = numBytesWritten == convertCommand.Length;
if (ftStatus != FTDI.FT_STATUS.FT_OK && isAllBytesWritten)
throw new Exception(string.Format("Failed to write Convert command to device; Status: {0}, isAllBytesWritten: {1}", ftStatus.ToString(), isAllBytesWritten));
}
读取传感器温度的方法
static private float? ReadTemperatureSensor(FTDI FtdiWrapper, byte sensorChannel)
{
float? degreesC = null;
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OTHER_ERROR;
UInt32 numBytesWritten = 0;
byte[] readSensorCommand = new byte[] { 0x03, 0x41, sensorChannel };
ftStatus = FtdiWrapper.Write(readSensorCommand, readSensorCommand.Length, ref numBytesWritten);
bool isAllBytesWritten = numBytesWritten == readSensorCommand.Length;
if (ftStatus != FTDI.FT_STATUS.FT_OK && isAllBytesWritten)
throw new Exception(string.Format("Failed to write readSensorCommand to device; Status: {0}, isAllBytesWritten: {1}", ftStatus.ToString(), isAllBytesWritten));
// Read back response
UInt32 numBytesAvailable = 0;
UInt32 numBytesExpected = 2; // read sensor command expected to return 2 bytes
while (numBytesAvailable == 0)
{
Thread.Sleep(40); // value of 40 taken from DLP IO20 demo solution
ftStatus = FtdiWrapper.GetRxBytesAvailable(ref numBytesAvailable);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
throw new Exception("Failed to get number of bytes available to read; error: " + ftStatus);
} //while (numBytesAvailable < numBytesExpected);
if (numBytesAvailable != numBytesExpected)
throw new Exception("Error: Invalid data in buffer. (1350)");
UInt32 numBytesRead = 0;
byte[] rawData = new byte[numBytesExpected];
ftStatus = FtdiWrapper.Read(rawData, numBytesAvailable, ref numBytesRead);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
throw new Exception("Failed to read data from device after command has been sent; error: " + ftStatus);
//convert raw response data to degrees Celsius
degreesC = ConvertTemperature(rawData);
return degreesC;
}
方法将温度从原始数据转换为摄氏度
static private float? ConvertTemperature(byte[] rawData)
{
float? tempCelsius = null;
bool isnegative = false;
//check input
if (rawData.Length < 2)
throw new Exception(string.Format("Input parameter rawData for temperature conversion must be 2 bytes, actual length is: {0}", rawData.Length));
int temp = rawData[0] | (rawData[1] << 8);
if ((temp & 0x8000) == 0x8000)//if MSBit is set then negative temperature
{
temp &= 0x07ff;
isnegative = true;
temp = 0x800 - temp;
}
temp &= 0x07ff;
tempCelsius = (float)((float)temp / 16.0);
if (isnegative) tempCelsius *= -1;
return tempCelsius;
}
}
静态私有浮动?转换器温度(字节[]原始数据)
{
浮动?温度=零;
bool为阴性=假;
//检查输入
if(rawData.Length<2)
抛出新异常(string.Format(“用于温度转换的输入参数rawData必须为2字节,实际长度为:{0}”,rawData.length));
int temp=rawData[0]|(rawData[1]我认为您需要将ConnectToFirstFtdiDevice中FTDI设备上的延迟计时器设置为最小值,我使用16ms,这有助于我解决此类问题。purgeRX只是一个软件缓冲区,而不是HW,因此不会阻止您在FTDI USB缓冲区中进行过时的测量。应该使用monitor/com这里的命令/响应设计模式。这里有重复的命令/响应序列,因此实现一个模式可以减少代码中存在的冗余。我从一个带有事件处理程序的接口&命令/响应结构开始;即,如果FTDI.deviceCount>0,则触发事件处理程序,然后使用子依赖处理程序.在接收端,最低级别的.net串行端口处理程序基于TDM。它将在过程中丢失一些信息。要完成此操作,请添加一个简单的数据包传递协议,例如两端都带有校验和的XOR。即,计算设备上消息的校验和、发送命令、另一端的接收命令、计算校验和、再次比较t捕获校验和。如果相同,则确定,否则,将捕获的命令回显到设备。这里是ersa
my two Seconds==my two dollars,同样的东西调用FtdiWrapper.GetRxBytesAvailable无法获得可用字节。您需要调用FtdiWrapper。在循环中读取,直到获得预期字节,然后跳出循环
static private void PurgeRxBuffer(FTDI FtdiWrapper)
{
UInt32 numBytesAvailable = 0;
UInt32 numBytesRead = 0;
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OTHER_ERROR;
byte[] rx = new byte[1001]; //allocate large enough space to read from device
Thread.Sleep(5);
ftStatus = FtdiWrapper.GetRxBytesAvailable(ref numBytesAvailable);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
throw new Exception("Failed to get number of bytes available to read; error: " + ftStatus);
if (numBytesAvailable > 1000)
numBytesAvailable = 1000;
while (numBytesAvailable > 0)
{
//read the data from the buffer
numBytesRead = 0;
ftStatus = FtdiWrapper.Read(rx, numBytesAvailable, ref numBytesRead);
ftStatus = FtdiWrapper.GetRxBytesAvailable(ref numBytesAvailable);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
throw new Exception("Failed to get number of bytes available to read; error: " + ftStatus);
if (numBytesAvailable > 1000)
numBytesAvailable = 1000;
Thread.Sleep(5);
}
}
static private float? ConvertTemperature(byte[] rawData)
{
float? tempCelsius = null;
bool isnegative = false;
//check input
if (rawData.Length < 2)
throw new Exception(string.Format("Input parameter rawData for temperature conversion must be 2 bytes, actual length is: {0}", rawData.Length));
int temp = rawData[0] | (rawData[1] << 8);
if ((temp & 0x8000) == 0x8000)//if MSBit is set then negative temperature
{
temp &= 0x07ff;
isnegative = true;
temp = 0x800 - temp;
}
temp &= 0x07ff;
tempCelsius = (float)((float)temp / 16.0);
if (isnegative) tempCelsius *= -1;
return tempCelsius;
}
}