求助:Arduino向串口写16进制数据问题-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: shory

[已解决] 求助:Arduino向串口写16进制数据问题

[复制链接]
 楼主| 发表于 2022-5-18 16:52 | 显示全部楼层
本帖最后由 shory 于 2022-5-18 16:54 编辑
shum_elli 发表于 2022-5-17 20:35
楼主 能否把读取BL0940数据的代码发下,现在刚好需要用到这个芯片呃呃呃

我也是参考了许多Github上的示例来的,其实我也不会计算...

  1. unsigned char bl0940Data[35];       // BL0940返回数据
复制代码

  1. // 接收数据
  2. while(Serial.available() > 0) {
  3.         if(Serial.peek() == 0x55) {
  4.             Serial.readBytes(bl0940Data, 35);
  5.         }
  6.     }
复制代码

  1. //解析电能信息
  2. void parseBL0940Data()
  3. {
  4.     //当前电压
  5.     uint32_t tmpV = (uint32_t(bl0940Data[12])&0xFF) <<16 | (uint32_t(bl0940Data[11])&0xFF) <<8 | (uint32_t(bl0940Data[10])&0xFF);
  6.     int currentV = round(((float)tmpV * 2376.318) / 79931000.0f);

  7.     //当前电流
  8.     uint32_t bl0940Current = (uint32_t(bl0940Data[6])&0xFF) << 16 | (uint32_t(bl0940Data[5])&0xFF) << 8 | (uint32_t(bl0940Data[4])&0xFF);
  9.     float currentI = ((float)bl0940Current * 1.218) / 324004.0f;

  10.     //当前功率
  11. //    int32_t tmpW = (uint32_t(bl0940Data[18])&0xFF) <<24 | (uint32_t(bl0940Data[17])&0xFF) <<16 | (uint32_t(bl0940Data[16])&0xFF) << 8;
  12. //    int32_t tepWW = tmpW >> 8;
  13. //    currentW = tepWW * 1.218 * 1.218 * 1951.0f) / (4046000.0f);

  14.     long powerValue = 0x7FFFFF & (((uint32_t(bl0940Data[18])&0xFF)<<16) | ((uint32_t(bl0940Data[17])&0xFF)<<8) | (uint32_t(bl0940Data[18])&0xFF));
  15.     powerValue = ((uint32_t(bl0940Data[18])&0xFF) & 0x80) ? powerValue - 0x7FFFFF : powerValue;
  16.     int currentW = round((((float)powerValue * 1.218 * 1.218 * 1951.0f)) / 4046000.0f);

  17.     //当前电度数
  18.     uint32_t tmpkWh = (uint32_t(bl0940Data[24])&0xFF) << 16 | (uint32_t(bl0940Data[23])&0xFF) << 8 | (uint32_t(bl0940Data[22])&0xFF);
  19.     float currentkWh = (0.33721683 / 4046.0f) * (float)tmpkWh;

  20.     //当前内部温度
  21.     uint16_t tps = (uint32_t(bl0940Data[29])&0xFF) << 8 | (uint32_t(bl0940Data[28])&0xFF);
  22.     byte currentT = round((170.0f/448.0f)*(((float)tps/2.0f)-32.0f)-45.0f);

  23. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 02:56 , Processed in 0.070537 second(s), 12 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表