如何使用UART板从DS18B20读取温度

时间:2014-01-26 13:01:28

标签: c++ c uart

我买了这样的board板,现在我想将温度传感器连接到这块板上。 如何从c或c ++中读取传感器的温度? 我试着编写一些代码,但它不起作用。我将DS18B20数据电缆直接连接到TXD和RXD引脚。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <cstring>
#include <inttypes.h>
#include <errno.h>

int
set_interface_attribs (int fd, int speed, int parity)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0)
    {
            std::cout<<"error "<<errno<<" from tcgetattr";
            return -1;
    }

    cfsetospeed (&tty, speed);
    cfsetispeed (&tty, speed);

    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
    // disable IGNBRK for mismatched speed tests; otherwise receive break
    // as \000 chars
    tty.c_iflag &= ~IGNBRK;         // ignore break signal
    tty.c_lflag = 0;                // no signaling chars, no echo,
                                    // no canonical processing
    tty.c_oflag = 0;                // no remapping, no delays
    tty.c_cc[VMIN]  = 0;            // read doesn't block
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

    tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                    // enable reading
    tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
    tty.c_cflag |= parity;
    tty.c_cflag &= ~CSTOPB;
    tty.c_cflag &= ~CRTSCTS;

    if (tcsetattr (fd, TCSANOW, &tty) != 0)
    {
        std::cout<<"error "<<errno<<" from tcsetattr";
            return -1;
    }
    return 0;
}

void
set_blocking (int fd, int should_block)
{
    struct termios tty;
    memset (&tty, 0, sizeof tty);
    if (tcgetattr (fd, &tty) != 0)
    {
        std::cout<<"error "<<errno<<" from tggetattr";
            return;
    }
    tty.c_cc[VMIN]  = should_block ? 1 : 0;
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    if (tcsetattr (fd, TCSANOW, &tty) != 0)
            std::cout<<"error "<<errno<<" setting term attributes";
}

int main()
{
        char *portname = "/dev/ttyUSB0";
        int tty_fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
        if (tty_fd < 0)
        {
            std::cout<<"error "<<errno<<" opening "<<portname<<": "<< strerror (errno);
                return -1;
        }

        set_interface_attribs (tty_fd, B9600, 0);  // set speed to 115,200 bps, 8n1 (no parity)
        set_blocking (tty_fd, true);

        unsigned char c = 0xCC;
        if(!write(tty_fd, &c, sizeof(c)))
            std::cout<<"Write error";

        sleep(2);

        unsigned char buffer[8];
        int size;
        if((size = read(tty_fd, &buffer, 8)) < 0)
                std::cout<<"Error";
        else
            std::cout<<"CC("<<size<<")='"<<buffer<<"'";

        std::cout<<"\n";

        c = 0x44;
        if(!write(tty_fd, &c, sizeof(c)))
            std::cout<<"Write error2";

        c = 0xBE;
        if(!write(tty_fd, &c, sizeof(c)))
            std::cout<<"Write error2";

        sleep(2);

        if((size = read(tty_fd, &buffer, 8)) < 0)
                std::cout<<"Error";
        else
            std::cout<<"BE("<<size<<")='"<<buffer<<"'";

        std::cout<<"\n######################\n";

        close(tty_fd);
}

我得到了:

CC(1)='Č@'
BE(2)='@ž@'
######################
CC(1)='Č@'
BE(2)='@ž@'
######################
CC(1)='Č@'
BE(2)='@ž@'
######################

你能帮助我吗?

3 个答案:

答案 0 :(得分:3)

您无法使用任何软件执行此操作。 DS18B20与您所使用的电路板不兼容。该传感器采用1线开路集电极通信方案,完全不同于该板通常使用的串行协议。您可能很难对RTS / CTS信号进行一些比特攻击,但您需要将它们组合成双向开路集电极信号的电路。

答案 1 :(得分:1)

您可以破解UART与1线协议进行通信。 将Rx连接到Tx并添加4.7上拉电阻 请参阅格言中的应用说明:

http://www.maximintegrated.com/en/app-notes/index.mvp/id/214

答案 2 :(得分:0)

正如user3804701所指出的,确实可以使用UART接口与1-Wire器件进行通信,并且https://www.maximintegrated.com/en/app-notes/index.mvp/id/214上的应用笔记包含了使其工作所需的所有信息。 但是OP的代码需要一些修复:

  • 与DS18B20进行的每笔交易均由3个步骤组成:初始化(也称为重置),ROM命令和功能命令,并可选地进行数据交换
  • 通过将UART配置为9600 bps波特率,发送0x0F并接收一个虚拟字节来执行初始化或复位步骤。然后必须将波特率设置为115200 bps才能执行下一步
  • 在复位步骤之后,通过从最低有效位开始,将设置为1的每个数据位写入UART,并将每个设置为0的0x00字节写入UART,将数据发送到DS18B20;例如,要发送0xAB(即10101011),则将序列(FF FF 00 FF 00 FF 00 FF)写入UART;对于写入UART的每个字节,都有一个“返回字节”需要从UART读取并丢弃
  • 通过按照上一个项目符号点中的规则发送0xFF字节从DS18B20接收数据,但不是丢弃从UART读取的“返回字节”,每个数据位的一个字节,从最低有效位开始: 0xFF值表示该位值为1,否则该位值为0;否则为0。例如,从UART读取的序列(00 FF FF 00 FF 00 00 FF)表示DS18B20发送了0x96(即10010110)
  • 如果只有一个DS18B20连接到1-Wire总线,则所有事务都可以使用“ skip ROM”(字节值0xCC)作为ROM命令
  • 要执行的第一个事务是使用功能命令0x44从DS18B20触发温度转换的事务
  • 在等待转换完成(最多可能需要750毫秒)之后,主机可以执行第二次事务以读取DS18B20暂存器存储器(功能命令0xBE);暂存器内存为9个字节长,其中包含温度值

因此,总而言之,从DS18B20获取温度样本所需的步骤是:重置,写入0xCC,写入0x44,等待转换,重置,写入0xCC,写入0xBE,读取9个字节。

可在https://github.com/dword1511/onewire-over-uart上找到实现这些内容的示例代码。