Example of interrupt-based version of reading serial port

My apologies for not answering this sooner.
As I was working in this project today, it came to my mind that I didn't elaborate what I am doing.

I am using the Arduino to monitor some digital test points on a piece of equipment being tested.
The signals seen by the Arduino are in 10's of milliseconds duration.
When any of the signals are seen by the Arduino, one of the pins is used to trigger a signal generator, to
generate a signal which is fed into the equipment being tested. This must occur within a specified time, which is 10-90 milliseconds.

In order for the Arduino to monitor these signals, each piece of equipment being monitored needs to have 10 or so wires soldered to the testpoints. Those wires are then connected to a 15Pin D connector in order for quick and easy connect/disconnect from the Arduino.
All of this prep work takes time, since we have many pieces of equipment to test.

It was suggested that we use the serial port on the test equipment instead of soldering wires to test points.
The firmware in the equipment would need to be changed to output a "trigger string" to the serial port, at the same places that the test points are written to, instead of turning the test points on & off.

When any of the trigger strings (2-3 characters) are seen by the Arduino, it would react the same way as when the test point signals
are seen.

So- I'm not really interested in the serial data itself as I am when it arrived. The Arduino needs to react quickly,
in order to activate the signal generator within the 10-90 millisecond window.

I hope that explains it clearly enough.

Ah, I see. You need to react within around 50 mS, so you think an interrupt is needed? Well, I set up a test case which just uses loop to see if 3 bytes of serial data has arrived from your test gear. (This 3 bytes is collected by the serial hardware's interrupts). Once that happens we call test_the_thing to do some action. In my case I turned LED 13 on, if I got a T in the first byte, otherwise turned it off.

void test_the_thing ()
  {
  char cmd [3];

  for (byte i = 0; i < sizeof cmd; i++)
    cmd [i] = Serial.read ();
  
  if (cmd [0] == 'T')
    digitalWrite (13, HIGH);
  else
    digitalWrite (13, LOW);
    
  }  // end of  test_the_thing

void setup ()
{
  Serial.begin (115200);
  pinMode (13, OUTPUT);
}  // end of setup

void loop ()
 {
   if (Serial.available () >= 3)
     test_the_thing ();
 }  // end of loop

Timing this, it shows that the reaction time to turn the LED on, after the third byte has arrived, is 19.375 uS. This then is reacting 2580 times as fast as you need it to, so that is plenty fast enough. Plenty of time in fact to be doing other things as well.

[quote author=Nick Gammon link=topic=80635.msg610144#msg610144 date=1322631772]
Timing this, it shows that the reaction time to turn the LED on, after the third byte has arrived, is 19.375 uS. This then is reacting 2580 times as fast as you need it to, so that is plenty fast enough. Plenty of time in fact to be doing other things as well.[/quote]
Nick, you continue to impress me with the steps you take to help people in this forum.

Nice work!

1

Thanks, James and Rob.

He writes some of if not the best explanations too.

Not necessarily in terms of response time. It's just more convenient to code that way.

Timing this, it shows that the reaction time to turn the LED on, after the third byte has arrived, is 19.375 uS. This then is reacting 2580 times as fast as you need it to, so that is plenty fast enough. Plenty of time in fact to be doing other things as well.

Thank you for your thorough analysis.
I like the way that image looks. What kind of logic analyzer are you using?

Saleae "Logic" - 8-port logic analyzer (there is also a 16-port version).

Probably one of the most useful tools I have for working out what is happening in digital circuits.