Communication for solid slow control.

Fork of sscm_comm by wimbeaumont Project

Revision:
2:1f9887337a1b
Parent:
1:288db0531b1f
Child:
3:e9053e5e7175
--- a/sscm_comm.cpp	Tue Sep 23 19:40:46 2014 +0000
+++ b/sscm_comm.cpp	Wed Sep 24 09:12:42 2014 +0000
@@ -1,9 +1,14 @@
+#if   defined (TARGET_KL25Z) 
+// in mbed problems to call  strtol
+#include "mbed.h" // for the strtol 
+#else 
+#include <stlib.h>  // for the strtol 
+#endif
 
-#include "mbed.h"
 #include "sscm_comm.h"
 
 
-#define SSCM_LIB_SRC_VERSION "1.01"
+#define SSCM_LIB_SRC_VERSION "1.20"
 
 using namespace sscm_comm;
 
@@ -12,36 +17,56 @@
 int rv=0;
 
 part[0]=input[0];part[1]=input[1];part[2]=0;sc->module = (u8)strtol(part, NULL, 16);
-if( input[3] != SEP ) rv =-1;
+if( input[3] != SEP ) rv =-1;  // tbd check for module range
 else { 
+    u32 ic;//input counter
     part[0]=input[4];part[1]=input[5];part[2]=0;sc->con = (u8)strtol(part, NULL, 16);
-    if( input[6] != SEP ) rv= -2;
+    if( input[6] != SEP ) rv= -2;  // tbd check for connector range 
     else {
         part[0]=input[7];part[1]=input[8];part[2]=0;sc->dev = (ssc_dev)strtol(part, NULL, 16);
         if( input[9] != SEP ) rv= -3;
-        else { 
-         sc->cmd[0]=input[10];sc->cmd[1]=input[11];sc->cmd[2]=input[12];sc->cmd[3]=input[13];
-         if( input[14] != SEP ) rv= -4;   
-         else {      
-            part[0]=input[15];part[1]=input[16];part[2]=0;sc->ch = (u8)strtol(part, NULL, 16);
-            if( input[17] != SEP ) rv= -5;
+        else { ic=10;
+            part[0]=input[ic++];part[1]=input[ic++];part[2]=0;sc->devnr = (u8)strtol(part, NULL, 16);
+            if( input[ic++] != SEP ) rv= -9;
+            
+            else {
+              sc->cmd[0]=input[ic++];sc->cmd[1]=input[ic++];sc->cmd[2]=input[ic++];sc->cmd[3]=input[ic++];
+             if( input[ic++] != SEP ) rv= -4;   
+                else {      
+            part[0]=input[ic++];part[1]=input[ic++];part[2]=0;sc->ch = (u8)strtol(part, NULL, 16);
+            if( input[ic++] != SEP ) rv= -5;
             else{
-                part[0]=input[18];part[1]=input[19];part[2]=input[20];part[3]=input[21];part[4]=0;sc->datain = (u32)strtol(part, NULL, 16);
-                if( input[22] != SEP ) rv= -6;
+                part[0]=input[ic++];part[1]=input[ic++];part[2]=input[ic++];part[3]=input[ic++];part[4]=0;sc->datain = (u32)strtol(part, NULL, 16);
+                if( input[ic++] != SEP ) rv= -6;
             else {
-                part[0]=input[23];part[1]=input[24];part[2]=input[25];part[3]=input[26];part[4]=0;sc->dataout = (u32)strtol(part, NULL, 16);
-                if( input[26] != SEP ) rv= -7;
+                part[0]=input[ic++];part[1]=input[ic++];part[2]=input[ic++];part[3]=input[ic++];part[4]=0;sc->dataout = (u32)strtol(part, NULL, 16);
+                if( input[ic++] != SEP ) rv= -7;
                 else rv=0;
                 
-      }  } }  } }} 
+      }  } }  } }} } 
+      int crr=check_ranges( sc);  if ( crr < 0 ) rv=crr ;
 for( int i=0;i<27;i++) input[i]=0;
 
 return rv;
 }
 
+int  sscm_comm::check_ranges( ssc_cmd* sc ){
+     
+     int rv =1;  // no check done 
+     // devnr check 
+        switch ( sc->dev) {
+         case  ADC   :   if(sc->devnr < 1  ||   sc->devnr > 2 ) rv=-23; break;
+         default     :    rv=1;
+         
+         
+                   }
+     return rv; 
+     
+}     
+
 void sscm_comm::encode_cmd ( char *output , ssc_cmd *sc) {
-    sprintf(output,"%c%02X%c%02X%c%02X%c%c%c%c%c%c%02X%c%04X%c%04X%c%c",
-        CMDSTART,sc->module,SEP,sc->con,SEP,sc->dev,SEP,sc->cmd[0],sc->cmd[1],sc->cmd[2],sc->cmd[3],
+    sprintf(output,"%c%02X%c%02X%c%02X%c%02X%c%c%c%c%c%c%02X%c%04X%c%04X%c%c",
+        CMDSTART,sc->module,SEP,sc->con,SEP,sc->dev,SEP,sc->devnr,SEP,sc->cmd[0],sc->cmd[1],sc->cmd[2],sc->cmd[3],
         SEP,sc->ch,SEP,sc->datain,SEP,sc->dataout,SEP,CMDSTOP);
     }