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