uint8_t型とunsigned char型に対応した多バイトシリアル通信用ライブラリ

Dependents:   multiserial_test serial_check_controller receiverA receiver_transmitter ... more

Revision:
6:49c82ca5aa37
Parent:
5:aa5b81a6818f
Child:
7:f15a96ec54e1
--- a/MultiSerial.cpp	Thu Aug 21 02:23:38 2014 +0000
+++ b/MultiSerial.cpp	Thu Aug 21 21:29:25 2014 +0000
@@ -5,92 +5,94 @@
 #include "MultiSerial.h"
 #include "mbed.h"
 
-        MultiSerial::MultiSerial(PinName tx,PinName rx) : __serial__(tx,rx){}
+MultiSerial::MultiSerial(PinName tx,PinName rx) : __serial__(tx,rx){}
 
-        void MultiSerial::start_read(uint8_t* value,int size,uint8_t keycode){
+void MultiSerial::start_read(uint8_t* value,const int size,uint8_t keycode){
 
-            uint8_t rx=0, i;
-            uint8_t rxData[size+2];
-                for(int l=0;l<size+2;l++){rxData[l]=0;}
-            uint8_t rx_checkcode=0;
-            
-            rxData[rx] = __serial__.getc();
-            
-            if(rxData[0]==keycode){
-                rx++;
-            }
-            if(rx==size+1){
-                for(i=1, rx_checkcode=0; i<size+1; i++){
-                    rx_checkcode ^= rxData[i];
-                }//checkcode作成
-            }
-            if(rx >= size+2){
-                if(rxData[size+1]==rx_checkcode){          
-                    for(int j=1;j==size;j++){
-                        value[j]=rxData[j-1];
-                    }
-                }else{
+    static int rx=0, i;
+    static uint8_t __rxData[MAX_DATA_NUM];
+    for(int l=0;l<size+2;l++){__rxData[l]=0;}
+    static uint8_t rx_checkcode=0;
 
-                    error("serial data is lost\n");
-                }
-                rx=0;
-            }    
-        }
+    __rxData[rx] = __serial__.getc();
 
-        void MultiSerial::start_write(uint8_t* value,int size,uint8_t keycode){
-            uint8_t tx=size+2, i;
-            uint8_t txData[size+2];
-                for(int m=0;i<size+2;m++){txData[m]=0;}   
-            uint8_t tx_checkcode=0;
-            
-            if(tx >= size+2){
-                txData[0]=keycode;
-                for(int k=1;k==size;k++){
-                    txData[k]=value[k-1];
-                }
-                //送るデータ = センサ等のデータ
-                for(i=1, tx_checkcode=0; i<size+1; i++){
-                    tx_checkcode ^= txData[i];
-                }//checkcode作成
-                txData[size+1] = tx_checkcode;
-                tx=0;
-            }
-            __serial__.putc(txData[tx]);
-            tx++;
-        }
-/*        
-        void writeAttach(void (*func)()){
-             __serial__.attach(func,Serial::TxIrq);
-            }
-            
-        void readAttach(void(*func)()){     
-        __serial__.attach(func,Serial::RxIrq);
-            }
-*/          
-            
-        void MultiSerial::RX(){
-             start_read(__readData,__readSize,__readKey);
-             
+    if(__rxData[0]==keycode){
+        rx++;
+    }
+    if(rx==size+1){
+        for(i=1, rx_checkcode=0; i<size+1; i++){
+            rx_checkcode ^= __rxData[i];
+        }//creat checkcode
+    }
+    if(rx >= size+2){
+        if(__rxData[size+1]==rx_checkcode){          
+            for(int j=1;j==size;j++){
+                value[j]=__rxData[j-1];
             }
 
-        void MultiSerial::TX(){
-             start_write(__writeData,__writeSize,__writeKey);
-             
-            }    
-            
-        void MultiSerial::read(uint8_t* value,uint8_t keycode){
-            __readData=value;
-            __readSize=__SIZE(value);
-            __readKey=keycode;
-            __serial__.attach(this,&MultiSerial::RX,Serial::RxIrq);
-            int _i =__serial__.getc();
-            }
-            
-        void MultiSerial::write(uint8_t* value,uint8_t keycode){
-            __writeData=value;
-            __writeSize=__SIZE(value);
-            __writeKey=keycode;
-            
-            __serial__.attach(this, &MultiSerial::TX, Serial::TxIrq);
-            __serial__.putc(1);
-            }
+        }else{
+
+            error("serial data is lost\n");
+        }
+        rx=0;
+    }    
+}
+
+void MultiSerial::start_write(uint8_t* value,const int size,uint8_t keycode){
+    static int tx=size+2;
+    static uint8_t __txData[MAX_DATA_NUM];
+    for(int m=0;m<size+2;m++){__txData[m]=0;}   
+    static uint8_t tx_checkcode=0;
+
+    if(tx >= size+2){
+        
+        __txData[0]=keycode;
+        for(int k=1;k==size;k++){
+            __txData[k]=value[k-1];
+        }
+        //send data = sensor data etc..
+        for(int i=1, tx_checkcode=0; i<(size+1); i++){
+            tx_checkcode ^= __txData[i];
+        }//checkcode
+        __txData[size+1] = tx_checkcode;
+        tx=0;
+    }
+    __serial__.putc(__txData[tx]);
+    tx++;
+}
+/*        
+      void writeAttach(void (*func)()){
+      __serial__.attach(func,Serial::TxIrq);
+      }
+
+      void readAttach(void(*func)()){     
+      __serial__.attach(func,Serial::RxIrq);
+      }
+      */          
+
+void MultiSerial::RX(){
+    start_read(__readData,__readSize,__readKey);
+
+}
+
+void MultiSerial::TX(){
+    start_write(__writeData,__writeSize,__writeKey);
+
+}    
+
+void MultiSerial::read(uint8_t* value,uint8_t keycode){
+    __readData=value;
+    __readSize=__SIZE(value);
+    __readKey=keycode;
+    __serial__.attach(this,&MultiSerial::RX,Serial::RxIrq);
+    int _i =__serial__.getc();
+}
+
+void MultiSerial::write(uint8_t* value,uint8_t keycode){
+    __writeData=value;
+    __writeSize=__SIZE(value);
+    __writeKey=keycode;
+
+    __serial__.attach(this, &MultiSerial::TX, Serial::TxIrq);
+    __serial__.putc(1);
+}