0

I want to perform a serial read using software serial with Arduino. I am looping for my normal tasks, but whenever something is received on the software serial pins I want to perform a read operation and based on the character received, I want to run a function in the loop. I think this is possible with interrupts only. But I can not see any documentation or help on how to perform this.
How to write an ISR for this task?

  • 1
    Please explain why you want to do such a thing. What is the difference of writing the incoming data in a buffer and processing that buffer in the loop. I think that if you write you sketch with 'millis' instead of 'delay' and use a hardware serial port with the standard Arduino functions, then all your problems are probably solved. Could this be a XY-problem ( xyproblem.info ) and your real question is how to reduce latency for incoming serial data in your sketch ? – Jot Jun 1 '17 at 11:56
  • you actually want more of an event callback than a strict interrupt routine – dandavis Jun 1 '17 at 20:39
2

You can't do it directly with an interrupt. SoftwareSerial already uses an interrupt to do the actual reception - the received byte being placed in a circular buffer.

The only thing you could do would be to modify the source code for SoftwareSerial so that instead of storing the byte in the buffer it calls your custom code instead.


Ok, so you're NOT using SoftwareSerial, you're using (something which I have never heard of) "NeoSWSerial". That has the ability to attach an interrupt (by adding it to the reception interrupt ISR), and the README.md file with that library gives you an example of how to use it:

#include <NeoSWSerial.h>
NeoSWSerial ss( 4, 3 );

volatile uint32_t newlines = 0UL;

static void handleRxChar( uint8_t c )
{
  if (c == '\n')
    newlines++;
}

void setup()
{
  ss.attachInterrupt( handleRxChar );
  ss.begin( 9600 );
}

So just read the character in there, set a flag to say you have a new "mode" arrived, and in your loop react to that flag and run the right function for that mode.

If you want to terminate a mode early you will have to program your modes to also react to the "new mode" flag and terminate. The interrupt cannot do that for you.

  • can I do something with attachInterrupt? – Arjun Singh Jun 1 '17 at 10:38
  • No, you can't do anything with attachInterrupt. – Majenko Jun 1 '17 at 10:41
  • I am using NeoSWSerial not the original Software Serial it has attachinerrupt function github.com/SlashDevin/NeoSWSerial/blob/master/src/… – Arjun Singh Jun 1 '17 at 10:53
  • 1
    Well that's a completely different story. It would have helped if you had stated that in your question. When you say you are using software serial it's immediately assumed (unless explicitly stated otherwise) that you are using the standard Arduino software serial. If you aren't you must state that at the beginning. Then we can tell you to go and read the manual where it shows you exactly how to do what you want to do. – Majenko Jun 1 '17 at 10:58
  • Is some help possible with that library. Sorry, I thought NeoSWSerial is like an extension of the original one so I thought may be the library already has some support. Please help if possible – Arjun Singh Jun 1 '17 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.