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.
Dependencies: MODSERIAL SDFileSystem mbed SDShell CRC CommHandler FP LinkedList LogUtil
PCMessaging.h
00001 //these are defines for the messages that are sent from the PC across the USB 00002 //these messages produce reactions on the mbed 00003 //currently there is only a single message -- the trigger command 00004 const unsigned char FIRE_TRIGGER_MSG = 1; 00005 00006 const unsigned short msgBuffSize = 28; 00007 char messageBuffer[msgBuffSize+1]; //the +1 allows for a null-terminated string 00008 00009 //flags to control the mbed actions (in main) in response to the PC command 00010 bool fireTrigger = false; 00011 00012 unsigned char CR = 0x0d; //ASCII Carriage Return 00013 unsigned char LF = 0x0a; //ASCII Line Feed 00014 00015 int totalNumChars = 0; 00016 00017 bool validMessage = false; 00018 00019 /* 00020 void readFromPC() 00021 { 00022 //read all available PC serial bytes and test the sequence for a command string 00023 if (toPC.readable()) 00024 { loopsThroughPCreadable++; //reset to zero when we detect a valid message 00025 //wait_ms(5); 00026 } 00027 while (toPC.readable()) 00028 { 00029 /////////////////////////////////////////////////////////////////////////////////// 00030 //PC-to-mbed and mbed-to-PC message format 00031 // 1) preamble: "mbedmessage " (note the space) 00032 // 2) messageType "trig " (note the space 00033 // 3) 10-char dataValue "0000000000" (ASCI integer representation 00034 //0123456789012345678901234567890123456789 00035 //mbedmessage trig 0000000000 CR/LF 00036 /////////////////////////////////////////////////////////////////////////////////// 00037 00038 // Read in next character 00039 // we get through the main loop at > 500,000 times a sec 00040 unsigned char inChar = 0; 00041 inChar = toPC.getc(); //read char from the USB serial link to the PC 00042 00043 totalNumChars++; 00044 00045 //slide all the characters back by one place -- oldest char in the buffer gets discarded 00046 for (int i=0; i<(msgBuffSize-1); i++) messageBuffer[i] = messageBuffer[i+1]; 00047 //now fill in the last entry to the buffer with the most recent received char 00048 messageBuffer[msgBuffSize-1] = inChar; 00049 00050 //locate the proper message signature in the saved buffer: "mbedmessage " 00051 if (messageBuffer[0] != 'm') continue; 00052 else if( messageBuffer[1] != 'b') continue; 00053 else if( messageBuffer[2] != 'e') continue; 00054 else if( messageBuffer[3] != 'd') continue; 00055 else if( messageBuffer[4] != 'm') continue; 00056 else if( messageBuffer[5] != 'e') continue; 00057 else if( messageBuffer[6] != 's') continue; 00058 else if( messageBuffer[7] != 's') continue; 00059 else if( messageBuffer[8] != 'a') continue; 00060 else if( messageBuffer[9] != 'g') continue; 00061 else if( messageBuffer[10] != 'e') continue; 00062 else if( messageBuffer[11] != ' ') continue; 00063 //else if( messageBuffer[28] != CR) return; 00064 //else if( messageBuffer[29] != LF) return; 00065 00066 //from testing: this always activates on inChar multiple of 28 00067 //if ( inChar == 0x0a ) toPC.printf("%d LF received: %s\n",totalNumChars, messageBuffer); 00068 00069 //if we get here. we have the proper signature of an incoming message 00070 //now test for the message types 00071 00072 //why do we do this?? 00073 //messageBuffer[msgBuffSize]= '/0'; //make the message buffer a null-terminated ASCII string 00074 00075 //if (toPC.writeable()) toPC.printf(messageBuffer); 00076 00077 if ( messageBuffer[12] == 't' && 00078 messageBuffer[13] == 'r' && 00079 messageBuffer[14] == 'i' && 00080 messageBuffer[15] == 'g' ) 00081 { 00082 //we have detected a trigger command message: "trig" 00083 //send back a response to this message 00084 if (toPC.writeable()) 00085 { 00086 toPC.printf("FromMbed %s \n", messageBuffer); 00087 toPC.printf("FromMbed %d %d \n", loopsThroughPCreadable, totalNumChars); 00088 } 00089 fireTrigger = true; 00090 loopsThroughPCreadable = 0; 00091 } 00092 00093 } //end pc.readable 00094 }; 00095 */ 00096 00097 void readFromPC() 00098 { 00099 00100 //if there is no chars to read -- just return; 00101 if (!toPC.readable()) return; 00102 00103 totalNumChars = 0; 00104 00105 if (toPC.writeable()) 00106 { 00107 toPC.printf("FromMbed testing for Trigger \n"); 00108 } 00109 00110 bool timeOutInRead = false; 00111 00112 timeInMessageRead.reset(); 00113 00114 while (totalNumChars < 16) 00115 { 00116 /////////////////////////////////////////////////////////////////////////////////// 00117 //PC-to-mbed and mbed-to-PC message format 00118 // 1) preamble: "mbedmessage " (note the space) 00119 // 2) messageType "trig " (note the space 00120 // 3) 10-char dataValue "0000000000" (ASCI integer representation 00121 //0123456789012345678901234567890123456789 00122 //mbedmessage trig 0000000000 CR/LF 00123 /////////////////////////////////////////////////////////////////////////////////// 00124 00125 // we get through the main loop at > 500,000 times a sec 00126 00127 if ( toPC.readable() ) 00128 { 00129 messageBuffer[totalNumChars] = toPC.getc(); 00130 totalNumChars++; 00131 } 00132 00133 if (timeInMessageRead.read_us() > 500) 00134 { 00135 timeOutInRead = true; 00136 break; 00137 } 00138 } 00139 00140 messageBuffer[28] = '\0'; //make a null-terminated string 00141 00142 if (timeOutInRead) 00143 { 00144 if (toPC.writeable()) 00145 { 00146 toPC.printf("FromMbed TimeOut %s \n", messageBuffer); 00147 } 00148 } 00149 00150 00151 00152 if (messageBuffer[0] == 'm' && 00153 messageBuffer[1] == 'b' && 00154 messageBuffer[2] == 'e' && 00155 messageBuffer[3] == 'd' && 00156 messageBuffer[4] == 'm' && 00157 messageBuffer[5] == 'e' && 00158 messageBuffer[6] == 's' && 00159 messageBuffer[7] == 's' && 00160 messageBuffer[8] == 'a' && 00161 messageBuffer[9] == 'g' && 00162 messageBuffer[10] == 'e' && 00163 messageBuffer[11] == ' ' && 00164 messageBuffer[12] == 't' && 00165 messageBuffer[13] == 'r' && 00166 messageBuffer[14] == 'i' && 00167 messageBuffer[15] == 'g' ) 00168 { 00169 00170 00171 //we have detected a trigger command message: "trig" 00172 //send back a response to this message 00173 if (toPC.writeable()) 00174 { 00175 toPC.printf("FromMbed %s %d\n", messageBuffer, timeInMessageRead.read_us()); 00176 } 00177 00178 for (int i=0; i<=msgBuffSize; i++) messageBuffer[i] = 0; 00179 fireTrigger = true; 00180 } 00181 else 00182 { 00183 toPC.printf("FromMbed badMsg %s \n", messageBuffer); 00184 for (int i=0; i<=msgBuffSize; i++) messageBuffer[i] = 0; 00185 } 00186 }; 00187
Generated on Wed Jul 13 2022 09:05:56 by
1.7.2