Takeuchi Issei
/
Serial_Writer-re
Serial_Writerの受信側
Serial_Writer.h@0:7babc610f171, 2021-05-07 (annotated)
- Committer:
- e2011220
- Date:
- Fri May 07 08:56:25 2021 +0000
- Revision:
- 0:7babc610f171
- Child:
- 1:ace5fce2d493
fs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
e2011220 | 0:7babc610f171 | 1 | /*--------------------------- |
e2011220 | 0:7babc610f171 | 2 | |
e2011220 | 0:7babc610f171 | 3 | Author:Issaimaru |
e2011220 | 0:7babc610f171 | 4 | |
e2011220 | 0:7babc610f171 | 5 | ----------------------------*/ |
e2011220 | 0:7babc610f171 | 6 | #ifndef SERIAL_WRITER |
e2011220 | 0:7babc610f171 | 7 | #define SERIAL_WRITER |
e2011220 | 0:7babc610f171 | 8 | #include <mbed.h> |
e2011220 | 0:7babc610f171 | 9 | class Serial_Writer |
e2011220 | 0:7babc610f171 | 10 | { |
e2011220 | 0:7babc610f171 | 11 | public: |
e2011220 | 0:7babc610f171 | 12 | Serial_Writer(PinName TxPin,PinName RxPin,int baudrate); |
e2011220 | 0:7babc610f171 | 13 | template <typename T> |
e2011220 | 0:7babc610f171 | 14 | inline void write(T &send,int delay){ |
e2011220 | 0:7babc610f171 | 15 | int num=sizeof(send); |
e2011220 | 0:7babc610f171 | 16 | char buffer[num+2]; |
e2011220 | 0:7babc610f171 | 17 | for (int i=1,k=0;i<=num;k++){ |
e2011220 | 0:7babc610f171 | 18 | for(int _bitNum=sizeof(send[0])-1;_bitNum>=0;i++,_bitNum--)buffer[i]=(send[k]>>(8*_bitNum))&0xFF; |
e2011220 | 0:7babc610f171 | 19 | } |
e2011220 | 0:7babc610f171 | 20 | buffer[0]='['; |
e2011220 | 0:7babc610f171 | 21 | buffer[num+1]=']'; |
e2011220 | 0:7babc610f171 | 22 | for (int p=0;p<sizeof(buffer);p++)_Serial.putc(buffer[p]); |
e2011220 | 0:7babc610f171 | 23 | wait_ms(delay); |
e2011220 | 0:7babc610f171 | 24 | } |
e2011220 | 0:7babc610f171 | 25 | |
e2011220 | 0:7babc610f171 | 26 | template <typename R> |
e2011220 | 0:7babc610f171 | 27 | inline int receive(R &get){ |
e2011220 | 0:7babc610f171 | 28 | int num=sizeof(get); |
e2011220 | 0:7babc610f171 | 29 | int num_0=sizeof(get[0]); |
e2011220 | 0:7babc610f171 | 30 | char buffer[num+2]; |
e2011220 | 0:7babc610f171 | 31 | if (_Serial.readable()){ |
e2011220 | 0:7babc610f171 | 32 | for(int i=0;i<sizeof(buffer);i++){ |
e2011220 | 0:7babc610f171 | 33 | buffer[i]=_Serial.getc(); |
e2011220 | 0:7babc610f171 | 34 | if(buffer[0]!='[')return -1; |
e2011220 | 0:7babc610f171 | 35 | } |
e2011220 | 0:7babc610f171 | 36 | if(buffer[num+1]==']'){ |
e2011220 | 0:7babc610f171 | 37 | for (int s=0;s<(num/num_0);s++)get[s]=0x0; |
e2011220 | 0:7babc610f171 | 38 | for (int p=1,k=0;p<=num;k++){ |
e2011220 | 0:7babc610f171 | 39 | for (int _byte=num_0-1;_byte>=0;p++,_byte--)get[k]|=buffer[p]<<(8*_byte); |
e2011220 | 0:7babc610f171 | 40 | } |
e2011220 | 0:7babc610f171 | 41 | return 0;//正常終了 |
e2011220 | 0:7babc610f171 | 42 | }else return -1;//異常終了1 |
e2011220 | 0:7babc610f171 | 43 | }else return -2;//異常終了2 |
e2011220 | 0:7babc610f171 | 44 | } |
e2011220 | 0:7babc610f171 | 45 | |
e2011220 | 0:7babc610f171 | 46 | private: |
e2011220 | 0:7babc610f171 | 47 | Serial _Serial; |
e2011220 | 0:7babc610f171 | 48 | }; |
e2011220 | 0:7babc610f171 | 49 | |
e2011220 | 0:7babc610f171 | 50 | #endif |