how to make software serial event interrupt the main loop

I would like to make the arduino repond to software serial infomtion sent by another module connect to the arduino immediately and in real time even the arduino is executing other parts of the main loop.

Is that possible?

I can think of hard wire mod like link one of the tx rx wire to the interrupt pin of arduino, and might also need other circuit etc. But is there a more elegant way?

I think there are some fancy ways like event triggering framework and even real time OSes which could achieve the same result. But I am thinking of simple way.

The simple way is to re-write your main loop so that it does not use delay() and is able to check serial.available() at least every 10ms or faster.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

Those examples should allow a very responsive program without the complexity of interrupts.

The first problem with interrupts for Serial is that they operate at the character level rather than when a complete message is received. The second problem is that the Arduino Serial library already uses the serial hardware UART interrupt.

...R

MorganS:
The simple way is to re-write your main loop so that it does not use delay() and is able to check serial.available() at least every 10ms or faster.

This might not be as simple as it sounds. If you have some big subfunctions which take quite a long while to run it is hard to put the check serial function in each of them and between every line.

Also to change all the delays to counter style cost something for a big program, for example, the ultimaker firmware takes up 90% of the storage of a mega 2560. What I have learned from school is one should keep tasks as decoupled as possible.

So I think the quick and simple answer to my question is no.

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

Those examples should allow a very responsive program without the complexity of interrupts.

The first problem with interrupts for Serial is that they operate at the character level rather than when a complete message is received. The second problem is that the Arduino Serial library already uses the serial hardware UART interrupt.

...R

Thanks Robin2, I have read your instruction. But from what I learn (correct me if I am wrong), your instruction shows how clocks is distributed and in order to avoid the mcu waiting for incoming message from the serial and without doing anthing else, one should follow your way to handle the information from serial.

But in my case, I actually care about the reverse of the problem, what to do in order to avoid the mcu keep excuting the main loop without reponding to the incoming message from the serial.

I some case, the main loop might have layers of subfunctions and which can be computation intensive (take searching for the 10^6th prime number for example). So far I think to interrupt that kind of execution and respond to incoming message from serial (both hard and soft) is not possible without the mutiple task framework.

I even wonder if it is possible to do that with the multiple task framework, because that kind of framework might have other issues for libraries.

joedodo:
But in my case, I actually care about the reverse of the problem, what to do in order to avoid the mcu keep excuting the main loop without reponding to the incoming message from the serial.

I some case, the main loop might have layers of subfunctions and which can be computation intensive (take searching for the 10^6th prime number for example)

The trick is to write your code so that it does not spend ages on a computation - do it a little bit at a time.

Otherwise you are going to have to disable the Arduino Serial library and write your own code to take its place.

...R

joedodo:
This might not be as simple as it sounds. If you have some big subfunctions which take quite a long while to run it is hard to put the check serial function in each of them and between every line.

Also to change all the delays to counter style cost something for a big program, for example, the ultimaker firmware takes up 90% of the storage of a mega 2560. What I have learned from school is one should keep tasks as decoupled as possible.

So I think the quick and simple answer to my question is no.

The stupid way is to call serialEvent() between every line.

The size of the firmware has nothing to do with the ability to respond to events when they occur. Here is the main loop from one of my largest Arduino projects which has more than 30,000 lines of code and took 5 years to write...

void loop() {
  readSensors();
  doMenu();        //interactive PC/USB menu
  readNMEA();      //for direct commands, eg from bluetooth app
  calculate();
  setOutputs();
  sendNMEA();
  delaySamplePeriod();
}

Yes, this even includes a delay, but it's a custom delay to achieve a consistent loop period and that period is only 4 milliseconds. Usually the loop completes in less than one millisecond.