-
Notifications
You must be signed in to change notification settings - Fork 6
/
jfyconnection.cpp
134 lines (105 loc) · 3.14 KB
/
jfyconnection.cpp
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
#include "jfyconnection.h"
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include "jfycommon.h"
#include "jfyexception.h"
#include "jfyserial.h"
using namespace std;
using namespace Jfy;
Connection::Connection()
: _conn( new Serial ),
_registered( false ),
_sourceAddress( 1 ),
_destinationAddress( 1 )
{
}
Connection::Connection( const string& device )
: _conn( new Serial( device ) ),
_registered( false ),
_sourceAddress( 1 ),
_destinationAddress( 1 )
{
}
Connection::~Connection()
{
close();
delete _conn;
}
bool Connection::init()
{
srand( time( 0 ) );
if ( !_conn->open() )
return false;
try {
// Re-register wtih the inverter
_conn->sendRequest( Data( Jfy::ReRegister, _sourceAddress, 0 ) );
// Get the serial number
Data response = _conn->sendRequestReadResponse( Data( Jfy::OfflineQuery, _sourceAddress, 0 ) );
if ( !response.isValid() )
throw Exception( "Cannot read the serial number from the device." );
_serialNumber = response.toString();
sleep( 1 );
Data request( SendRegisterAddress, _sourceAddress, 0 );
request.addData( _serialNumber );
request.addData( _destinationAddress );
response = _conn->sendRequestReadResponse( request );
if ( !response.isValid() || response.size() == 0 || response.data()[ 0 ] != AckResponseCode )
throw Exception( "Cannot read the registration response." );
_registered = true;
}
catch ( Exception e ) {
cerr << e.what() << endl;
close();
return false;
}
return true;
}
void Connection::close()
{
if ( _registered ) {
Data response = _conn->sendRequestReadResponse( Data( RemoveRegister, _sourceAddress, _destinationAddress ) );
if ( !response.isValid() || response.size() == 0 || response.data()[ 0 ] != AckResponseCode )
cerr << "Could not send the remove registration request to the device." << endl;
}
_conn->close();
_sourceAddress = 0;
_destinationAddress = 0;
_registered = false;
_serialNumber = "";
}
bool Connection::isRegistered() const
{
return _registered;
}
string Connection::serialNumber() const
{
return _serialNumber;
}
unsigned char Connection::registrationAddress() const
{
return _destinationAddress;
}
bool Connection::readNormalInfo( InverterData* data )
{
if ( !isRegistered() ) {
cerr << "Device is not registered." << endl;
return false;
}
Data response = _conn->sendRequestReadResponse( Data( QueryNormalInfo, _sourceAddress, _destinationAddress ) );
if ( !response.isValid() ) {
cerr << "Could not read the information response." << endl;
return false;
}
const unsigned char* buf = response.data();
int size = response.size();
data->temperature = Common::buildShort( buf[ 0 ], buf[ 1 ] ) / 10.0;
data->voltageDc = Common::buildShort( buf[ 4 ], buf[ 5 ] ) / 10.0;
data->energyCurrent = Common::buildShort( buf[ 6 ], buf[ 7 ] ) / 10.0;
data->energyToday = Common::buildShort( buf[ 10 ], buf[ 11 ] ) / 100.0;
data->current = Common::buildShort( buf[ 12 ], buf[ 13 ] ) / 10.0;
data->voltageAc = Common::buildShort( buf[ 14 ], buf[ 15 ] ) / 10.0;
data->frequency = Common::buildShort( buf[ 16 ], buf[ 17 ] ) / 100.0;
data->pvoltageAc = Common::buildShort( buf[ 18 ], buf[ 19 ] ) / 10.0;
return true;
}