SerialEvent on softwareSerial ?

Hi,
im just wondering if anyone knows this??

On hardware serial i use SerialEvent to get serial data.....

Now i want serialEvent BUT for software serial... how can i manage this?

Don't bother.

All serialEvent does is the same as this code

void loop() {
  // usual stuff
   if (mySerial.available()  > ) {
      mySerialFunction();
   }
}

void mySerialFunction() {
   // whatever you want
}

...R

Robin2:
Don’t bother.

All serialEvent does is the same as this code

void loop() {

// usual stuff
  if (mySerial.available()  > ) {
      mySerialFunction();
  }
}

void mySerialFunction() {
  // whatever you want
}

…R

Hello. And what about cycle?

for example if i’m now in for() cycle and serial comes, cycle must finish until i can read it.

i create alternate loop like:

void loop(){
start();
}

void start(){
if(doRead()){
mySerialFunction();
}
}

void doRead(){
if (mySerial.available() > ) {
return true;
}else{
return false;
}
}

void mySerialFunction(){
for(int i=0; i<256; i++){
if(doRead())start();
//something work here
}
}

everything works perfectly, but i think it’s not so good to call 1000x if… if… if… if…

i try attachInterrupt to RX pin, but it’s not good idea and don’t works.

So any ideas if i still want to use SoftwareSerial and make any alternative thing to catch serial event?

Maajkl:
i create alternate loop like:

Let’s analyze your code by gradually reducing it to the simpler, equivalent forms. Actually, the compiler and linker will do most of this for us.

First, let’s look at doRead. This:

void doRead(){
      if (mySerial.available()  > ) {
         return true;
      }else{
         return false;
      }
}

… is the same as this:

void doRead(){
      return (mySerial.available()  > 0);
}

Then you can replace all the calls to doRead with that one line:

void loop(){
  start();
}

void start(){
   if (mySerial.available()  > 0) {
      mySerialFunction();
   }
}

void mySerialFunction(){
   for (int i=0; i<256; i++) {
      if (mySerial.available()  > 0)
        start();
      //something work here
   }
}

And then you could replace all calls to start with the simple if test it performs:

void loop(){
   if (mySerial.available()  > 0) {
      mySerialFunction();
   }
}

void mySerialFunction(){
   for(int i=0; i<256; i++){
      if (mySerial.available()  > 0) {
        if (mySerial.available()  > 0) {
          mySerialFunction();
        }
      }
      //something work here
   }
}

Now, I hope you can see

* You are calling mySerial.available 2 extra times.

* You are not calling mySerial.read() to get the received character. Until you call read, available will always return 1 or more.

* You are doing “something work here” 256 times for every character received.

* You are calling mySerialFunction from inside mySerialFunction. That’s called recursion, and your usage is not guarded to prevent a stack overflow.

All this monkey business is correctly replaced with:

void loop()
{
  if (mySerial.available()) {
    char c = mySerial.read();

      //something work here  (with c?)
  }
}

If you really need to do something 256 times for each character, the sketch should look like this:

void loop()
{
  if (mySerial.available()) {
    char c = mySerial.read();

    for (int i=0; i<256; i++) {
      //something work here  (with c?)
    }
  }
}

Unless “something work” takes a very long time, you will never lose data with this simple loop. The Arduino can perform about 1 million CPU instructions before it loses a character. That is tens of thousands of loop iterations.

everything works perfectly,

It does not. Your stack will overflow at some time, depending on how long the “something work” takes.

but i think it’s not so good to call 1000x if… if… if… if…

Actually, that is the best program structure for this embedded environment. If something is ready, do it, then check something else. If you use delay, the Arduino does nothing but twiddle its thumbs for that duration. Its time would be better spent servicing other things while the time elapses. This is called a “non-blocking” approach.

Cheers,
/dev