implementation of parts of the unilynx protocol, for communicating with danfos photovoltaic inverters. Still BETA ! needs byte stuff/unstuff fixed, and some CRC are left out for niw...

Dependencies:   mbed

Revision:
2:de090b60d543
Parent:
1:df4e9da66448
--- a/unilynx.cpp	Tue Aug 28 19:53:42 2012 +0000
+++ b/unilynx.cpp	Tue Sep 04 05:48:43 2012 +0000
@@ -42,19 +42,6 @@
 { 0x7E, 0xFF, 0x03, 0x00, 0x02, 0xFF, 0xFF, 0x00, 0x15, 0xB1, 0x8B, 0x7E };
 
 
-
-/*const char getNodeReqStr[41] =
-{ 0x7E, 0xFF, 0x03, 0x00, 0x02, 0x12, 0x03, 0x1D, 0x13, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-        0xFF, 0xC9, 0x35, 0x7E };
-*/
-/*
-const char getNodeReqStr[41] =
-{ 0x7E, 0xFF, 0x03, 0x00, 0x02, 0x12, 0x03, 0x1D, 0x13, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-        0xFF, 0xC9, 0x35, 0x7E };
-*/
-
 char pingAllStrWoCrc[12] =
 { 0x7E, 0xFF, 0x03, 0x00, 0x02, 0xFF, 0xFF, 0x00, 0x15, 0, 0, 0x7E };
 //{ 0x7E, 0xFF, 0x03, 0x00, 0x02, 0x12, 0x03, 0x00, 0x15, 0, 0, 0x7E };
@@ -72,6 +59,9 @@
 
 char tx[40];
 
+/*
+    Steup RS 485 ON UART1,using DTR for direction control on RS485
+*/
 void rs485init(void)
 {
     rs485.baud(19200);
@@ -94,11 +84,25 @@
 {
     int i;
     i=0;
-    while(i < len) {
-        rs485.putc(*(str +i));
+    while(i < len) 
+    {
+        /* Byte stuff needed ? */
+/*        if((*(str +i)) == 0x7E)
+        {
+            rs485.putc(0x7D);
+            rs485.putc(0x5E);
+        }        
+        else if((*(str +i)) == 0x7D)
+        {
+            rs485.putc(0x7D);
+            rs485.putc(0x5D);
+        }   
+        /* 
+        /* nope, TX as usual */            
+        //else
+            rs485.putc(*(str +i));
+        /* incr. index */
         i++;
-        //TODO byte stuff
-
     }
 }
 
@@ -108,8 +112,28 @@
     i=0;
     while(i < len) {
         (*(str +i)) = rs485.getc();
+
+#if 0
+        /* Byte unstuff needed ? */
+        if(((*(str +i-1)) == 0x7D) &&((*(str +i)) == 0x5E))
+        {
+            /* subtract first bytestuff-byte w 0x7E*/
+            (*(str +i-1)) = 0x7E;
+            /*decrement indenx, so next byte will contain next RX char, 
+            and not byte-stuff leftovers..*/
+            i--;
+            /*and btw. we need read one more char, since byte stuff has added one...*/
+            len++;
+        }        
+        else if(((*(str +i-1)) == 0x7D) &&((*(str +i)) == 0x5D))
+
+        {
+            (*(str +i-1)) = 0x7D;
+            i--;
+            len++;
+        }    
+#endif
         i++;
-        //TODO byte unstuff
         //TODO add timeout
     }
 }
@@ -169,7 +193,7 @@
     getNodeParamStrWoCrc[11] = 0xD0;//((source<<4) | page);
     getNodeParamStrWoCrc[12] = (param_idx);
     getNodeParamStrWoCrc[13] = (param_sub_idx);
-    getNodeParamStrWoCrc[14] = 0x80;//	we want reply (DO_REPLY | IM_REQUEST | TYPE_U16);
+    getNodeParamStrWoCrc[14] = 0x80;//    we want reply (DO_REPLY | IM_REQUEST | TYPE_U16);
     getNodeParamStrWoCrc[15] = 0;
     getNodeParamStrWoCrc[16] = 0;
     getNodeParamStrWoCrc[17] = 0;
@@ -190,11 +214,11 @@
     flags = rx[14];
 
     switch (flags & 0x0F) {
-        case 0x06:	//decode U16
+        case 0x06:    //decode U16
             u16val = ((rx[16]<<8)+rx[15]);
             return (int)u16val;
             break;
-        case 0x07:	//decode U32
+        case 0x07:    //decode U32
             u32val = ((rx[18]<<24)+(rx[17]<<16)+(rx[16]<<8)+rx[15]);
             return u32val;
             break;