我买了像
这样的板,现在我想把温度传感器连接到这个板上。如何用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条答案
按热度按时间i2byvkas1#
您无法使用任何软件执行此操作。DS18B20与您的电路板电气不兼容。传感器使用1线制,集电极开路通信方案,它与该板通常使用的串行协议完全不同。您可能很难对RTS/CTS信号进行一些位碰撞,但您需要电路将它们组合成双向集电极开路通信方案。集电极信号。
uajslkp62#
您可以破解UART,使其与单线协议通信。将Rx连接到Tx,并添加4.7上拉电阻。请参阅maxim的应用笔记:
http://www.maximintegrated.com/en/app-notes/index.mvp/id/214
ruarlubt3#
正如user 3804701所指出的,使用UART接口与1-Wire器件通信确实是可能的,https://www.maximintegrated.com/en/app-notes/index.mvp/id/214的应用笔记包含了实现该功能所需的所有信息,但OP代码需要进行几处修复:
总之,从DS18B20获取温度样本所需的步骤如下:复位、写入0xCC、写入0x 44、等待转换、复位、写入0xCC、写入0xBE、读取9个字节。
https://github.com/dword1511/onewire-over-uart上提供了实现这些功能的示例代码。