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@3:de9ceec558b5, 2021-05-07 (annotated)
- Committer:
- e2011220
- Date:
- Fri May 07 09:36:57 2021 +0000
- Revision:
- 3:de9ceec558b5
- Parent:
- 2:4f7655735c6f
- Child:
- 5:47cf103f9223
fs;
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 | 0:1779b0a23b09 | 4 | class Serial_Writer |
| e2011220 | 0:1779b0a23b09 | 5 | { |
| e2011220 | 0:1779b0a23b09 | 6 | public: |
| e2011220 | 0:1779b0a23b09 | 7 | Serial_Writer(PinName TxPin,PinName RxPin,int baudrate); |
| e2011220 | 0:1779b0a23b09 | 8 | template <typename T> |
| e2011220 | 0:1779b0a23b09 | 9 | inline void write(T &send,int delay){ |
| e2011220 | 0:1779b0a23b09 | 10 | int num=sizeof(send); |
| e2011220 | 1:a9cd302df0c5 | 11 | char buffer[num+2]; |
| e2011220 | 1:a9cd302df0c5 | 12 | for (int i=1,k=0;i<=num;k++){ |
| e2011220 | 0:1779b0a23b09 | 13 | for(int _bitNum=sizeof(send[0])-1;_bitNum>=0;i++,_bitNum--)buffer[i]=(send[k]>>(8*_bitNum))&0xFF; |
| e2011220 | 0:1779b0a23b09 | 14 | } |
| e2011220 | 1:a9cd302df0c5 | 15 | buffer[0]='['; |
| e2011220 | 1:a9cd302df0c5 | 16 | buffer[num+1]=']'; |
| e2011220 | 1:a9cd302df0c5 | 17 | for (int p=0;p<sizeof(buffer);p++)_Serial.putc(buffer[p]); |
| e2011220 | 0:1779b0a23b09 | 18 | wait_ms(delay); |
| e2011220 | 0:1779b0a23b09 | 19 | } |
| e2011220 | 0:1779b0a23b09 | 20 | |
| e2011220 | 0:1779b0a23b09 | 21 | template <typename R> |
| e2011220 | 1:a9cd302df0c5 | 22 | inline int receive(R &get){ |
| e2011220 | 1:a9cd302df0c5 | 23 | int num=sizeof(get); |
| e2011220 | 1:a9cd302df0c5 | 24 | int num_0=sizeof(get[0]); |
| e2011220 | 1:a9cd302df0c5 | 25 | char buffer[num+2]; |
| e2011220 | 1:a9cd302df0c5 | 26 | if (_Serial.readable()){ |
| e2011220 | 1:a9cd302df0c5 | 27 | for(int i=0;i<sizeof(buffer);i++){ |
| e2011220 | 1:a9cd302df0c5 | 28 | buffer[i]=_Serial.getc(); |
| e2011220 | 1:a9cd302df0c5 | 29 | if(buffer[0]!='[')return -1; |
| e2011220 | 1:a9cd302df0c5 | 30 | } |
| e2011220 | 1:a9cd302df0c5 | 31 | if(buffer[num+1]==']'){ |
| e2011220 | 1:a9cd302df0c5 | 32 | for (int s=0;s<(num/num_0);s++)get[s]=0x0; |
| e2011220 | 1:a9cd302df0c5 | 33 | for (int p=1,k=0;p<=num;k++){ |
| e2011220 | 1:a9cd302df0c5 | 34 | for (int _byte=num_0-1;_byte>=0;p++,_byte--)get[k]|=buffer[p]<<(8*_byte); |
| e2011220 | 0:1779b0a23b09 | 35 | } |
| e2011220 | 1:a9cd302df0c5 | 36 | return 0;//正常終了 |
| e2011220 | 2:4f7655735c6f | 37 | }else return -1;//異常終了1(正しく受信していない) |
| e2011220 | 2:4f7655735c6f | 38 | }else return -2;//異常終了2(受信しない) |
| e2011220 | 0:1779b0a23b09 | 39 | } |
| e2011220 | 0:1779b0a23b09 | 40 | |
| e2011220 | 0:1779b0a23b09 | 41 | private: |
| e2011220 | 0:1779b0a23b09 | 42 | Serial _Serial; |
| e2011220 | 0:1779b0a23b09 | 43 | }; |
| e2011220 | 0:1779b0a23b09 | 44 | |
| e2011220 | 0:1779b0a23b09 | 45 | #endif |