-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscdplus.cpp
More file actions
150 lines (123 loc) · 5.42 KB
/
Copy pathscdplus.cpp
File metadata and controls
150 lines (123 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
using namespace std;
#define BAUDRATE B9600
size_t sendcommand(int fd, const char * buf, const size_t len, char * recv_buf, const size_t recv_len)
{
const size_t write_result = write(fd, buf, len);
if(write_result < 0)
{
fprintf(stderr, "%s: write(2) failed %d %s\n", __PRETTY_FUNCTION__, errno, strerror(errno));
return 0;
}
sleep(1);
const size_t read_result = read(fd, recv_buf, recv_len);
return read_result;
}
int main(int argc, char ** argv)
{
const char * delim = ",";
struct termios m_oldtio,m_newtio;
if(argc < 3)
{
fprintf(stderr, "Usage: %s device baudrate\n", argv[0]);
return -1;
}
const int serial_port = open(argv[1], O_RDWR | O_NOCTTY);
if(serial_port == -1)
{
fprintf(stderr, "%s: error in open(2): %d %s\n", __PRETTY_FUNCTION__, errno, strerror(errno));
return -1;
}
tcgetattr(serial_port, &m_oldtio);
bzero(&m_newtio, sizeof(m_newtio));
m_newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
m_newtio.c_iflag = IGNPAR;
m_newtio.c_oflag = 0;
m_newtio.c_lflag = 0;
m_newtio.c_cc[VTIME] = 0;
m_newtio.c_cc[VMIN] = 1;
tcflush(serial_port, TCIFLUSH);
const int tcset_result = tcsetattr(serial_port, TCSANOW, &m_newtio);
if(tcset_result < 0)
{
fprintf(stderr, "%s: error in tcsetattr(3): %d %s\n", __PRETTY_FUNCTION__, errno, strerror(errno));
return -1;
}
// sendcommand(serial_port, init_command, init_command_length);
/*
cout << "time_before.tv_sec" << delim << "time_before.tv_nsec" << delim << "time_after.tv_sec" << delim << "time_after.tv_nsec" << delim;
cout << "stage1_voltage" << delim << "stage2_voltage" << delim << "stage3_voltage" << delim;
cout << "stage1_length" << delim << "stage2_length" << delim;
cout << "discharge_threshold_low" << delim << "discharge_threshold_high" << delim;
cout << "year" << delim << "month" << delim << "dom" << delim << "hour" << delim << "minute" << delim << "sec" << delim;
cout << "ah100" << delim << "ah" << delim;
cout << "kwh100" << delim << "kwh" << endl << flush;
*/
// while(true)
{
const char * command = "pc.solar.getparams";
const size_t command_length = strlen(command);
const char * expected_response = "solar.pc.sendparams.";
const size_t expected_response_length = strlen(expected_response);
const size_t response_length = 1024; //18+27;
char response[response_length];
struct timespec time_before, time_after;
clock_gettime(CLOCK_REALTIME, &time_before);
const size_t command_result = sendcommand(serial_port, command, command_length, response, response_length);
if(command_result < 1)
return -1;
const char * response_values = &response[expected_response_length];
const unsigned int stage1_voltage = response_values[0] + 79;
const unsigned int stage2_voltage = response_values[1] + 79;
const unsigned int stage3_voltage = response_values[2] + 79;
const unsigned int stage1_length = response_values[3] - 1;
const unsigned int stage2_length = response_values[4] - 1;
const unsigned int dis_lo_thres = response_values[5] + 79;
const unsigned int dis_hi_thres = response_values[6] + 79;
const unsigned int year = ((unsigned int)response_values[14]) + 1999;
const unsigned int month = ((unsigned int)response_values[15]) + 1;
const unsigned int dom = ((unsigned int)response_values[16]) + 1;
const unsigned int hour = ((unsigned int)response_values[17]) + 1;
const unsigned int min = ((unsigned int)response_values[18]) + 1;
const unsigned int sec = ((unsigned int)response_values[19]) + 1;
const unsigned ah100 = ((unsigned int)response_values[20])-1;
const unsigned ah = ((unsigned int)response_values[21])-1;
const unsigned kwh100 = ((unsigned int)response_values[22])-1;
const unsigned kwh = ((unsigned int)response_values[23])-1;
const char * command_getdata = "pc.solar.getdata";
const size_t command_getdata_length = strlen(command_getdata);
clock_gettime(CLOCK_REALTIME, &time_after);
// printf("%d,%d,%d,%d,%s\n", time_before.tv_sec, time_before.tv_nsec, time_after.tv_sec, time_after.tv_nsec, line_buffer);
cout << time_before.tv_sec << delim << time_before.tv_nsec << delim << time_after.tv_sec << delim << time_after.tv_nsec << delim;
cout << stage1_voltage << delim << stage2_voltage << delim << stage3_voltage << delim;
cout << stage1_length << delim << stage2_length << delim;
cout << dis_lo_thres << delim << dis_hi_thres << delim;
cout << year << delim << month << delim << dom << delim << hour << delim << min << delim << sec << delim;
cout << ah100 << delim << ah << delim;
cout << kwh100 << delim << kwh << delim;
for(size_t i=expected_response_length; i<command_result; i++)
cout << (unsigned int)response[i] << ",";
const char * command_getdata_expected_response = "solar.pc.senddata";
const size_t command_getdata_expected_response_length = strlen(command_getdata_expected_response);
const size_t command_getdata_result = sendcommand(serial_port, command_getdata, command_getdata_length, response, response_length);
if(command_getdata_result > 0)
{
for(size_t i=command_getdata_expected_response_length; i<command_getdata_result; i++)
cout << ((unsigned int)response[i]) << ",";
}
cout << endl << flush;
// fflush(stdout);
}
return 0;
};