Takeuchi Issei
/
Serial_Writer-re
Serial_Writerの受信側
Serial_Writer.h@6:657302a3a908, 2021-05-14 (annotated)
- Committer:
- e2011220
- Date:
- Fri May 14 10:33:36 2021 +0000
- Revision:
- 6:657302a3a908
- Parent:
- 2:9e785d92712d
double and float
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
e2011220 | 0:7babc610f171 | 1 | #ifndef SERIAL_WRITER |
e2011220 | 0:7babc610f171 | 2 | #define SERIAL_WRITER |
e2011220 | 0:7babc610f171 | 3 | #include <mbed.h> |
e2011220 | 6:657302a3a908 | 4 | #include <iostream> |
e2011220 | 6:657302a3a908 | 5 | |
e2011220 | 0:7babc610f171 | 6 | class Serial_Writer |
e2011220 | 0:7babc610f171 | 7 | { |
e2011220 | 0:7babc610f171 | 8 | public: |
e2011220 | 0:7babc610f171 | 9 | Serial_Writer(PinName TxPin,PinName RxPin,int baudrate); |
e2011220 | 6:657302a3a908 | 10 | |
e2011220 | 6:657302a3a908 | 11 | template<typename T,std::size_t N>void write(T (&send)[N],int delay){ |
e2011220 | 6:657302a3a908 | 12 | writer<T,N>::write(send,delay,_Serial); |
e2011220 | 6:657302a3a908 | 13 | } |
e2011220 | 6:657302a3a908 | 14 | |
e2011220 | 6:657302a3a908 | 15 | template <typename R,std::size_t S>int receive(R (&get)[S]){ |
e2011220 | 6:657302a3a908 | 16 | return receiver<R,S>::receive(get,_Serial); |
e2011220 | 0:7babc610f171 | 17 | } |
e2011220 | 0:7babc610f171 | 18 | |
e2011220 | 6:657302a3a908 | 19 | Serial _Serial; |
e2011220 | 0:7babc610f171 | 20 | |
e2011220 | 0:7babc610f171 | 21 | private: |
e2011220 | 6:657302a3a908 | 22 | //--------------------------------------write--------------------------------------// |
e2011220 | 6:657302a3a908 | 23 | template <typename T,std::size_t N> |
e2011220 | 6:657302a3a908 | 24 | struct writer{ |
e2011220 | 6:657302a3a908 | 25 | static void write(T (&send)[N],int delay,Serial &_Serial){ |
e2011220 | 6:657302a3a908 | 26 | int num=sizeof(send); |
e2011220 | 6:657302a3a908 | 27 | char buffer[num+2]; |
e2011220 | 6:657302a3a908 | 28 | for (int i=1,k=0;i<=num;k++){ |
e2011220 | 6:657302a3a908 | 29 | for(int _bitNum=sizeof(send[0])-1;_bitNum>=0;i++,_bitNum--)buffer[i]=(send[k]>>(8*_bitNum))&0xFF; |
e2011220 | 6:657302a3a908 | 30 | } |
e2011220 | 6:657302a3a908 | 31 | buffer[0]='['; |
e2011220 | 6:657302a3a908 | 32 | buffer[num+1]=']'; |
e2011220 | 6:657302a3a908 | 33 | for (int p=0;p<sizeof(buffer);p++)_Serial.putc(buffer[p]); |
e2011220 | 6:657302a3a908 | 34 | wait_ms(delay); |
e2011220 | 6:657302a3a908 | 35 | } |
e2011220 | 6:657302a3a908 | 36 | }; |
e2011220 | 6:657302a3a908 | 37 | |
e2011220 | 6:657302a3a908 | 38 | template<std::size_t N> |
e2011220 | 6:657302a3a908 | 39 | struct writer<double,N>{ |
e2011220 | 6:657302a3a908 | 40 | static void write(double (&send)[N],int delay,Serial &_Serial){ |
e2011220 | 6:657302a3a908 | 41 | int numer=sizeof(send)/sizeof(send[0]); |
e2011220 | 6:657302a3a908 | 42 | int send_c[numer]; |
e2011220 | 6:657302a3a908 | 43 | for(int _n=0;_n<numer;_n++)send_c[_n]=int(send[_n]*100); |
e2011220 | 6:657302a3a908 | 44 | writer<int,N>::write(send_c,delay,_Serial); |
e2011220 | 6:657302a3a908 | 45 | } |
e2011220 | 6:657302a3a908 | 46 | }; |
e2011220 | 6:657302a3a908 | 47 | |
e2011220 | 6:657302a3a908 | 48 | template<std::size_t N> |
e2011220 | 6:657302a3a908 | 49 | struct writer<float,N>{ |
e2011220 | 6:657302a3a908 | 50 | static void write(float (&send)[N],int delay,Serial &_Serial){ |
e2011220 | 6:657302a3a908 | 51 | int numer=sizeof(send)/sizeof(send[0]); |
e2011220 | 6:657302a3a908 | 52 | int send_c[numer]; |
e2011220 | 6:657302a3a908 | 53 | for(int _n=0;_n<numer;_n++)send_c[_n]=int(send[_n]*100); |
e2011220 | 6:657302a3a908 | 54 | writer<int,N>::write(send_c,delay,_Serial); |
e2011220 | 6:657302a3a908 | 55 | } |
e2011220 | 6:657302a3a908 | 56 | }; |
e2011220 | 6:657302a3a908 | 57 | |
e2011220 | 6:657302a3a908 | 58 | //--------------------------------------end--------------------------------------// |
e2011220 | 6:657302a3a908 | 59 | |
e2011220 | 6:657302a3a908 | 60 | //------------------------------------receive------------------------------------// |
e2011220 | 6:657302a3a908 | 61 | |
e2011220 | 6:657302a3a908 | 62 | template <typename R,std::size_t S> |
e2011220 | 6:657302a3a908 | 63 | struct receiver{ |
e2011220 | 6:657302a3a908 | 64 | static int receive(R (&get)[S],Serial &_Serial){ |
e2011220 | 6:657302a3a908 | 65 | int num=sizeof(get); |
e2011220 | 6:657302a3a908 | 66 | int num_0=sizeof(get[0]); |
e2011220 | 6:657302a3a908 | 67 | char buffer[num+2]; |
e2011220 | 6:657302a3a908 | 68 | if (_Serial.readable()){ |
e2011220 | 6:657302a3a908 | 69 | for(int i=0;i<sizeof(buffer);i++){ |
e2011220 | 6:657302a3a908 | 70 | buffer[i]=_Serial.getc(); |
e2011220 | 6:657302a3a908 | 71 | if(buffer[0]!='[')return -1; |
e2011220 | 6:657302a3a908 | 72 | } |
e2011220 | 6:657302a3a908 | 73 | if(buffer[num+1]==']'){ |
e2011220 | 6:657302a3a908 | 74 | for (int s=0;s<(num/num_0);s++)get[s]=0x0; |
e2011220 | 6:657302a3a908 | 75 | for (int p=1,k=0;p<=num;k++){ |
e2011220 | 6:657302a3a908 | 76 | for (int _byte=num_0-1;_byte>=0;p++,_byte--)get[k]|=buffer[p]<<(8*_byte); |
e2011220 | 6:657302a3a908 | 77 | } |
e2011220 | 6:657302a3a908 | 78 | return 0;//正常終了 |
e2011220 | 6:657302a3a908 | 79 | }else return -1;//異常終了1(正しく受信できていない) |
e2011220 | 6:657302a3a908 | 80 | }else return -2;//異常終了2(受信するものがない) |
e2011220 | 6:657302a3a908 | 81 | } |
e2011220 | 6:657302a3a908 | 82 | }; |
e2011220 | 6:657302a3a908 | 83 | |
e2011220 | 6:657302a3a908 | 84 | template <std::size_t S> |
e2011220 | 6:657302a3a908 | 85 | struct receiver<double,S>{ |
e2011220 | 6:657302a3a908 | 86 | static int receive(double (&get)[S],Serial &_Serial){ |
e2011220 | 6:657302a3a908 | 87 | int geter[S]; |
e2011220 | 6:657302a3a908 | 88 | int _return=receiver<int,S>::receive(geter,_Serial); |
e2011220 | 6:657302a3a908 | 89 | for (int _g_=0;_g_<sizeof(geter)/sizeof(geter[0]);_g_++)get[_g_]=double(geter[_g_])/100.0; |
e2011220 | 6:657302a3a908 | 90 | return _return; |
e2011220 | 6:657302a3a908 | 91 | } |
e2011220 | 6:657302a3a908 | 92 | }; |
e2011220 | 6:657302a3a908 | 93 | |
e2011220 | 6:657302a3a908 | 94 | template <std::size_t S> |
e2011220 | 6:657302a3a908 | 95 | struct receiver<float,S>{ |
e2011220 | 6:657302a3a908 | 96 | static int receive(float (&get)[S],Serial &_Serial){ |
e2011220 | 6:657302a3a908 | 97 | int geter[S]; |
e2011220 | 6:657302a3a908 | 98 | int _return=receiver<int,S>::receive(geter,_Serial); |
e2011220 | 6:657302a3a908 | 99 | for (int _g_=0;_g_<sizeof(geter)/sizeof(geter[0]);_g_++)get[_g_]=double(geter[_g_])/100.0; |
e2011220 | 6:657302a3a908 | 100 | return _return; |
e2011220 | 6:657302a3a908 | 101 | } |
e2011220 | 6:657302a3a908 | 102 | }; |
e2011220 | 0:7babc610f171 | 103 | }; |
e2011220 | 0:7babc610f171 | 104 | |
e2011220 | 6:657302a3a908 | 105 | //------------------------------------end------------------------------------// |
e2011220 | 6:657302a3a908 | 106 | |
e2011220 | 6:657302a3a908 | 107 | |
e2011220 | 0:7babc610f171 | 108 | #endif |