Serialport und Allnet3370

R

realshiva

Guest
Hi leute,
ich hab nen temperatursensor und feuchtigkeitsensor von allnet,
habe einen funktionierenden script hierfür in c auf debian entwickelt.
Nun unter openbsd funzt da garnix. bzw. funktioniert schon nur bsd liefert mir immer nen byte einsen wo keine seien sollten.


habe den code mal angehängt, kann mir jemand sagen welche bsd eigenart ich übersehen habe?

Script ist noch nicht ganz ideal aber erstmal soll er grundsätzlich funktionieren.

Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <time.h>
#define __USE_ISOC99
#include <math.h>

#define CLK_HI(fd) set_bit(fd, TIOCM_RTS)
#define CLK_LO(fd) clear_bit(fd, TIOCM_RTS)
#define DATA_HI(fd) set_bit(fd, TIOCM_DTR)
#define DATA_LO(fd) clear_bit(fd, TIOCM_DTR)
#define DATA_IN(fd) read_state(fd)

#ifndef NAN
#define NAN (0.0/0.0)
#endif 


static void delay(int n)
{
	usleep(n * 1000);
}

static int read_state(int fd)
{
	int ser;
	if(ioctl(fd, TIOCMGET, &ser)) return -1;
	if(ser & TIOCM_CTS) return 1;
	return 0;
}

static inline int clear_bit(int fd, int ser)
{
	if(ioctl(fd, TIOCMBIC, &ser)) return -1;
	return 0;
}

static inline int set_bit(int fd, int ser)
{
	if(ioctl(fd, TIOCMBIS, &ser)) return -1;
	return 0;
}

static int send_byte(int fd, unsigned char b)
{
	int i;
	for(i = 7; i >= 0; i--) {
		if((b >> i) & 1) { if(DATA_HI(fd)) return -1; }
		else { if(DATA_LO(fd)) return -1; }
		delay(1);
		if(CLK_HI(fd)) return -1;
		delay(2);
		if(CLK_LO(fd)) return -1;
		delay(1);
	}
	return 0;
}

static int wait_ack(int fd, unsigned char x)
{
	int rw = 0;
	int e;
	
	delay(1);
	e = DATA_IN(fd);
	if(e == -1) return -1;
	if(!e) rw = 1;
	if(DATA_HI(fd)) return -1;
	if(x != 0) {
		if(CLK_HI(fd)) return -1;
		delay(1);
		if(CLK_LO(fd)) return -1;
		delay(3);
	}
	return rw;
}

static int send_ack(int fd)
{
	if(DATA_LO(fd)) return -1;
	delay(1);
	if(CLK_HI(fd)) return -1;
	delay(1);
	if(CLK_LO(fd)) return -1;
	if(DATA_HI(fd)) return -1;
	delay(3);
	return 0;
}

static int send_nak(int fd)
{
	if(DATA_HI(fd)) return -1;
	delay(1);
	if(CLK_HI(fd)) return -1;
	delay(1);
	if(CLK_LO(fd)) return -1;
	delay(3);
	return 0;
}

static int send_stop(int fd)
{
	if(CLK_HI(fd)) return -1;
	delay(1);
	if(DATA_HI(fd)) return -1;
	delay(3);
	return 0;
}

static int recv_byte(int fd)
{
	unsigned char rw = 0;
	int i;
	int e;
	
	for(i = 7; i >= 0; i--) {
		delay(1);
		if(CLK_HI(fd)) return -1;
		delay(1);
		e = DATA_IN(fd);
		if(e == -1) return -1;
		if(e) rw |= (1 << i);
		delay(1);
		if(CLK_LO(fd)) return -1;
		delay(1);
	}
	return rw;
}

static int send_start(int fd)
{
	if(DATA_HI(fd)) return -1;
	if(CLK_HI(fd)) return -1;
	delay(5);
	if(DATA_LO(fd)) return -1;
	delay(5);
	if(CLK_LO(fd)) return -1;
	delay(5);
	return 0;
}

static int send_start_sht(int fd)
{
	if(DATA_HI(fd)) return -1;
	if(CLK_HI(fd)) return -1;
	delay(10);

	if(CLK_LO(fd)) return -1;;
	delay(5);
	if(CLK_HI(fd)) return -1;
	delay(5);

	if(DATA_LO(fd)) return -1;;
	delay(5);

	if(CLK_LO(fd)) return -1;;
	delay(5);
	if(CLK_HI(fd)) return -1;
	delay(5);

	if(DATA_HI(fd)) return -1;
	delay(5);
	if(CLK_LO(fd)) return -1;;
	delay(5);
	return 0;
}

int thermo_i2c_init(int fd)
{
	if(CLK_HI(fd)) return -1;
	if(DATA_HI(fd)) return -1;
	return 0;
}

int thermo_i2c_read_sht71(int fd, float temp_out, float hum_out)
{
	int foo;
	float temp, hum;
	int a,b;
			
	// Luftfeuchte 
	if(send_start_sht(fd)) return -1;
	if(send_byte(fd, 5)) return -2;
	foo = wait_ack(fd, 1);
	if(foo == -1) return -3;
	delay(125);
	foo = DATA_IN(fd);
	if(foo == -1) return -4;
	foo = foo ? 0 : 1;
	a = recv_byte(fd);
	if(a == -1) return -5;
	if(send_ack(fd)) return -6;
	b = recv_byte(fd);
	if(b == -1) return -7;
	if(send_ack(fd)) return -8;
	if(recv_byte(fd) == -1) return -9;
	if(send_ack(fd)) return -10;
	if(send_stop(fd)) return -11;
	
	hum = a * 256.0 + b;
	hum = -4 + 0.0405 * hum - 0.0000028 * hum * hum;
	printf("%f",hum);

	
	//Temperatur
	if(send_start_sht(fd)) return -12;
	if(send_byte(fd, 3)) return -13;
	foo = wait_ack(fd, 1);
	if(foo == -1) return -14;
	delay(300);
	foo = DATA_IN(fd);
	if(foo == -1) return -15;
	foo = foo ? 0 : 1;
	a = recv_byte(fd);
	if(a == -1) return -16;
	if(send_ack(fd)) return -17;
	b = recv_byte(fd);
	if(b == -1) return -18;
	if(send_ack(fd)) return -19;
	if(recv_byte(fd) == -1) return -20;
	if(send_ack(fd)) return -21;
	if(send_stop(fd)) return -22;
	if(a == 0 && b == 0) foo = 0;
	
	temp = -39.75 + 0.01 * (a * 256 + b) - 2.0;
	//printf("Temp: %f \n",temp);
	return 0;
}



int main(int argc, char **argv){
		int fd;
		fd = open("/dev/tty00", O_RDWR | O_NOCTTY | O_NDELAY);
		if (fd == -1) {
		perror("0");
		return 1;
		}
	
		int line_bits;
		ioctl(fd,TIOCMGET,&line_bits);

		line_bits |= TIOCM_DTR;
		ioctl(fd,TIOCMSET,&line_bits);
	
		line_bits &= ~TIOCM_DTR;
		ioctl(fd,TIOCMSET,&line_bits);
		
		
		float temp,hum;
		thermo_i2c_init(fd); 
	  thermo_i2c_read_sht71(fd,temp,hum);
	 	  	
		
}
 
Zurück
Oben