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