我买了这样的板,现在我想将温度传感器连接到这块板上。 如何从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)='@ž@'
######################
你能帮助我吗?
答案 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获取温度样本所需的步骤是:重置,写入0xCC,写入0x44,等待转换,重置,写入0xCC,写入0xBE,读取9个字节。
可在https://github.com/dword1511/onewire-over-uart上找到实现这些内容的示例代码。