C 代码在 3 字节中通过 USB 2.0 检索温度的问题

问题描述 投票:0回答:2

我目前正在 C. 从事供暖调节项目。

我已经在项目的不同部分取得了很多进展,但我卡在了温度读取部分。

我的指示是:

读取是通过电子板和调节微型计算机之间的 USB 2.0 连接完成的: • 通过 FTDI 的 FT245R 电路的 FPGA 到 USB 传输接口 • 温度传输协议: o 外部和内部的数字化相对温度 (SOT) 交替传输超过 6 个字节。 o 40 ms 周期代表实际 100 秒。 o 12 位数字化相对温度 SOT 编码(保留传感器编码):以 °C 为单位的绝对温度 = -39.64 + 0.04 x SOT .

我有一个描述表,说明两个温度如何分别用 3 个字节表示:

                  Byte       bit   
                             7; 6 ; 5 ; 4 ; 3; 2 ; 1; 0 
Outer Temperature :1st      0 ;0 ;0 ;0 ; SOT bit 11;SOT bit 10;SOT bit 9;SOT bit 8 
                   2nd      0 ;0 ;0 ;1 ; SOT bit 7; SOT bit 6; SOT bit 5;SOT bit 4 
                   3rd      0 ;0 ;1 ;0 ; SOT bit 3; SOT bit 2; SOT bit 1;SOT bit 0 
Inner Temperature :1st      1 ;0 ;0 ;0 ; SOT bit 11;SOT bit 10;SOT bit 9;SOT bit 8 
                   2nd      1 ;0 ;0 ;1 ; SOT bit 7; SOT bit 6; SOT bit 5;SOT bit 4 
                   3rd      1 ;0 ;1 ;0 ; SOT bit 3; SOT bit 2; SOT bit 1;SOT bit 0

我已经做了一个 releve() 函数,但是我不明白如何正确地进行位移以获得正确的值。

#include "releve.h"
#include <windows.h>
#include "ftd2xx.h"
#include "define.h"
void releve(){
    FT_HANDLE ftHandle;
    FT_STATUS ftStatus;
    DWORD RxBytes = 8;
    DWORD BytesReceived;
    ftStatus = FT_Open(0, &ftHandle);
    char RxBuffer[6];
    // Déclaration des variables
    int tempExtSOT =  0.0;
    int tempIntSOT = 0.0;
    float tempExtDec = 0.0;
    float tempIntDec = 0.0;
    ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived);
    
    for(int i =0;i<6;i++){
        if((r))
    }
// Conversion en décimal des valeurs SOT récupérées  
    tempExtDec = -39.64 + (0.04 * tempExtSOT);   // Conversion de la température extérieure en décimal  
    tempIntDec = -39.64 + (0.04 * tempIntSOT);   // Conversion de la température intérieure en décimal
    printf("%g\n", tempExtSOT);
    printf("%g\n", tempIntSOT);
    printf("%g\n", tempExtDec);
    printf("%g", tempIntDec);

我正在使用 FT_Read 函数将 6 个字节的值以十六进制形式检索到缓冲区中。感谢您的帮助和指导!


c bit d2xx
2个回答
0
投票

您好 retiox2,欢迎来到 stackoverflow!

根据您的代码和温度传输协议,您似乎尝试处理接收到的字节并提取内部和外部温度的 SOT 值。

我认为您可以通过分析接收到的字节并使用按位运算来移位和组合相关位来做到这一点。

我改变了

releve()
一点点:

void releve() {
    FT_HANDLE ftHandle;
    FT_STATUS ftStatus;
    DWORD RxBytes = 6;
    DWORD BytesReceived;
    ftStatus = FT_Open(0, &ftHandle);
    unsigned char RxBuffer[6];

    // Declaration of variables
    int tempExtSOT = 0;
    int tempIntSOT = 0;
    float tempExtDec = 0.0;
    float tempIntDec = 0.0;

    ftStatus = FT_Read(ftHandle, RxBuffer, RxBytes, &BytesReceived);

    // Extract SOT values from received bytes
    tempExtSOT |= (RxBuffer[0] & 0x0F) << 8;
    tempExtSOT |= (RxBuffer[1] & 0x0F) << 4;
    tempExtSOT |= (RxBuffer[2] & 0x0F);

    tempIntSOT |= (RxBuffer[3] & 0x0F) << 8;
    tempIntSOT |= (RxBuffer[4] & 0x0F) << 4;
    tempIntSOT |= (RxBuffer[5] & 0x0F);

    // Conversion of SOT values to decimal temperatures
    tempExtDec = -39.64 + (0.04 * tempExtSOT); // Conversion of the outer temperature to decimal
    tempIntDec = -39.64 + (0.04 * tempIntSOT); // Conversion of the inner temperature to decimal

    printf("%d\n", tempExtSOT);
    printf("%d\n", tempIntSOT);
    printf("%g\n", tempExtDec);
    printf("%g", tempIntDec);
}

这里发生了什么?

  • 我将
    RxBytes
    更改为 6,因为您提到您正在接收 6 个字节的温度数据。
  • char RxBuffer[6]
    应该是
    unsigned char RxBuffer[6]
    所以我们在执行按位运算时一定要使用无符号值。
  • 删除了你的 for 循环,因为它没有被使用
  • 添加了按位运算以提取和组合内部和外部温度的 SOT 位
  • printf
    函数中
    tempExtSOT
    tempIntSOT
    的格式说明符更改为
    %d
    ,因为它们是整数。

希望对您有所帮助!


0
投票

正如您从表中看到的那样,这些值只是简单地打包在每个字节的低 4 位中,以大端顺序提供。所以你可以写一个简单的函数来构建这样的值:

float GetTemperature(const char *byte3)
{
    int SOT = (byte3[0] & 0x0f) << 8
            | (byte3[1] & 0x0f) << 4
            | (byte3[2] & 0x0f);
    return -39.64f + 0.4f * SOT;
}

看到SOT的构造只是简单的屏蔽掉低4位,然后左移适当数量的二进制位。各个部分通过按位或组合,但如果您愿意,可以在这种情况下使用普通加法。

您可能还希望使用高 4 位来验证数据是否有效。

现在您可以使用它从缓冲区获取温度:

float tempExtDec = GetTemperature(RxBuffer);
float tempIntDec = GetTemperature(RxBuffer + 3);
© www.soinside.com 2019 - 2024. All rights reserved.