10 years, 9 months ago.

pc to mbed serial commands

i am using this code to send a command to the pc then recieve a command from the pc to make pin five close a relay. It works as is but i am having a problem if the pc sends the command more than once. if ap6 goes high 2 times then the programs locks till the pc sends 2 co11 commands via serial. is there any way to reset after the function is done. thanks for any help

  1. include "mbed.h" / Pin 6 is the external input to the system. You want to have it go high to trigger the program. Pin 5 will fire after the conditions of the program are met. The program listens for pin 6 to go high and once it does, tells WireReady to play a commercial. When WireReady finishes the commercial, it sends a code back to the mBed to tell say it's done. The mBed compares that code to see if it's correct. The mBed then momentarily closes a relay on pin 5 to return to regular programming.
    • /

DigitalOut ap5 (p5); DigitalIn ap6 (p6); Serial pc(USBTX, USBRX); tx, rx

int main() { char c[5]; set this number to be the length of the string while(1) {

if (ap6) { printf("12x1\n"); if ap6 goes high, this will fire pc.gets(c, 5); this converts the stored characters to a string pc.printf("Received '%s'\n",c); if(strcmp(c,"c011")== 0) compare the string in c to what you're looking for { pc.printf("Firing Pin 5\n"); ap5 = 1; if the compare comes back true, this will fire } LPC_UART0->FCR | 0x06; wait(1.2); wait 1.2 seconds after the compare fires regardless if true ap5 = 0; return ap5 to low state LPC_UART0->FCR | 0x06; } } }

Please use <<code>> and <</code>> around your code (on seperate lines). Now it is pretty much unreadable.

And is the question if the sends the command more than once, or if ap6 goes high twice and the pc needs to send the command more than once?

posted by Erik - 13 Feb 2014

1 Answer

10 years, 9 months ago.

sorry this should be better. the problem is if i send the command multiple times i have to make pin 5 high that many times to clear it out. and if i make pin 5 go high multiple times then i have to send the command that many times to clear it out. i want it to basically reset the buffer after each command, or pin 5 going high. thanks

<<code>> DigitalOut ap5 (p5); <<code>>DigitalIn ap6 (p6); <<code>>Serial pc(USBTX, USBRX); // tx, rx <<code>>int main() <<code>>{ <<code>> char c[5]; // set this number to be the length of the string <<code>> while(1) <<code>> { <<code>> <<code>> if (ap6) <<code>> { <<code>> printf("Hello World!\n"); // if ap6 goes high, this will fire <<code>> pc.gets(c, 5); // this converts the stored characters to a string <<code>> pc.printf("Received '%s'\n",c); <<code>> if(strcmp(c,"c011")== 0) // compare the string in c to what you're looking for <<code>> { <<code>> pc.printf("Firing Pin 5\n"); <<code>> ap5 = 1; // if the compare comes back true, this will fire <<code>> } <<code>> LPC_UART0->FCR | 0x06; <<code>> wait(1.2); // wait 1.2 seconds after the compare fires regardless if true <<code>> ap5 = 0; // return ap5 to low state <<code>> LPC_UART0->FCR | 0x06; <<code>> } <<code>> } } <</code>>

With seperate lines I meant <<code>> on one line, next line all your code, then <</code>> on the last line ;). Not fr each line, this is better, but not alot :P

But still, what exactly is the issue? You say if you send the command multiple times, you have to rise p5 multiple times, so you have to send the command multiple times? That is a bit circular. It only now does something when ap6 is high.

If you want to clear the received serial data, do something like:

while (pc.readable())
  pc.getc();

That empties your serial receive buffer.

posted by Erik - 13 Feb 2014