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@6:ef64b02d2ff5, 2021-05-20 (annotated)
- Committer:
- e2011220
- Date:
- Thu May 20 15:23:17 2021 +0000
- Revision:
- 6:ef64b02d2ff5
- Parent:
- 5:47cf103f9223
plastic
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 | 6:ef64b02d2ff5 | 9 | Serial_Writer(PinName txPin,PinName rxPin,int baudrate); |
| e2011220 | 5:47cf103f9223 | 10 | |
| e2011220 | 6:ef64b02d2ff5 | 11 | int readable(); |
| e2011220 | 6:ef64b02d2ff5 | 12 | void attach(void (*fptr)()); |
| e2011220 | 6:ef64b02d2ff5 | 13 | |
| e2011220 | 6:ef64b02d2ff5 | 14 | template<typename T,std::size_t N>void write(T (&send)[N]){ |
| e2011220 | 6:ef64b02d2ff5 | 15 | writer<T,N>::write(send,_Serial); |
| e2011220 | 5:47cf103f9223 | 16 | } |
| e2011220 | 5:47cf103f9223 | 17 | |
| e2011220 | 6:ef64b02d2ff5 | 18 | template <typename T,std::size_t N>int receive(T (&get)[N]){ |
| e2011220 | 6:ef64b02d2ff5 | 19 | return receiver<T,N>::receive(get,_Serial); |
| e2011220 | 0:1779b0a23b09 | 20 | } |
| e2011220 | 0:1779b0a23b09 | 21 | |
| e2011220 | 5:47cf103f9223 | 22 | Serial _Serial; |
| e2011220 | 0:1779b0a23b09 | 23 | |
| e2011220 | 0:1779b0a23b09 | 24 | private: |
| e2011220 | 5:47cf103f9223 | 25 | //--------------------------------------write--------------------------------------// |
| e2011220 | 6:ef64b02d2ff5 | 26 | |
| e2011220 | 6:ef64b02d2ff5 | 27 | //下記の型以外の型のときの処理 |
| e2011220 | 5:47cf103f9223 | 28 | template <typename T,std::size_t N> |
| e2011220 | 5:47cf103f9223 | 29 | struct writer{ |
| e2011220 | 6:ef64b02d2ff5 | 30 | static void write(T (&send)[N],Serial &_Serial){ |
| e2011220 | 6:ef64b02d2ff5 | 31 | const int byte=sizeof(send); |
| e2011220 | 6:ef64b02d2ff5 | 32 | char buffer[byte+2]; |
| e2011220 | 6:ef64b02d2ff5 | 33 | for (int i=1,k=0;i<=byte;k++){ |
| e2011220 | 6:ef64b02d2ff5 | 34 | for(int byte_shift=sizeof(send[0])-1;byte_shift>=0;i++,byte_shift--)buffer[i]=(send[k]>>(8*byte_shift))&0xFF; |
| e2011220 | 5:47cf103f9223 | 35 | } |
| e2011220 | 5:47cf103f9223 | 36 | buffer[0]='['; |
| e2011220 | 6:ef64b02d2ff5 | 37 | buffer[byte+1]=']'; |
| e2011220 | 5:47cf103f9223 | 38 | for (int p=0;p<sizeof(buffer);p++)_Serial.putc(buffer[p]); |
| e2011220 | 5:47cf103f9223 | 39 | } |
| e2011220 | 5:47cf103f9223 | 40 | }; |
| e2011220 | 5:47cf103f9223 | 41 | |
| e2011220 | 6:ef64b02d2ff5 | 42 | //doubleのときの処理 |
| e2011220 | 5:47cf103f9223 | 43 | template<std::size_t N> |
| e2011220 | 5:47cf103f9223 | 44 | struct writer<double,N>{ |
| e2011220 | 6:ef64b02d2ff5 | 45 | static void write(double (&send)[N],Serial &_Serial){ |
| e2011220 | 6:ef64b02d2ff5 | 46 | const int arrays_num=sizeof(send)/sizeof(send[0]); |
| e2011220 | 6:ef64b02d2ff5 | 47 | long long send_long[arrays_num]; |
| e2011220 | 6:ef64b02d2ff5 | 48 | for(int j=0;j<arrays_num;j++)send_long[j]=send[j]*10000000;//小数点以下7桁、最大11桁(指数部4095)まで対応(doubleの有効桁数を参照) |
| e2011220 | 6:ef64b02d2ff5 | 49 | writer<long long,N>::write(send_long,_Serial); |
| e2011220 | 5:47cf103f9223 | 50 | } |
| e2011220 | 5:47cf103f9223 | 51 | }; |
| e2011220 | 5:47cf103f9223 | 52 | |
| e2011220 | 6:ef64b02d2ff5 | 53 | //floatのときの処理 |
| e2011220 | 5:47cf103f9223 | 54 | template<std::size_t N> |
| e2011220 | 5:47cf103f9223 | 55 | struct writer<float,N>{ |
| e2011220 | 6:ef64b02d2ff5 | 56 | static void write(float (&send)[N],Serial &_Serial){ |
| e2011220 | 6:ef64b02d2ff5 | 57 | const int arrays_num=sizeof(send)/sizeof(send[0]); |
| e2011220 | 6:ef64b02d2ff5 | 58 | long long send_long[arrays_num]; |
| e2011220 | 6:ef64b02d2ff5 | 59 | for(int j=0;j<arrays_num;j++)send_long[j]=send[j]*10000000;//小数点以下7桁、最大10桁(指数部255)まで対応(floatの有効桁数を参照) |
| e2011220 | 6:ef64b02d2ff5 | 60 | writer<long long,N>::write(send_long,_Serial); |
| e2011220 | 5:47cf103f9223 | 61 | } |
| e2011220 | 5:47cf103f9223 | 62 | }; |
| e2011220 | 5:47cf103f9223 | 63 | |
| e2011220 | 6:ef64b02d2ff5 | 64 | |
| e2011220 | 5:47cf103f9223 | 65 | //--------------------------------------end--------------------------------------// |
| e2011220 | 5:47cf103f9223 | 66 | |
| e2011220 | 5:47cf103f9223 | 67 | //------------------------------------receive------------------------------------// |
| e2011220 | 5:47cf103f9223 | 68 | |
| e2011220 | 6:ef64b02d2ff5 | 69 | //下記の型以外の型のときの処理 |
| e2011220 | 6:ef64b02d2ff5 | 70 | template <typename T,std::size_t N> |
| e2011220 | 5:47cf103f9223 | 71 | struct receiver{ |
| e2011220 | 6:ef64b02d2ff5 | 72 | static int receive(T (&get)[N],Serial &_Serial){ |
| e2011220 | 6:ef64b02d2ff5 | 73 | const int byte=sizeof(get); |
| e2011220 | 6:ef64b02d2ff5 | 74 | const int byte_0=sizeof(get[0]); |
| e2011220 | 6:ef64b02d2ff5 | 75 | char buffer[byte+2]; |
| e2011220 | 6:ef64b02d2ff5 | 76 | for(int i=0;i<sizeof(buffer);i++){ |
| e2011220 | 6:ef64b02d2ff5 | 77 | buffer[i]=_Serial.getc(); |
| e2011220 | 6:ef64b02d2ff5 | 78 | if(buffer[0]!='[')return -1; |
| e2011220 | 6:ef64b02d2ff5 | 79 | } |
| e2011220 | 6:ef64b02d2ff5 | 80 | if(buffer[byte+1]==']'){ |
| e2011220 | 6:ef64b02d2ff5 | 81 | for (int j=0;j<(byte/byte_0);j++)get[j]=0x0; |
| e2011220 | 6:ef64b02d2ff5 | 82 | for (int p=1,k=0;p<=byte;k++){ |
| e2011220 | 6:ef64b02d2ff5 | 83 | for (int byte_shift=byte_0-1;byte_shift>=0;p++,byte_shift--)get[k]|=buffer[p]<<(8*byte_shift); |
| e2011220 | 5:47cf103f9223 | 84 | } |
| e2011220 | 6:ef64b02d2ff5 | 85 | return 0;//正常終了 |
| e2011220 | 6:ef64b02d2ff5 | 86 | }else return -1;//異常終了1(正しく受信できていない) |
| e2011220 | 5:47cf103f9223 | 87 | } |
| e2011220 | 5:47cf103f9223 | 88 | }; |
| e2011220 | 5:47cf103f9223 | 89 | |
| e2011220 | 6:ef64b02d2ff5 | 90 | //doubleのときの処理 |
| e2011220 | 6:ef64b02d2ff5 | 91 | template <std::size_t N> |
| e2011220 | 6:ef64b02d2ff5 | 92 | struct receiver<double,N>{ |
| e2011220 | 6:ef64b02d2ff5 | 93 | static int receive(double (&get)[N],Serial &_Serial){ |
| e2011220 | 6:ef64b02d2ff5 | 94 | long long arrays[N]; |
| e2011220 | 6:ef64b02d2ff5 | 95 | int _return=receiver<long long,N>::receive(arrays,_Serial); |
| e2011220 | 6:ef64b02d2ff5 | 96 | for (int l=0;l<sizeof(arrays)/sizeof(arrays[0]);l++)get[l]=double(arrays[l])/10000000.0; |
| e2011220 | 5:47cf103f9223 | 97 | return _return; |
| e2011220 | 5:47cf103f9223 | 98 | } |
| e2011220 | 5:47cf103f9223 | 99 | }; |
| e2011220 | 5:47cf103f9223 | 100 | |
| e2011220 | 6:ef64b02d2ff5 | 101 | //floatのときの処理 |
| e2011220 | 6:ef64b02d2ff5 | 102 | template <std::size_t N> |
| e2011220 | 6:ef64b02d2ff5 | 103 | struct receiver<float,N>{ |
| e2011220 | 6:ef64b02d2ff5 | 104 | static int receive(float (&get)[N],Serial &_Serial){ |
| e2011220 | 6:ef64b02d2ff5 | 105 | long long arrays[N]; |
| e2011220 | 6:ef64b02d2ff5 | 106 | int _return=receiver<long long,N>::receive(arrays,_Serial); |
| e2011220 | 6:ef64b02d2ff5 | 107 | for (int l=0;l<sizeof(arrays)/sizeof(arrays[0]);l++)get[l]=double(arrays[l])/10000000.0; |
| e2011220 | 5:47cf103f9223 | 108 | return _return; |
| e2011220 | 5:47cf103f9223 | 109 | } |
| e2011220 | 5:47cf103f9223 | 110 | }; |
| e2011220 | 0:1779b0a23b09 | 111 | }; |
| e2011220 | 0:1779b0a23b09 | 112 | |
| e2011220 | 5:47cf103f9223 | 113 | //------------------------------------end------------------------------------// |
| e2011220 | 5:47cf103f9223 | 114 | |
| e2011220 | 5:47cf103f9223 | 115 | |
| e2011220 | 0:1779b0a23b09 | 116 | #endif |