Skip to content
Browse files

N

  • Loading branch information...
1 parent 121b3a9 commit 3b258a7d84f816d33c9d115ecb97c2bd718ab342 @thewknd committed
Showing with 321 additions and 0 deletions.
  1. +232 −0 kellerbus.cpp
  2. +89 −0 kellerbus.h
View
232 kellerbus.cpp
@@ -0,0 +1,232 @@
+#include "kellerbus.h"
+
+// kellerbus.cpp
+
+CKellerBus::CKellerBus(HardwareSerial* mComm, unsigned long pBaudrate,unsigned char RTS){
+
+ Baudrate = pBaudrate;
+ Comm = mComm;
+ RTS_PIN = RTS;
+
+ pinMode(RTS_PIN,OUTPUT);
+ digitalWrite(RTS_PIN,LOW);
+
+ cClass = -1;
+ cGroup = -1;
+ cYear = -1;
+ cWeek = -1;
+ cBuffer = -1;
+ cState = -1;
+ ch0 = -1;
+ chP1 = -1;
+ chP2 = -1;
+ chTOB1 = -1;
+ chTOB2 = -1;
+ chT = -1;
+}
+unsigned short CKellerBus::Open()
+{
+ Comm->begin(Baudrate);
+ delay(2);
+
+ return 1;
+}
+int CKellerBus::initDevice(unsigned char Device)
+{
+ int ret;
+ Open();
+ cDevice = Device;
+ TxBuffer[0] = cDevice;
+ TxBuffer[1] = 0b01111111 & 48;
+
+ if(TransferData(2,10) == COMM_OK) {
+ cClass = RxBuffer[2];
+ cGroup = RxBuffer[3];
+ cYear = RxBuffer[4];
+ cWeek = RxBuffer[5];
+ cBuffer = RxBuffer[6];
+ cState = RxBuffer[7];
+ ret = COMM_OK;
+ } else {
+ ret = COMM_ERR_BAD_CRC;
+ }
+ Close();
+ return ret;
+}
+int CKellerBus::initDevice()
+{
+ int ret;
+ Open();
+ TxBuffer[0] = cDevice;
+ TxBuffer[1] = 0b01111111 & 48;
+
+ if(TransferData(2,10) == COMM_OK) {
+ cClass = RxBuffer[2];
+ cGroup = RxBuffer[3];
+ cYear = RxBuffer[4];
+ cWeek = RxBuffer[5];
+ cBuffer = RxBuffer[6];
+ cState = RxBuffer[7];
+ ret = COMM_OK;
+ } else {
+ ret = COMM_ERR_BAD_CRC;
+ }
+ Close();
+ return ret;
+}
+int CKellerBus::readChannel(unsigned char Channel)
+{
+ unsigned char bteArr[4];
+ float value;
+ int ret;
+
+ Open();
+
+ TxBuffer[0] = cDevice;
+ TxBuffer[1] = 0b01111111 & 73;
+ TxBuffer[2] = Channel;
+ if(TransferData(3,9) == COMM_OK) {
+
+ bteArr[0] = RxBuffer[5];
+ bteArr[1] = RxBuffer[4];
+ bteArr[2] = RxBuffer[3];
+ bteArr[3] = RxBuffer[2];
+
+ value = *(float*)(&bteArr[0]);
+
+ switch(Channel) {
+ case 0: ch0 = value; break;
+ case 1: chP1 = value; break;
+ case 2: chP2 = value; break;
+ case 4: chTOB1 = value; break;
+ case 5: chTOB2 = value; break;
+ case 3: chT = value; break;
+ default:break;
+ ret = COMM_OK;
+ }
+
+ } else {
+ ret = COMM_ERR_BAD_CRC;
+ }
+ Close();
+ return ret;
+}
+int CKellerBus::TransferData(unsigned short nTX, unsigned short nRX)
+{
+ unsigned int Crc;
+ unsigned char n, m, x,CRC_H,CRC_L,delay_cnt;
+ unsigned short Repeat = 1;
+ int ret;
+ unsigned long b=0;
+
+ // initialisation CRC16
+ Crc= 0xFFFF;
+ m= nTX;
+ x= 0;
+
+ for(b = 0; b < COMM_TX_MAX + COMM_RX_MAX; b++) {
+ RxBuffer[b] = 0;
+ }
+ b = 0;
+
+ // loop over all bits
+ while(m>0) {
+ Crc^= TxBuffer[x];
+ for(n=0; n<8; n++) {
+ if(Crc&1) {
+ Crc>>= 1;
+ Crc^= 0xA001;
+ } else {
+ Crc>>= 1;
+ }
+ }
+ m--;
+ x++;
+ } // result
+ CRC_H= (Crc>>8)&0xFF;
+ CRC_L= Crc&0xFF;
+ // End CRC16
+
+
+ TxBuffer[nTX] = CRC_H;
+ TxBuffer[nTX+1] = CRC_L;
+
+
+ do {
+ digitalWrite(RTS_PIN,HIGH);
+ delay(3);
+ Comm->write(TxBuffer,nTX + 2);
+ delay(2);
+ digitalWrite(RTS_PIN,LOW);
+
+ delay_cnt = 0;
+
+ do {
+ if (Comm->available() > 0) {
+ RxBuffer[b] = Comm->read();
+ b++;
+ Repeat = 1;
+ }
+ delay(1);
+ delay_cnt += 1;
+ } while(delay_cnt <= 105); // timeout max 105ms
+ Repeat--;
+ delay(2);
+ } while(Repeat >= 1);
+
+ if(b == nRX) {
+ ret = COMM_OK;
+ } else {
+ ret = COMM_ERR_BAD_CRC;
+ }
+
+ return ret;
+}
+
+unsigned char CKellerBus::getClass() {
+ return cClass;
+}
+unsigned char CKellerBus::getGroup() {
+ return cGroup;
+}
+unsigned char CKellerBus::getYear() {
+ return cYear;
+}
+unsigned char CKellerBus::getWeek() {
+ return cWeek;
+}
+unsigned char CKellerBus::getBuffer() {
+ return cBuffer;
+}
+unsigned char CKellerBus::getState() {
+ return cState;
+}
+unsigned char CKellerBus::getDevice() {
+ return cDevice;
+}
+float CKellerBus::getCH0() {
+ return ch0;
+}
+float CKellerBus::getP1() {
+ return chP1;
+}
+float CKellerBus::getP2() {
+ return chP2;
+}
+float CKellerBus::getTOB1() {
+ return chTOB1;
+}
+float CKellerBus::getTOB2() {
+ return chTOB2;
+}
+float CKellerBus::getT() {
+ return chT;
+}
+
+unsigned short CKellerBus::Close()
+{
+ Comm->end();
+ delay(2);
+
+ return 1;
+}
View
89 kellerbus.h
@@ -0,0 +1,89 @@
+
+// kellerbus.h
+
+#include "WProgram.h"
+
+#ifndef _H_KELLERBUS
+#define _H_KELLERBUS
+
+
+#define COMM_ERR_NO_RESPONSE -7
+#define COMM_ERR_BAD_RESPONSE -6
+#define COMM_ERR_BAD_LENGTH -5
+#define COMM_ERR_BAD_CRC -4
+#define COMM_ERR_BAD_ADDR -3
+#define COMM_ERR_BAD_EXCEPTION -2
+#define COMM_ERR_RX_ERROR -1
+
+#define COMM_OK 0
+
+#define COMM_EXCEPTION_1 1
+#define COMM_EXCEPTION_2 2
+#define COMM_EXCEPTION_3 3
+#define COMM_EXCEPTION_32 32
+
+
+#define COMM_TX_MAX 20
+#define COMM_RX_MAX 260
+
+#define MAX_CHANNELS 32
+#define MAX_UNITS 15
+
+class CKellerBus
+{
+ private:
+ HardwareSerial *Comm;
+ unsigned long Baudrate;
+ unsigned char RTS_PIN;
+
+ unsigned char TxBuffer[COMM_TX_MAX];
+ unsigned char RxBuffer[COMM_TX_MAX + COMM_RX_MAX];
+
+ unsigned short Open();
+ unsigned short Close();
+
+ int TransferData(unsigned short nTX, unsigned short nRX);
+
+ unsigned char cClass;
+ unsigned char cGroup;
+ unsigned char cYear;
+ unsigned char cWeek;
+ unsigned char cBuffer;
+ unsigned char cState;
+
+ unsigned char cDevice;
+
+ float ch0;
+ float chP1;
+ float chP2;
+ float chTOB1;
+ float chTOB2;
+ float chT;
+
+
+ public:
+ unsigned short Result;
+ bool echo;
+
+ CKellerBus(HardwareSerial* mComm, unsigned long pBaudrate, unsigned char RTS);
+
+ int initDevice();
+ int initDevice(unsigned char Device);
+ int readChannel(unsigned char Channel);
+
+ unsigned char getClass();
+ unsigned char getGroup();
+ unsigned char getYear();
+ unsigned char getWeek();
+ unsigned char getBuffer();
+ unsigned char getState();
+ unsigned char getDevice();
+
+ float getCH0();
+ float getP1();
+ float getP2();
+ float getTOB1();
+ float getTOB2();
+ float getT();
+};
+#endif

0 comments on commit 3b258a7

Please sign in to comment.
Something went wrong with that request. Please try again.