Committer:
Wimpie
Date:
Thu Dec 30 18:04:45 2010 +0000
Revision:
1:d37117275b0b
Parent:
0:313c9e399e12
Child:
2:0306beaf5ff7
negative values

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wimpie 0:313c9e399e12 1 /*
Wimpie 0:313c9e399e12 2 Copyright (c) 2010 Wim De Roeve
Wimpie 0:313c9e399e12 3
Wimpie 0:313c9e399e12 4 Permission is hereby granted, free of charge, to any person obtaining a copy
Wimpie 0:313c9e399e12 5 of this software and associated documentation files (the "Software"), to deal
Wimpie 0:313c9e399e12 6 in the Software without restriction, including without limitation the rights
Wimpie 0:313c9e399e12 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Wimpie 0:313c9e399e12 8 copies of the Software, and to permit persons to whom the Software is
Wimpie 0:313c9e399e12 9 furnished to do so, subject to the following conditions:
Wimpie 0:313c9e399e12 10
Wimpie 0:313c9e399e12 11 The above copyright notice and this permission notice shall be included in
Wimpie 0:313c9e399e12 12 all copies or substantial portions of the Software.
Wimpie 0:313c9e399e12 13
Wimpie 0:313c9e399e12 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Wimpie 0:313c9e399e12 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Wimpie 0:313c9e399e12 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Wimpie 0:313c9e399e12 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Wimpie 0:313c9e399e12 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Wimpie 0:313c9e399e12 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Wimpie 0:313c9e399e12 20 THE SOFTWARE.
Wimpie 0:313c9e399e12 21 */
Wimpie 0:313c9e399e12 22
Wimpie 0:313c9e399e12 23 #include "VBus.h"
Wimpie 0:313c9e399e12 24 #include "DebugTrace.h"
Wimpie 0:313c9e399e12 25
Wimpie 1:d37117275b0b 26 #define DEBUG 1
Wimpie 0:313c9e399e12 27
Wimpie 0:313c9e399e12 28 #if DEBUG
Wimpie 1:d37117275b0b 29 DebugTrace pc_VBus(ON, TO_SERIAL);
Wimpie 0:313c9e399e12 30 #endif
Wimpie 0:313c9e399e12 31
Wimpie 0:313c9e399e12 32 static uint8_t CalcCrc(const unsigned char *Buffer, int Offset, int Length);
Wimpie 0:313c9e399e12 33 float CalcTemp(char Byte1, char Byte2);
Wimpie 0:313c9e399e12 34
Wimpie 0:313c9e399e12 35 VBus::VBus(PinName tx, PinName rx)
Wimpie 0:313c9e399e12 36 : _Serial(tx, rx) {
Wimpie 0:313c9e399e12 37 Init(0);
Wimpie 0:313c9e399e12 38 }
Wimpie 0:313c9e399e12 39
Wimpie 0:313c9e399e12 40 void VBus::Init(int addr=0) {
Wimpie 0:313c9e399e12 41
Wimpie 0:313c9e399e12 42 _Serial.baud(9600);
Wimpie 0:313c9e399e12 43 _Serial.format(8,Serial::None,1);
Wimpie 0:313c9e399e12 44 m_timeout=2;
Wimpie 0:313c9e399e12 45
Wimpie 0:313c9e399e12 46 if (addr!=0)
Wimpie 0:313c9e399e12 47 networkaddress=addr;
Wimpie 0:313c9e399e12 48 else
Wimpie 0:313c9e399e12 49 networkaddress=ResolAddress;
Wimpie 0:313c9e399e12 50
Wimpie 0:313c9e399e12 51 }
Wimpie 0:313c9e399e12 52
Wimpie 0:313c9e399e12 53 void VBus::InjectSeptet(unsigned char *Buffer, int Offset, int Length) {
Wimpie 0:313c9e399e12 54 for (unsigned int i = 0; i < Length; i++) {
Wimpie 0:313c9e399e12 55 if (Septet & (1 << i)) {
Wimpie 0:313c9e399e12 56 Buffer [Offset + i] |= 0x80;
Wimpie 0:313c9e399e12 57 }
Wimpie 0:313c9e399e12 58 }
Wimpie 0:313c9e399e12 59 }
Wimpie 0:313c9e399e12 60
Wimpie 0:313c9e399e12 61 bool VBus::Read() {
Wimpie 0:313c9e399e12 62 int F;
Wimpie 0:313c9e399e12 63 char c;
Wimpie 0:313c9e399e12 64 bool start,stop,quit;
Wimpie 0:313c9e399e12 65
Wimpie 0:313c9e399e12 66 start = true;
Wimpie 0:313c9e399e12 67 stop = false;
Wimpie 0:313c9e399e12 68 quit = false;
Wimpie 0:313c9e399e12 69 Bufferlength=0;
Wimpie 0:313c9e399e12 70
Wimpie 0:313c9e399e12 71 m_timer.reset();
Wimpie 0:313c9e399e12 72 m_timer.start();
Wimpie 0:313c9e399e12 73
Wimpie 0:313c9e399e12 74 while ((!stop) and (!quit)) {
Wimpie 0:313c9e399e12 75 if (_Serial.readable()) {
Wimpie 0:313c9e399e12 76 c=_Serial.getc();
Wimpie 0:313c9e399e12 77
Wimpie 0:313c9e399e12 78 if (c == Sync) {
Wimpie 0:313c9e399e12 79 if (start) {
Wimpie 0:313c9e399e12 80 start=false;
Wimpie 0:313c9e399e12 81 Bufferlength=0;
Wimpie 0:313c9e399e12 82 //#if DEBUG
Wimpie 0:313c9e399e12 83 // pc_VBus.traceOut("\r\n");
Wimpie 0:313c9e399e12 84 //#endif
Wimpie 0:313c9e399e12 85 } else {
Wimpie 0:313c9e399e12 86 if (Bufferlength<20) {
Wimpie 0:313c9e399e12 87 m_timer.stop();
Wimpie 0:313c9e399e12 88 m_timer.reset();
Wimpie 0:313c9e399e12 89 m_timer.start();
Wimpie 0:313c9e399e12 90 Bufferlength=0;
Wimpie 0:313c9e399e12 91 } else
Wimpie 0:313c9e399e12 92 stop=true;
Wimpie 0:313c9e399e12 93 }
Wimpie 0:313c9e399e12 94 }
Wimpie 0:313c9e399e12 95 //#if DEBUG
Wimpie 0:313c9e399e12 96 // pc_VBus.traceOut("%X ",c);
Wimpie 0:313c9e399e12 97 //#endif
Wimpie 0:313c9e399e12 98 if ((!start) and (!stop)) {
Wimpie 0:313c9e399e12 99 Buffer[Bufferlength]=c;
Wimpie 0:313c9e399e12 100 Bufferlength++;
Wimpie 0:313c9e399e12 101 }
Wimpie 0:313c9e399e12 102 }
Wimpie 0:313c9e399e12 103 if ((m_timeout > 0) && (m_timer.read() > m_timeout )) {
Wimpie 0:313c9e399e12 104 quit=true;
Wimpie 0:313c9e399e12 105 //#if DEBUG
Wimpie 0:313c9e399e12 106 // pc_VBus.traceOut("Timeout %i",m_timer.read());
Wimpie 0:313c9e399e12 107 //#endif
Wimpie 0:313c9e399e12 108 }
Wimpie 0:313c9e399e12 109 }
Wimpie 0:313c9e399e12 110
Wimpie 0:313c9e399e12 111 m_timer.stop();
Wimpie 0:313c9e399e12 112
Wimpie 0:313c9e399e12 113 #if DEBUG
Wimpie 0:313c9e399e12 114
Wimpie 0:313c9e399e12 115 pc_VBus.traceOut("\r\nBuffer %i \r\n",Bufferlength);
Wimpie 1:d37117275b0b 116 for (int i=0; i<Bufferlength;i++) {
Wimpie 0:313c9e399e12 117 pc_VBus.traceOut("%X ",Buffer[i]);
Wimpie 0:313c9e399e12 118 }
Wimpie 0:313c9e399e12 119 pc_VBus.traceOut("\r\n");
Wimpie 0:313c9e399e12 120
Wimpie 0:313c9e399e12 121 #endif
Wimpie 0:313c9e399e12 122 if (!quit) {
Wimpie 0:313c9e399e12 123 Destination_address = Buffer[2] << 8;
Wimpie 0:313c9e399e12 124 Destination_address |= Buffer[1];
Wimpie 0:313c9e399e12 125 Source_address = Buffer[4] << 8;
Wimpie 0:313c9e399e12 126 Source_address |= Buffer[3];
Wimpie 0:313c9e399e12 127 ProtocolVersion = (Buffer[5]>>4) + (Buffer[5] &(1<<15));
Wimpie 0:313c9e399e12 128
Wimpie 0:313c9e399e12 129 Command = Buffer[7] << 8;
Wimpie 0:313c9e399e12 130 Command |= Buffer[6];
Wimpie 0:313c9e399e12 131 Framecnt = Buffer[8];
Wimpie 0:313c9e399e12 132 Checksum = Buffer[9]; //TODO check if Checksum is OK
Wimpie 0:313c9e399e12 133 #if DEBUG
Wimpie 0:313c9e399e12 134 pc_VBus.traceOut("\r\nDestination %X",Destination_address);
Wimpie 0:313c9e399e12 135 pc_VBus.traceOut("\r\nSource %X",Source_address);
Wimpie 0:313c9e399e12 136 pc_VBus.traceOut("\r\nProtocol version %X",ProtocolVersion);
Wimpie 0:313c9e399e12 137 pc_VBus.traceOut("\r\nCommand %X",Command);
Wimpie 0:313c9e399e12 138 pc_VBus.traceOut("\r\nFramecnt %X", Framecnt);
Wimpie 0:313c9e399e12 139 pc_VBus.traceOut("\r\nChecksum %X\r\n", Checksum);
Wimpie 0:313c9e399e12 140 #endif
Wimpie 0:313c9e399e12 141 // only analyse Commands 0x100 = Packet Contains data for slave
Wimpie 0:313c9e399e12 142 // with correct length = 10 bytes for HEADER and 6 Bytes for each frame
Wimpie 0:313c9e399e12 143
Wimpie 0:313c9e399e12 144 if ((Command==0x0100) and (Bufferlength==10+Framecnt*6)) {
Wimpie 0:313c9e399e12 145
Wimpie 0:313c9e399e12 146 // Frame info for the Resol ConergyDT5 used by IZEN
Wimpie 0:313c9e399e12 147 // check VBusprotocol specification for other products
Wimpie 0:313c9e399e12 148 //
Wimpie 0:313c9e399e12 149 // Each frame has 6 bytes
Wimpie 0:313c9e399e12 150 // byte 1 to 4 are data bytes -> MSB of each bytes
Wimpie 0:313c9e399e12 151 // byte 5 is a septet and contains MSB of bytes 1 to 4
Wimpie 0:313c9e399e12 152 // byte 6 is a checksum
Wimpie 0:313c9e399e12 153 //
Wimpie 0:313c9e399e12 154 //******************* Frame 1 *******************
Wimpie 0:313c9e399e12 155
Wimpie 0:313c9e399e12 156 F=FOffset;
Wimpie 0:313c9e399e12 157
Wimpie 0:313c9e399e12 158 Septet=Buffer[F+FSeptet];
Wimpie 0:313c9e399e12 159 InjectSeptet(Buffer,F,4);
Wimpie 0:313c9e399e12 160
Wimpie 0:313c9e399e12 161 // 'collector1' Temperatur Sensor 1, 15 bits, factor 0.1 in C
Wimpie 0:313c9e399e12 162 Sensor1_temp =CalcTemp(Buffer[F+1], Buffer[F]);
Wimpie 0:313c9e399e12 163 // 'store1' Temperature sensor 2, 15 bits, factor 0.1 in C
Wimpie 0:313c9e399e12 164 Sensor2_temp =CalcTemp(Buffer[F+3], Buffer[F+2]);
Wimpie 0:313c9e399e12 165
Wimpie 0:313c9e399e12 166 //******************* Frame 2 *******************
Wimpie 0:313c9e399e12 167 F=FOffset+FLength;
Wimpie 0:313c9e399e12 168
Wimpie 0:313c9e399e12 169 Septet=Buffer[F+FSeptet];
Wimpie 0:313c9e399e12 170 InjectSeptet(Buffer,F,4);
Wimpie 0:313c9e399e12 171
Wimpie 0:313c9e399e12 172 Sensor3_temp =CalcTemp(Buffer[F+1], Buffer[F]);
Wimpie 0:313c9e399e12 173 Sensor4_temp =CalcTemp(Buffer[F+3], Buffer[F+2]);
Wimpie 0:313c9e399e12 174
Wimpie 0:313c9e399e12 175 //******************* Frame 3 *******************
Wimpie 0:313c9e399e12 176 F=FOffset+FLength*2;
Wimpie 0:313c9e399e12 177
Wimpie 0:313c9e399e12 178 Septet=Buffer[F+FSeptet];
Wimpie 0:313c9e399e12 179 InjectSeptet(Buffer,F,4);
Wimpie 0:313c9e399e12 180
Wimpie 0:313c9e399e12 181 PumpSpeed1 = (Buffer[F] & 0X7F);
Wimpie 0:313c9e399e12 182 PumpSpeed2 = (Buffer[F+1] & 0X7F);
Wimpie 0:313c9e399e12 183 RelaisMask = Buffer[F+2];
Wimpie 0:313c9e399e12 184 ErrorMask = Buffer[F+3];
Wimpie 0:313c9e399e12 185
Wimpie 0:313c9e399e12 186 //******************* Frame 4 *******************
Wimpie 0:313c9e399e12 187 F=FOffset+FLength*3;
Wimpie 0:313c9e399e12 188
Wimpie 0:313c9e399e12 189 Septet=Buffer[F+FSeptet];
Wimpie 0:313c9e399e12 190 InjectSeptet(Buffer,F,4);
Wimpie 0:313c9e399e12 191
Wimpie 0:313c9e399e12 192 SystemTime = Buffer[F+1] << 8 | Buffer[F];
Wimpie 0:313c9e399e12 193 System = Buffer[F+2];
Wimpie 0:313c9e399e12 194
Wimpie 0:313c9e399e12 195 OptionPostPulse = (Buffer[F+3] & 0x01);
Wimpie 0:313c9e399e12 196 OptionThermostat = ((Buffer[F+3] & 0x02) >> 1);
Wimpie 0:313c9e399e12 197 OptionWMZ = ((Buffer[F+3] & 0x04) >> 2);
Wimpie 0:313c9e399e12 198
Wimpie 0:313c9e399e12 199 //******************* Frame 5 *******************
Wimpie 0:313c9e399e12 200 F=FOffset+FLength*4;
Wimpie 0:313c9e399e12 201
Wimpie 0:313c9e399e12 202 Septet=Buffer[F+FSeptet];
Wimpie 0:313c9e399e12 203 InjectSeptet(Buffer,F,4);
Wimpie 0:313c9e399e12 204
Wimpie 0:313c9e399e12 205 OperatingHoursRelais1=Buffer[F+1] << 8 | Buffer[F];
Wimpie 0:313c9e399e12 206 OperatingHoursRelais2=Buffer[F+3] << 8| Buffer[F+2];
Wimpie 0:313c9e399e12 207
Wimpie 0:313c9e399e12 208 ///******************* End of frames ****************
Wimpie 0:313c9e399e12 209
Wimpie 0:313c9e399e12 210 #if DEBUG
Wimpie 0:313c9e399e12 211
Wimpie 0:313c9e399e12 212 pc_VBus.traceOut("\r\nSensor 1 %f", Sensor1_temp);
Wimpie 0:313c9e399e12 213 pc_VBus.traceOut("\r\nSensor 2 %f", Sensor2_temp);
Wimpie 0:313c9e399e12 214 pc_VBus.traceOut("\r\nSensor 3 %f", Sensor3_temp);
Wimpie 0:313c9e399e12 215 pc_VBus.traceOut("\r\nSensor 4 %f", Sensor4_temp);
Wimpie 0:313c9e399e12 216 pc_VBus.traceOut("\r\nPumpSpeed1 %i", PumpSpeed1);
Wimpie 0:313c9e399e12 217 pc_VBus.traceOut("\r\nPumpSpeed2 %i", PumpSpeed2);
Wimpie 0:313c9e399e12 218 pc_VBus.traceOut("\r\nRelaismask %i", RelaisMask);
Wimpie 0:313c9e399e12 219 pc_VBus.traceOut("\r\nErrorMask %i", ErrorMask);
Wimpie 0:313c9e399e12 220 pc_VBus.traceOut("\r\nSystemTime %i", SystemTime);
Wimpie 0:313c9e399e12 221 pc_VBus.traceOut("\r\nSystem %i", System);
Wimpie 0:313c9e399e12 222 pc_VBus.traceOut("\r\nOptionPostPulse %i", OptionPostPulse);
Wimpie 0:313c9e399e12 223 pc_VBus.traceOut("\r\nOptionThermostat %i", OptionThermostat);
Wimpie 0:313c9e399e12 224 pc_VBus.traceOut("\r\nOptionWMZ %i", OptionWMZ);
Wimpie 0:313c9e399e12 225 pc_VBus.traceOut("\r\nOperatingHoursRelais1 %i", OperatingHoursRelais1);
Wimpie 0:313c9e399e12 226 pc_VBus.traceOut("\r\nOperatingHoursRelais2 %i", OperatingHoursRelais2);
Wimpie 0:313c9e399e12 227
Wimpie 0:313c9e399e12 228 #endif
Wimpie 0:313c9e399e12 229 }
Wimpie 0:313c9e399e12 230 }
Wimpie 0:313c9e399e12 231
Wimpie 0:313c9e399e12 232 return !quit;
Wimpie 0:313c9e399e12 233 }
Wimpie 0:313c9e399e12 234
Wimpie 0:313c9e399e12 235 void VBus::make_Header (unsigned int DAdr,unsigned int SAdr,unsigned char Ver,unsigned int Cmd,unsigned char AFrames) {
Wimpie 0:313c9e399e12 236 unsigned char Buffer[10]={Sync,(uint8_t)DAdr,DAdr>>8,(uint8_t)SAdr,SAdr>>8,Ver,(uint8_t)Cmd,Cmd>>8,AFrames};
Wimpie 0:313c9e399e12 237 Buffer[9]=CalcCrc(Buffer,1,8);
Wimpie 0:313c9e399e12 238 // pc_VBus.traceOut("\r\nmake_Header\r\n");
Wimpie 0:313c9e399e12 239 for (unsigned bo=0;bo<=9;bo++) {
Wimpie 0:313c9e399e12 240 _Serial.putc(Buffer[bo]);
Wimpie 0:313c9e399e12 241 }
Wimpie 0:313c9e399e12 242 // pc_VBus.traceOut("\r\n");
Wimpie 0:313c9e399e12 243 }
Wimpie 0:313c9e399e12 244
Wimpie 0:313c9e399e12 245 static uint8_t CalcCrc(const unsigned char *Buffer, int Offset, int Length) {
Wimpie 0:313c9e399e12 246 static uint8_t Crc= 0x7F;
Wimpie 0:313c9e399e12 247
Wimpie 0:313c9e399e12 248 for (unsigned int i = 0; i < Length; i++) {
Wimpie 0:313c9e399e12 249 Crc = (Crc - Buffer [Offset + i]) & 0x7F;
Wimpie 0:313c9e399e12 250 }
Wimpie 0:313c9e399e12 251 return Crc;
Wimpie 0:313c9e399e12 252 }
Wimpie 0:313c9e399e12 253
Wimpie 0:313c9e399e12 254 float CalcTemp(char Byte1, char Byte2) {
Wimpie 0:313c9e399e12 255 int v;
Wimpie 0:313c9e399e12 256 v = Byte1 << 8 | Byte2;
Wimpie 1:d37117275b0b 257
Wimpie 1:d37117275b0b 258 if (Byte1 == 0xFF) // negatief value
Wimpie 1:d37117275b0b 259 v = v - 0x10000;
Wimpie 1:d37117275b0b 260
Wimpie 0:313c9e399e12 261 if (v==SENSORNOTCONNECTED)
Wimpie 0:313c9e399e12 262 v=0;
Wimpie 0:313c9e399e12 263 return (float)((float) v * 0.1);
Wimpie 0:313c9e399e12 264 }