Subversion Repositories heating

Rev

Rev 8 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 * 
16
 * Author: Andreas Theofilu <andreas@theosys.at>
17
 */
18
#include <iostream>
19
#include <fstream>
20
#include <string>
21
#include <iomanip>
22
#include <cstring>
23
#include <cctype>
24
#include <cstddef>
25
#include <cstdio>
26
#include <cstdlib>
27
#include <ctime>
28
#include <fstream>
29
#include <cerrno>
30
#include <unistd.h>
31
#include <syslog.h>
32
#include <termios.h>
33
#include <sys/stat.h>
34
#include <sys/types.h>
35
#include <fcntl.h>
36
#include "config.h"
37
#include "temperature.h"
38
 
39
using namespace std;
40
 
41
temperature::temperature()
42
{
43
struct termios options;
44
 
45
	initialized = false;
46
	fd_serial = -1;
47
 
48
	// Test if serial port is available.
49
	if (access(Configure.serial, R_OK | W_OK))
50
	{
51
		syslog(LOG_DAEMON, "Serial device <%s> is not accessible!", Configure.serial);
52
		return;
53
	}
54
 
55
	// open the serial port for reading and writing
56
	if ((fd_serial = open(Configure.serial, O_RDWR | O_NOCTTY | O_NDELAY)) == -1)
57
	{
58
		syslog(LOG_DAEMON, "Error opening serial device <%s>: %s", Configure.serial, strerror(errno));
59
		return;
60
	}
61
 
62
	tcgetattr(fd_serial, &options);
63
	cfsetispeed(&options, B9600);
64
	cfsetospeed(&options, B9600);
65
	tcsetattr(fd_serial, TCSANOW, &options);
66
	fcntl(fd_serial, F_SETFL, 0);
67
	// TODO: Detecting the menu of the temperature controller and set it to
68
	//       meassure mode.
69
 
70
	initialized = true;
71
}
72
 
73
temperature::~temperature()
74
{
75
	if (fd_serial != -1)
76
		close(fd_serial);
77
 
78
	fd_serial = -1;
79
	initialized = false;
80
}
81
 
82
double temperature::getTemp (int pos)
83
{
84
char hv0[1024];
85
int r;
86
 
87
	if (!pos || fd_serial == -1 || !initialized)
88
		return 100.0;
89
 
90
	if ((r = read(fd_serial, &hv0[0], sizeof(hv0))) <= 0)
91
	{
92
		if (r < 0)
93
			syslog(LOG_DAEMON, "Error reading serial device <%s>: %s", Configure.serial, strerror(errno));
94
 
95
		return 100.0;
96
	}
97
 
98
	buffer.append(hv0);
99
 
100
	if (buffer.find('\x0d') != string::npos)
101
	{
102
		string temps = buffer.substr(buffer.find('\x0d') - 1);
103
		buffer.erase(0, buffer.find('\x0d'));
104
		vector<string> s = split(temps, ' ');
105
		return strtod(s.at(pos - 1).c_str(), NULL);
106
	}
107
 
108
	return 100.0;
109
}
110