Software modbus, display, knoppen, bewegingssensor en mbed OS

Dependents:   testSoftware8_alles_display

Revision:
2:ca3d2395b6d3
Parent:
1:92f9d6cec962
Child:
3:4fff62544119
--- a/setup.cpp	Fri Jun 08 06:33:04 2018 +0000
+++ b/setup.cpp	Tue Sep 11 09:34:54 2018 +0000
@@ -1,36 +1,41 @@
 #include "mbed.h"
 #include "setup.h"
+#include "ssd1306.h"
+#include "FlashIAP.h"
 
+// slaveID
 unsigned int slaveID = 0x07;
+// ondersteundende functie codes
+
 unsigned int functionID0x03 = 0x03;
 unsigned int functionID0x05 = 0x05;
 unsigned int functionID0x02 = 0x02;
 
+//?
+int TimerEnd = 0 ; 
 char TxEnablePin = PA_8;
 
-Serial bus(PA_9,PA_10);
-DigitalOut led1(LED1);
+//UART to RS485 bus line & enable of RS485
+Serial bus(PA_9,PA_10); //rx , tx
 DigitalOut activateSN65(PA_8);
-Serial pc(USBTX, USBRX);
 
-// Timers
-Timer intercharacter_timer;
-Timeout interframe_timeout;
+// Timer max message time
+Timer maxMessageTime ; 
 
-
+//?
 unsigned int T1;
 unsigned int T1_5; // inter character time out
 unsigned int T3_5; // frame delay
 unsigned static int buffer = 0;
+
 unsigned int dataReceived[10] ; 
 unsigned int dataSend[10] ; 
-
+unsigned int count = 0, count1 = 0, count2 = 0   ; 
 
 void modbus_configure(long baud, int _slaveID )
 {
-    pc.baud(115200);
     slaveID = _slaveID;
-
+    
     if (baud == 1000000 ){
         T1_5 = 1; 
         T3_5 = 10;
@@ -48,144 +53,170 @@
         T3_5 = 35000000/baud; // 1T * 3.5 = T3.5
     }
     T1 = 10000000/baud;
-}
-int modbus_read2(void)
-{
-
-    //int data ; 
-    //int value = 0 ;
-    //activateSN65 = 0 ;
-
-    if(bus.readable())
-    { 
-        int data = bus.getc();
-        pc.putc(data);
-    }
-    return 0 ; 
+//    maxMessageTime.reset();
 }
 
-int modbus_read(void)
+
+
+
+int modbus_read(dataModbus *allData)
 {
-        activateSN65 = 0 ;
-        if(bus.readable())
-        { 
-            int data = bus.getc();
-            //pc.putc(data);
-            if ( data == slaveID && buffer == 0 ) 
-            {
-               dataReceived[buffer++] = slaveID ;   // slaveID checks out proceed to checking function ID
-            }
-            else if ( data == 0x02 && buffer == 1)
-            {
-                dataReceived[buffer++] = data ;     // FunctionID checks out proceed to receiving data 
-            } 
-            else if ( buffer == 2 || buffer == 3 ) // get data adress high and low order
-            {
-               // adress high and low 
-               if ( buffer == 2 ) 
-               {
-                   dataReceived[buffer++] = data ; 
-                   //pc.putc(buffer);
-               }
-               else
-               {
-                    dataReceived[buffer++] = data ; 
-                    //pc.putc(buffer);
-               }                      
-            }
-            else if ( buffer == 4 || buffer == 5 )
+    activateSN65 = 0 ;
+    static unsigned int maxSizeBuffer = 0 ; 
+    if(bus.readable())
+    {
+//        if( maxMessageTime.read_ms() > (int)180 )
+//        {
+//            buffer = 0 ;  
+//            maxMessageTime.reset();  
+//        }
+        int data = bus.getc(); 
+        if ( data == slaveID && buffer == 0 ) 
+        {
+//            maxMessageTime.start();
+            dataReceived[buffer++] = slaveID ;   // slaveID checks out proceed to checking function ID 
+        }
+        else if ( buffer == 1 )
+        {
+             dataReceived[buffer++] = data ;     // functie code
+             maxSizeBuffer = functionCodeCheckMaxData(data) ;
+        }
+        else if ( ( buffer >= 2 ) && ( buffer <  maxSizeBuffer ) )
+        {
+            dataReceived[buffer++] = data ;     // data 
+        } 
+        else if ( buffer == maxSizeBuffer || buffer == maxSizeBuffer + 1 ) 
+        {
+            if (buffer == maxSizeBuffer )
             {
-               // adress high and low 
-               if ( buffer == 4 ) 
-               {
-                   dataReceived[buffer++] = data ; 
-                   //pc.putc(buffer);
-               }
-               else
-               {
-                    dataReceived[buffer++] = data ;
-                    //pc.putc(buffer);
-               }                      
+                dataReceived[buffer++] = data ;
             }
-            else if ( buffer == 6 || buffer == 7 ) 
-            {
-                if ( buffer == 6 )
-                {
-                    dataReceived[buffer++] = data ;
-                    //pc.putc(buffer);
-                }
-                else 
-                {
-                    dataReceived[buffer++] = data ;  
-                    unsigned long crc = ((dataReceived[buffer - 1] << 8) | dataReceived[buffer - 2]); // combine the crc Low & High bytes 
-                    // check crc
-                    if ( calculate_crc16_Modbus(dataReceived, buffer - 2 ) == crc )
-                    {
-                        buffer = 8  ; 
-                        //pc.putc(buffer);  
-                    }
-                    else 
-                    {
-                        buffer = 0 ; 
-                        //pc.putc(buffer);
-                        // error
-                    }
-                }    
-            }    
             else 
             {
-                buffer = 0 ; 
-                pc.putc(buffer);    
-            } 
+                dataReceived[buffer++] = data ;  
+                //buffer++; 
+                CRCChecking();
+            }    
         }
-        if ( buffer == 8 ) 
+        else 
         {
-            modbus_sendData();
-        }
+            buffer = 0 ;  
+//            maxMessageTime.stop();
+//            maxMessageTime.reset();  
+        } 
+    }
+    if ( buffer == maxSizeBuffer + 3 ) 
+    {
+//        maxMessageTime.stop();
+//        maxMessageTime.reset();
+        return modbus_sendData(allData);
+    }
     
     return 0 ; 
 }
 
-int modbus_sendData(void)
+int modbus_sendData(dataModbus *allData)
 {
-    int i = 0 ; 
+    int i = 0 ;
     int maxData = 0 ; 
-    unsigned static int count = 0 ; 
+    static int temperatuur = 0, beweging = 0 ; //allData->temperatuur ; //0 ; //\, count1 = 0, count2 = 0   ; 
+    temperatuur = allData->temperatuur ; 
+    beweging = allData->beweging ; 
     dataSend[maxData++] = slaveID ; 
     dataSend[maxData++] = dataReceived[1];
-    dataSend[maxData++] = 0x01 ;        // byte count
-    count++; 
-    if ( count > 12 )
+    if ( dataReceived[1] == 0x02 ) 
+    {
+        dataSend[maxData++] = 0x01 ;          // byte count
+        dataSend[maxData++] = 0x01 ;
+    }
+    else if (dataReceived[1] == 0x04 )
+    {
+        if ( dataReceived[3] == 0 )
+        {
+            dataSend[maxData++] = 0x02 ;        // byte count
+            dataSend[maxData++] = 0x00 ; 
+            dataSend[maxData++] = temperatuur ;
+        }
+        else 
+        {
+            dataSend[maxData++] = 0x02 ;        // byte count
+            dataSend[maxData++] = 0x00 ; 
+            dataSend[maxData++] = beweging ;
+        }
+    }
+    else if ( dataReceived[1] == 0x06 )
     {
-        dataSend[maxData++] = 0x00 ;     
-              
-    }
+        dataSend[maxData++] = dataReceived[2] ;     //echo
+        dataSend[maxData++] = dataReceived[3] ;
+        dataSend[maxData++] = dataReceived[4] ;
+        dataSend[maxData++] = dataReceived[5] ;
+        if ( dataReceived[3] == 0x01 ) 
+        {
+            count2 = dataReceived[5] ;
+            if ( count2 == 1 )
+            {
+                allData->RelaisK1 = 1 ;
+            }
+            else 
+            {
+                allData->RelaisK1 = 0 ; 
+            }     
+        }
+        else 
+        {
+            count1 = dataReceived[5] ;
+            if ( count1 == 1 )
+            {
+                allData->RelaisK2 = 1 ; 
+            }
+            else 
+            {
+                allData->RelaisK2 = 0 ; 
+            }
+        }
+        temperatuur = allData->temperatuur ; //count2 * count1 ; 
+        beweging = allData->beweging ;
+    } 
     else 
-    {
-        dataSend[maxData++] = 0x01 ; 
+    { 
+        return 0 ; 
     }
-    if ( count == 25 )
-    { 
-        count = 0 ; 
-    }
-
     uint16_t temp = calculate_crc16_Modbus(dataSend, maxData ) ; 
     dataSend[maxData++] = (uint8_t)temp ; 
     dataSend[maxData++] = ((uint16_t)temp>>8); 
     activateSN65 = 1 ; 
     wait_ms(3);
-    led1 = !led1; 
     for(i = 0 ; i < maxData ; i++ )
     {
         bus.putc(dataSend[i]);
-        pc.putc(dataSend[i]);
         wait_us(750);
     }
+   
     buffer = 0 ; 
     wait_ms(2) ;
     activateSN65 = 0 ; 
     
-    return 0 ; 
+    if ( allData->RelaisK1 == 1 && allData->RelaisK2 == 1 )
+    {
+        return 1 ; 
+    }
+    else if ( allData->RelaisK1 == 0 && allData->RelaisK2 == 1 )
+    {
+        return 2 ;    
+    }
+    else if ( allData->RelaisK1 == 1 && allData->RelaisK2 == 0 )
+    {
+        return 3 ;    
+    }
+    else if ( allData->RelaisK1 == 0 && allData->RelaisK2 == 0 )
+    {
+        return 4 ;    
+    }
+    else 
+    {
+        return 0 ;         
+    }
+
 }
 
 uint16_t update_crc16_reflected(const uint16_t *table, uint16_t crc, unsigned int c )
@@ -219,4 +250,34 @@
     return crc;
 }
 
-
+int functionCodeCheckMaxData(int functionCode)
+{
+    // exclude function code in byte count
+    switch (functionCode) 
+    {
+        // function code 0x01
+        case 0x02 :
+            return 0x04 + 2 ;  
+            //break ;
+        case 0x04 :
+            return 0x04 + 2 ;  
+            //break ; 
+        case 0x06 :
+            return 0x04 + 2 ;
+        default :
+            buffer = 0 ; 
+            return 0 ;  
+    }
+}
+void CRCChecking(void)
+{
+    unsigned long crc = ((dataReceived[buffer - 1] << 8) | dataReceived[buffer - 2]); // combine the crc Low & High bytes 
+    if ( calculate_crc16_Modbus(dataReceived, buffer - 2 ) == crc )
+    {
+        buffer++  ; 
+    }
+    else 
+    {
+        buffer = 0 ; 
+    }  
+}
\ No newline at end of file