首页 > 系统相关 >Linux利用DS9490R读取多个温湿度

Linux利用DS9490R读取多个温湿度

时间:2024-03-14 10:59:27浏览次数:25  
标签:06 温湿度 devices Linux portnum DS9490R find 26.0 255

1. DS9490R是一款由Maxim Integrated(原Dallas Semiconductor)生产的USB至1-Wire®接口适配器。它允许计算机通过USB端口与遵循1-Wire协议的设备进行通信,非常适合需要通过PC进行数据采集和控制的应用。

1.1 基本特性

  • 接口类型:USB至1-Wire。
  • 连接方式:通过USB接口连接至PC,提供1-Wire通信接口。
  • 支持设备:兼容所有Dallas Semiconductor/Maxim Integrated的1-Wire设备。
  • 封装:提供RJ11连接器,方便直接连接至1-Wire设备或网络。

1.2 应用场景

  • 温度监测:与DS18B20等数字温度传感器配合使用,进行温度数据的采集和监控。
  • 系统认证:结合带有唯一序列号的1-Wire存储设备,如DS1990A,用于系统访问控制和认证。
  • 环境监测:连接多种1-Wire传感器,实现对环境参数(如湿度、光照等)的监测。

1.3 外形如下

在这里插入图片描述

2. 温湿度探头, 支持1-wire总线

在这里插入图片描述
在这里插入图片描述

3. 测试环境

3.1 Ubuntu 16.04, gcc V5.3.1

lsb_release -a
# Distributor ID:	Ubuntu
# Description:	Ubuntu 16.04 LTS
# Release:	16.04
# Codename:	xenial

gcc -v
# gcc version 5.3.1 20160413 (Ubuntu/Linaro 5.3.1-14ubuntu2)

# 安装libusb
sudo apt install libusb-dev

3.2 设备接线

在这里插入图片描述

4. 相关测试代码

4.1 基础部分

int ow_device_acquire(char *device_name)
{
  g_portnum = owAcquireEx(device_name);
  //printf("device_acquire start-> owAcquireEx(\"%s\"), ret = %d\n", com, g_portnum);

  if (g_portnum == -1) {
    owerr_report();
    port_opened = 0;
    return 0;
  }

  //printf("device_acquire OK-> owAcquireEx(\"%s\"), ret = %d\n", com, g_portnum);
  port_opened = 1;
  return 1;
}

int ow_device_open(char *device_name)
{
  if (port_opened) return 1 ;
  return ow_device_acquire(device_name);
}

int ow_device_close (void)
{
  /* Done with 1-wire net and weather station */
  if (!port_opened)
    return 1;	/* Nothing to do */

  /* release the 1-Wire Net */
  owRelease(g_portnum);
  port_opened = 0;

  return 1;
}

int ow_read2c_base(int portnum, uchar *SerialNum, unsigned char *send_block)
{
  uchar rt = FALSE;
    int send_cnt, i, loop = 0;
    uchar lastcrc8;

   // set the device serial number to the counter device
   owSerialNum(portnum, SerialNum, FALSE);

   for (loop = 0; loop < 2; loop ++) {
      // access the device
      if (owAccess(portnum)) {
         // send the convert command and start power delivery.
         // 1 温度变换  44H 进行温度变换,结果存入内部9字节RAM中。
         if (!owWriteBytePower(portnum, 0x44))
            return FALSE;

         // must sleep for > 750ms
         msDelay(800);

         // turn off the 1-Wire Net strong pull-up
         if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
            return FALSE;

         // access the device
         if (owAccess(portnum)) {
            // create a block to send that reads the temperature
            // read scratchpad command, 2 读暂存器  BEH 读内部RAM中9字节的内容。
            send_cnt = 0;
            send_block[send_cnt++] = 0xBE;
            // now add the read bytes for data bytes and crc8
            for (i = 0; i < 9; i++)
               send_block[send_cnt++] = 0xFF;

            // now send the block
            if (owBlock(portnum, FALSE, send_block, send_cnt)) {
               // initialize the CRC8
               setcrc8(portnum, 0);
               // perform the CRC8 on the last 8 bytes of packet
               for (i = send_cnt - 9; i < send_cnt; i++) {
                  lastcrc8 = docrc8(portnum, send_block[i]);
               }

               // verify CRC8 is correct
               if (lastcrc8 == 0x00) {
                  // success
                  rt = TRUE;
                  break;
               }
            }
         }
      }
   }

   // return the result flag rt
   return rt;
}

4.2 主调用部分

int ow_read2c(int portnum)
{
    int count=0, i;
    unsigned char rom_id[MAXDEVICES][8];
    unsigned char rx[32];
    u8 c, l;
    unsigned short t;
    short vt;
    float v, vh;

  owFamilySearchSetup(portnum, 0x00);
  while ((count < MAXDEVICES) && (owNext(portnum, TRUE, FALSE))) {
    count++;

      owSerialNum(portnum, rom_id[count], TRUE);
      printf("%d: %s, ", count, array_hex(&rom_id[count], 8));

      memset(rx, 0, sizeof(rx));
    if (ow_read2c_base(portnum, rom_id[count], rx)) {
      printf("%s, ", array_hex(rx, 10));
      // 2CEE039A0000EE92, BEEA0500017FC6B233A4, 0-1: 19.0(EA 05), 69.0(B2 33)
      c = rx[3];  // TH, 一般用作缆号, 可根据现场规则修改
      l = rx[4];  // TL, 一般用做点号, 可根据现场规则修改
      t = ((rx[2] << 8) + rx[1]) & 0x0fff;  // low 12 bit
        vt = (signed)t;
        v = -45 + 175 * vt / 4095;
        t = (rx[7] << 8) + rx[6];
        vh = 100 * t / 65535;
        printf("%d-%d: %.1f(%02X %02X), %.1f(%02X %02X)\n", c, l, v, rx[1], rx[2], vh, rx[7], rx[8]);
      }
      else {
        printf("Error reading temperature, verify device present:%d\n", (int)owVerify(portnum, FALSE));
      }
    }

    owTouchReset(portnum);

  return 1;
}

int main(int argc, char *argv[])
{
  unsigned char rom_id[MAXDEVICES][8];
    int iret, i, family_id;
    float current_temp;
    unsigned short usdata;
    int portnum = g_portnum;

  // --- "USB"则查询所有的USB口
  iret = ow_device_open("USB");
  if (iret != 1) {
    printf("Device open Failure!\n");
    return(-1);
  }
  
#if 1
  family_id = 0x2c;
  iret = ow_find_devices(portnum, rom_id, family_id);
  for (i=0; i<iret; i++) {
    printf("find_devices: %d, %s\n", i+1, array_hex(rom_id[i], 8));
  }
  ow_read2c(portnum);
#endif

  // --- end must close
  ow_device_close();

  return 1;
}

4.3 Makefile

UNAME_S:=$(shell uname -s)
ifeq ($(UNAME_S),Darwin)
	RCOMPILE_FLAGS = -D OS_MAC
	BUILD = /tmp
else
	RCOMPILE_FLAGS = -D OS_LINUX
	BUILD = /dev/shm
endif
BIN = xuow_test

SRC_PATH = .

CC  = gcc
LIB_PUBLIC = -lusb -lxuow

all: clean mybin
#
mybin:
	$(CC) $(RCOMPILE_FLAGS) -o $(BUILD)/$(BIN) \
		$(SRC_PATH)/ow_base.c \
		$(SRC_PATH)/xu_base.c \
		$(SRC_PATH)/main.c  \
		$(LIB_PUBLIC)

clean:
	@rm -vf $(BUILD)/$(BIN)

4.4 测试结果, 49个温湿度探头读取成功

find_devices: 1, 2CEE004A0000EEDD
find_devices: 2, 2CEEE0490000EE8F
find_devices: 3, 2CEE484A0000EE0A
find_devices: 4, 2CEEA84A0000EED0
find_devices: 5, 2CEE584D0000EEF0
find_devices: 6, 2CEE384A0000EE67
find_devices: 7, 2CEEB84D0000EE2A
find_devices: 8, 2CEE244D0000EEBC
find_devices: 9, 2CEEA44A0000EEF1
find_devices: 10, 2CEEE4490000EE90
find_devices: 11, 2CEE544A0000EE57
find_devices: 12, 2CEE2C4D0000EE82
find_devices: 13, 2CEE6C4D0000EE6B
find_devices: 14, 2CEEEC490000EEAE
find_devices: 15, 2CEE3C4A0000EE78
find_devices: 16, 2CEE424A0000EEB7
find_devices: 17, 2CEEE2490000EE0C
find_devices: 18, 2CEE524A0000EECB
find_devices: 19, 2CEEAA4D0000EED5
find_devices: 20, 2CEE5A4A0000EEF5
find_devices: 21, 2CEE5A4D0000EE73
find_devices: 22, 2CEEFA490000EE4E
find_devices: 23, 2CEEE6490000EE13
find_devices: 24, 2CEEEE490000EE2D
find_devices: 25, 2CEEDE490000EEA9
find_devices: 26, 2CEE3E4A0000EEFB
find_devices: 27, 2CEE694D0000EEB9
find_devices: 28, 2CEEE9490000EE7C
find_devices: 29, 2CEE594A0000EEBB
find_devices: 30, 2CEE394A0000EEAA
find_devices: 31, 2CEEF9490000EE00
find_devices: 32, 2CEE454A0000EEE6
find_devices: 33, 2CEEE5490000EE5D
find_devices: 34, 2CEEB54D0000EEC6
find_devices: 35, 2CEEF5490000EE21
find_devices: 36, 2CEEED490000EE63
find_devices: 37, 2CEEDD490000EEE7
find_devices: 38, 2CEEFD490000EE1F
find_devices: 39, 2CEEA3490000EE28
find_devices: 40, 2CEEE3490000EEC1
find_devices: 41, 2CEE534A0000EE06
find_devices: 42, 2CEEB34D0000EE5A
find_devices: 43, 2CEEAB4A0000EE9E
find_devices: 44, 2CEEEB490000EEFF
find_devices: 45, 2CEE074A0000EE8C
find_devices: 46, 2CEE274D0000EEF2
find_devices: 47, 2CEE574A0000EE19
find_devices: 48, 2CEE6F4D0000EE25
find_devices: 49, 2CEE3F4A0000EE36
1: 2CEE004A0000EEDD, BE9206FFFF9805785FC3, 255-255: 26.0(92 06), 46.0(78 5F)
2: 2CEEE0490000EE8F, BE9006FFFF790679B997, 255-255: 26.0(90 06), 47.0(79 B9)
3: 2CEE484A0000EE0A, BEAF06FFFF026373955A, 255-255: 28.0(AF 06), 45.0(73 95)
4: 2CEEA84A0000EED0, BEA306FFFF1023743648, 255-255: 27.0(A3 06), 45.0(74 36)
5: 2CEE584D0000EEF0, BE8F067FFFEEE479A502, 127-255: 26.0(8F 06), 47.0(79 A5)
6: 2CEE384A0000EE67, BEB506FFFFFA44709CE8, 255-255: 28.0(B5 06), 43.0(70 9C)
7: 2CEEB84D0000EE2A, BE4C06FFFF0C238B7FB0, 255-255: 23.0(4C 06), 54.0(8B 7F)
8: 2CEE244D0000EEBC, BE5B06FFFFD0A487EE3D, 255-255: 24.0(5B 06), 52.0(87 EE)
9: 2CEEA44A0000EEF1, BE7D06FFFFD3647E777B, 255-255: 25.0(7D 06), 49.0(7E 77)
10: 2CEEE4490000EE90, BE9206FFFF5B2278C611, 255-255: 26.0(92 06), 46.0(78 C6)
11: 2CEE544A0000EE57, BEB506FFFF9CE5724694, 255-255: 28.0(B5 06), 44.0(72 46)
12: 2CEE2C4D0000EE82, BE8106FEFE7EC67D3707, 254-254: 26.0(81 06), 49.0(7D 37)
13: 2CEE6C4D0000EE6B, BE8706FFFF14237B6C46, 255-255: 26.0(87 06), 48.0(7B 6C)
14: 2CEEEC490000EEAE, BE8B06FFFFEB047A9F2E, 255-255: 26.0(8B 06), 47.0(7A 9F)
15: 2CEE3C4A0000EE78, BE8D06FFFE0E6378BAEA, 255-254: 26.0(8D 06), 47.0(78 BA)
16: 2CEE424A0000EEB7, BE8A06F7FECF047B91A4, 247-254: 26.0(8A 06), 48.0(7B 91)
17: 2CEEE2490000EE0C, BE9006FFFFFA64786A5D, 255-255: 26.0(90 06), 47.0(78 6A)
18: 2CEE524A0000EECB, BE8D06FFFE13037A7273, 255-254: 26.0(8D 06), 47.0(7A 72)
19: 2CEEAA4D0000EED5, BE8106FFFFC9A47C9FA9, 255-255: 26.0(81 06), 48.0(7C 9F)
20: 2CEE5A4A0000EEF5, BE5E06FFFF87C5863875, 255-255: 24.0(5E 06), 52.0(86 38)
21: 2CEE5A4D0000EE73, BE7106FFFF7A467F49C0, 255-255: 25.0(71 06), 49.0(7F 49)
22: 2CEEFA490000EE4E, BEA7067FFFE7E471DADC, 127-255: 27.0(A7 06), 44.0(71 DA)
23: 2CEEE6490000EE13, BE7F06FFFFA6C57C4E3A, 255-255: 26.0(7F 06), 48.0(7C 4E)
24: 2CEEEE490000EE2D, BE9706F7FF0BE37654B7, 247-255: 27.0(97 06), 46.0(76 54)
25: 2CEEDE490000EEA9, BEB706FFFF85C56EEFBF, 255-255: 28.0(B7 06), 43.0(6E EF)
26: 2CEE3E4A0000EEFB, BE8306FFFD27A37A85C3, 255-253: 26.0(83 06), 47.0(7A 85)
27: 2CEE694D0000EEB9, BE9C06FFFFF1A4748CDC, 255-255: 27.0(9C 06), 45.0(74 8C)
28: 2CEEE9490000EE7C, BE8306FFFFC9247BC35B, 255-255: 26.0(83 06), 48.0(7B C3)
29: 2CEE594A0000EEBB, BE6D06FEFFF46481B026, 254-255: 25.0(6D 06), 50.0(81 B0)
30: 2CEE394A0000EEAA, BE8D06FFFF3A2376AF2E, 255-255: 26.0(8D 06), 46.0(76 AF)
31: 2CEEF9490000EE00, BEB806FFFFE1046F6B0A, 255-255: 28.0(B8 06), 43.0(6F 6B)
32: 2CEE454A0000EEE6, BE8506FEFEC4C47B4A78, 254-254: 26.0(85 06), 48.0(7B 4A)
33: 2CEEE5490000EE5D, BE9706FFFFC7A4776CCE, 255-255: 27.0(97 06), 46.0(77 6C)
34: 2CEEB54D0000EEC6, BE9306FFFFB125771C4B, 255-255: 26.0(93 06), 46.0(77 1C)
35: 2CEEF5490000EE21, BE8606FFFF52067BE062, 255-255: 26.0(86 06), 48.0(7B E0)
36: 2CEEED490000EE63, BE8606FFFFD5247C8FAC, 255-255: 26.0(86 06), 48.0(7C 8F)
37: 2CEEDD490000EEE7, BE9C06FFFE032374BF23, 255-254: 27.0(9C 06), 45.0(74 BF)
38: 2CEEFD490000EE1F, BE8E06FFFFD9C4787E42, 255-255: 26.0(8E 06), 47.0(78 7E)
39: 2CEEA3490000EE28, BE8606FFF78D057A31EE, 255-247: 26.0(86 06), 47.0(7A 31)
40: 2CEEE3490000EEC1, BEA506FFFF210773FCCC, 255-255: 27.0(A5 06), 44.0(73 FC)
41: 2CEE534A0000EE06, BE9706FDFE6CE67A737B, 253-254: 27.0(97 06), 48.0(7A 73)
42: 2CEEB34D0000EE5A, BEA406FFFF95E5762851, 255-255: 27.0(A4 06), 46.0(76 28)
43: 2CEEAB4A0000EE9E, BE9E06FFFFEF04752A37, 255-255: 27.0(9E 06), 45.0(75 2A)
44: 2CEEEB490000EEFF, BEA106FFFFBA45751032, 255-255: 27.0(A1 06), 45.0(75 10)
45: 2CEE074A0000EE8C, BEA606FFFF4EE27183D9, 255-255: 27.0(A6 06), 44.0(71 83)
46: 2CEE274D0000EEF2, BE6F06FFFE520682FE1C, 255-254: 25.0(6F 06), 50.0(82 FE)
47: 2CEE574A0000EE19, BEB106FEFD5A1A756578, 254-253: 28.0(B1 06), 45.0(75 65)
48: 2CEE6F4D0000EE25, BE8E06FFFF2147792215, 255-255: 26.0(8E 06), 47.0(79 22)
49: 2CEE3F4A0000EE36, BEB806FFFFE1246FBFA8, 255-255: 28.0(B8 06), 43.0(6F BF)

标签:06,温湿度,devices,Linux,portnum,DS9490R,find,26.0,255
From: https://blog.csdn.net/ytsanhuan/article/details/136698200

相关文章

  • linux系统报错AER PCIe Bus Error
    1、报错信息pcieport0000:00:1c.7:AER:PCIeBusError:severity=Corrected,type=PhysicalLayer,(ReceiverID)device[8086:a33f]errorstatus/mask=00000001/00002000[0[RxErr2、修改grub文件//备份grubsudocp/etc/default/grub/etc/default/grub.bak//随便......
  • Linux安装MySQL
    安装对于MySQL数据库的安装,我们将要使用rpm进行安装。那么首先我们先了解一下什么rpm?**RPM:**全称为Red-HatPackageManager,RPM软件包管理器,是红帽Linux用于管理和安装软件的工具。我们要通过rpm,进行MySQL数据库的安装,主要的步骤如下:1).检测当前系统是否安装过MySQL相......
  • Linux学习-day8
    vim的使用安装vimyuminstallvim-y验证是否有vimvim的使用基本上vi/vim共分为三种模式,分别是:命令模式(Commandmode)最长用的,按下字母,a,i,o(a在光标前开始编辑,i是在光标处,开始编辑,o是在光标下一行开始编辑)当你使用vim标记某个文件时,第一步就进入了命令模式。你此......
  • Linux内核编译(版本6.0以及版本v0.01)并用qemu驱动
    系统环境:ubuntu-22.04.1-desktop-amd64目标平台:x86i386内核版本:linux-6.0.1linux-0.0.1环境配置修改root密码sudopasswd修改软件源(非必要)vmtools安装(实现win-linux软件互传)安装一些必须的软件:sudoaptinstallbuild-essentialopenssh-servervimnet-toolsgc......
  • Linux线程池的创建(超详细解析)
    线程池:若干个线程组合在一起形成线程池;为什么需要线程池:多线程版本服务器一个客户端就需要创建一个线程,如果客户端太多,明显不太合适;创建思路:我们需要一个线程池结构体,然后这个结构体里面包含任务池,这个线程池结构体是全局变量,需要使用互斥锁,当子线程执行回调函数时,把该线程......
  • Linux系统架构----Nginx的服务优化
    Linux系统架构----Nginx的服务优化一.隐藏版本号在生产环境中,需要隐藏Nginx的版本号,以免泄露Nginx的版本,使得攻击者不能针对特定版本进行攻击查看Nginx的版本有两种方法使用fiddler工具抓取数据包,查看Nginx版本在Centos7上使用使用命令curl-I查看隐藏Nginx版本......
  • linux:services服务器配置
    1.环境准备。配置selinux和防火墙vim/etc/selinux/configSELINUX=permissiveyum-yremovefirewalldip地址基础[root@server~]#ipaddressshow[root@server~]#ipas临时添加IP地址[root@server~]#ipaddressadd192.168.10.1/24deveth......
  • 3.1 RK3399项目开发实录-Linux开发,编译 Linux 固件(物联技术666)
    通过百度网盘分享的文件:嵌入式物联网单片…链接:https://pan.baidu.com/s/1Zi9hj41p_dSskPOhIUnu9Q?pwd=8qo1提取码:8qo1复制这段内容打开「百度网盘APP即可获取」1.编译Linux固件为了方便用户的使用与开发,官方提供了Linux开发的整套SDK,本章详细的说明SDK的具......
  • linux(centos7)通过ckman安装clickhouse并设置自启动
    软件所需安装包:链接:https://pan.baidu.com/s/1MvvS-UoZgn-c0H8pPAavEg?pwd=li9f提取码:li9f--来自百度网盘超级会员V5的分享安装ckman1.使用rpm的方式安装:将rpm包放到服务器,执行命令rpm-ivhckman-2.2.3.x86_64.rpm2.启动:systemctlstartckman3.默认来说ckman是配置了......
  • 常用Linux系统性能分析命令
    Linux系统提供了许多命令来分析系统性能。以下是一些常用的Linux系统性能分析命令:top:实时监视系统的运行状态和进程信息,包括CPU使用率、内存使用情况、进程状态等。实例:直接在终端中输入top,即可实时查看系统进程状态及资源占用情况。htop:类似于top,但提供更友好的界面和交互......