help with attach

16 Feb 2010

Can some one point me to an idiots guide to using the attach function with serial input.

What I would like to do is use attach to direct the interupt to a function that sets a flag that the main program uses as a signal to tell it to get data from the serial port. This avoids the need for my code to be always monitoring the serail port.

The code below shows a very much simplified version of what i am trying to do. It works to the point of setting the flag but subsequent attempts to send data over the serial port cause the code to go into a loop in my responftoPC function.

#include "mbed.h"

void respondtoPC() ;
Serial pc(USBTX, USBRX); // tx, rx


DigitalOut myled(LED1);

main()
{
struct tm t;
volatile int test ;
test = 0 ;
pc.attach( respondtoPC) ;
while (1)
{
myled = !myled ;
if ( test == 1 )
{

printf("Enter current date and time:  \n");
printf("YYYY MM DD HH MM SS[enter]  \n");   
scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday
, &t.tm_hour, &t.tm_min, &t.tm_sec);   
// adjust for tm structure required values
t.tm_year = t.tm_year - 1900;
t.tm_mon = t.tm_mon - 1;
// set the time
set_time(mktime(&t));
printf("set time\n");
}
wait(1) ;
}
}

void respondtoPC()
{  
char dummy ;
dummy = pc.getc() ;
test++ ;
return ;
}

 

It seems to me that either I have the major structure wrong or I have my respond code wrong. Of course there may be another explanation.

 

Thanks in advance.

 

Allan

16 Feb 2010 . Edited: 16 Feb 2010

My guess is that the interrupt is generated on both arriving and sent bytes. A bit more verbose documentation would certainly be helpful... Try adding a check for pc.readable() in the handler.

P.S. if you just want to use scanf, I don't think you need to poll the port. scanf() should block and wait for input.

16 Feb 2010

Hi Allan,

I think the problem is around the logic between the respondtoPC() and if(test == 1). You have an interrupt which chomps the characters as you get interrupts, and after the first test will equal 1. Therefore, you'll fall in to your set date function. When you come to scanf, you'll have scanf and your interrupt routine fighting over the characters.

So if, for example, you put pc.attach(NULL) just inside your test == 1 code (so you don't fire interrupts while setting the time), the code actually works fine! Not sure exactly what logic you were after, but that is one solution.

Here is a published version with that change:

Also in this code, note the "volatile int test" should be outside main so it is global, but I guess that is simply a transcription error.

Hope that helps a little.

Simon

16 Feb 2010

Gentlemen,

 

Very many thanks for constructive and useful replies.

The code I showed was a very much cut down version of my project as I hoped to only show what was relevant, aplogies if this slightly misled you.

The reason for this particular structure by the way is that what I am building is a data logger which would normally have reading of the ADC and writing of the results to a file on an SD card within the main loop. For this reason it seemed to me to be inelegant to be always polling the serial port when it will only have this use every few hours. Hence my attempt to use interupts and attach to tell my code when to look at the port and when to ignore it. As this is a learning process for me any comments on my overall approach would be welcome.

 

Allan