Takeuchi Issei
/
Serial_Writer-re
Serial_Writerの受信側
Serial_Writer.h
- Committer:
- e2011220
- Date:
- 2021-05-07
- Revision:
- 2:9e785d92712d
- Parent:
- 1:ace5fce2d493
- Child:
- 6:657302a3a908
File content as of revision 2:9e785d92712d:
#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