Takeuchi Issei
/
Serial_Writer-re
Serial_Writerの受信側
Serial_Writer.h
- Committer:
- e2011220
- Date:
- 2021-05-07
- Revision:
- 0:7babc610f171
- Child:
- 1:ace5fce2d493
File content as of revision 0:7babc610f171:
/*--------------------------- Author:Issaimaru ----------------------------*/ #ifndef SERIAL_WRITER #define SERIAL_WRITER #include <mbed.h> class Serial_Writer { public: Serial_Writer(PinName TxPin,PinName RxPin,int baudrate); template <typename T> inline void write(T &send,int delay){ int num=sizeof(send); char buffer[num+2]; for (int i=1,k=0;i<=num;k++){ for(int _bitNum=sizeof(send[0])-1;_bitNum>=0;i++,_bitNum--)buffer[i]=(send[k]>>(8*_bitNum))&0xFF; } buffer[0]='['; buffer[num+1]=']'; for (int p=0;p<sizeof(buffer);p++)_Serial.putc(buffer[p]); wait_ms(delay); } template <typename R> inline int receive(R &get){ int num=sizeof(get); int num_0=sizeof(get[0]); char buffer[num+2]; if (_Serial.readable()){ for(int i=0;i<sizeof(buffer);i++){ buffer[i]=_Serial.getc(); if(buffer[0]!='[')return -1; } if(buffer[num+1]==']'){ for (int s=0;s<(num/num_0);s++)get[s]=0x0; for (int p=1,k=0;p<=num;k++){ for (int _byte=num_0-1;_byte>=0;p++,_byte--)get[k]|=buffer[p]<<(8*_byte); } return 0;//正常終了 }else return -1;//異常終了1 }else return -2;//異常終了2 } private: Serial _Serial; }; #endif