serialEvent call

Hi guys, i have tryed to use the serialEvent() function:

void serialEvent() {
  Serial.println("wtf?");
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }

As i understanded, it should be called automatically when data is recieved, but it never happens.
Maybe i should connect it with some interrupts "manually" or am i missing something?

It gets called at the end of each iteration of loop(), if there is something in the serial buffer. Without seeing the rest of your sketch, its hard to tell what the problem is.

Main.cpp

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USB.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

HardwareSerial.cpp

void serialEventRun(void)
{
#ifdef serialEvent_implemented
  if (Serial.available()) serialEvent();
#endif
#ifdef serialEvent1_implemented
  if (Serial1.available()) serialEvent1();
#endif
#ifdef serialEvent2_implemented
  if (Serial2.available()) serialEvent2();
#endif
#ifdef serialEvent3_implemented
  if (Serial3.available()) serialEvent3();
#endif
}

What is "if (serialEventRun)" ...? Kinda confused about that one. Does that evaluate to "true" if the function serialEventRun() is defined?

The following code should get you working.

import processing.serial.*;
Serial myport; // Create object from Serial class

void setup() {
// The next 3 lines are needed to call the event
String arduinoPort = Serial.list()[0];
myport = new Serial(this, arduinoPort, 115200);
myport.bufferUntil('\n');
}


// This function must be called serialEvent for this to work!!!
// it is part of the  processing.serial library and is defined this way
void serialEvent(Serial myport)
{
String inString = myport.readStringUntil( '\n' );

if(inString != null )
  {
  inString = trim(inString);
// process the code as you wish from here

}

spirilis:
What is "if (serialEventRun)" ...? Kinda confused about that one. Does that evaluate to "true" if the function serialEventRun() is defined?

serialEventRun is a function pointer and it's saying to only make a call if the pointer actually points to something.

well, the entire code is here:

/*
  Serial Event example
 
 When new serial data arrives, this sketch adds it to a String.
 When a newline is received, the loop prints the string and
 clears it.
 
 A good test for this is to try it with a GPS receiver
 that sends out NMEA 0183 sentences.
 
 Created 9 May 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/SerialEvent
 
 */

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
}

void loop() {
  /*
  if (Serial.available()) {
    Serial.print((char)Serial.read()); 
  }
  */
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
*/
void serialEvent() {
  Serial.println("wtf?");
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

taken and modified a little bit from the tutorial.
as it is, serialEvent is never called, but if i uncomment the first lines of loop(), those:

  if (Serial.available()) {
    Serial.print((char)Serial.read()); 
  }

It prints me back successfully all that i send.
Tryed even sending some '\n' (the real char ofc), CR, ecc. but nothing.
I'll try the code of buteman and let u know :wink:
Tnx for the moment for your answers =)

Ah, i didn't notice at the first sight...
Buteman, your code is for Processing, not for arduino, am i right?.
I'm looking on the code for arduino board.

Ok, sorry for have bothered you...
That's the point:
i had an old version of arduino (about 0.22), instead of the 1.0.

After the upgrade, now it works perfectly :confused:

zamy:
Ah, i didn't notice at the first sight...
Buteman, your code is for Processing, not for arduino, am i right?.
I'm looking on the code for arduino board.

Yes code is for Processing.
Sorry but I thought you were sending data from arduino for your PC to read.

No problem dude :slight_smile:
I was just testing some stuffs at low level.
Now it works anyway =)
Thank you all for your help

Can this help me?

I am using a MakeyMakey to input single numbers or letters instead of a keyboard. I need these numbers and letters to trigger events in my arduino script.

simply a loop

if L start the motor
if K stop the motor
if J reverse the motor

I tried SerialInput but this requires the user to always click the SerialMonitor send button.

thanks - Theo

Use a proper terminal emulator.

Please don't necro unrelated five year old threads.