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.
serialdata.cpp
00001 #include <mbed.h> 00002 00003 extern Serial pc; 00004 00005 void serialSend(float v) { 00006 pc.printf("aaaa"); // header 00007 unsigned char* data = (unsigned char*) &v; 00008 for (int i = 0; i < 4; i++) { 00009 pc.putc(data[i]); 00010 } 00011 pc.printf("\r\n"); // end of line 00012 } 00013 00014 void serialSendVec(float vec[], int length) { 00015 pc.printf("aaaa"); // header 00016 unsigned char* data = (unsigned char*) vec; 00017 for (int i = 0; i < length*4; i++) { 00018 pc.putc(data[i]); 00019 } 00020 pc.printf("\r\n"); // end of line 00021 00022 } 00023 00024 enum RecvState { 00025 NONE, 00026 HEADER, 00027 DATA, 00028 END 00029 }; 00030 00031 float serialRecv() { 00032 RecvState state = NONE; 00033 int headerStep = 0; 00034 int dataStep = 0; 00035 bool receiving = 1; 00036 char buf[8]; 00037 float v = -10; 00038 while(receiving) { 00039 char c = pc.getc(); 00040 switch(state) { 00041 case NONE: 00042 case HEADER: 00043 if(c == 'a') { 00044 headerStep++; 00045 if(headerStep == 4) { 00046 state = DATA; 00047 dataStep = 0; 00048 v = 0; 00049 } 00050 } else { 00051 headerStep = 0; 00052 } 00053 break; 00054 case DATA: 00055 dataStep += 1; 00056 buf[dataStep-1] = c; 00057 if(dataStep == 4) { 00058 state = END; 00059 v = *(float*)buf; 00060 } 00061 break; 00062 case END: 00063 if(c == '\r' && pc.getc() == '\n') { 00064 return v; 00065 } else { 00066 state = NONE; 00067 headerStep = 0; 00068 } 00069 break; 00070 } 00071 } 00072 return v; 00073 }
Generated on Sat Jul 16 2022 02:23:26 by
1.7.2