Pages: [1]   Go Down
Author Topic: SerialEvent() wont work  (Read 3214 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

iam currently porting my software for the ardunio mega to the new arduino Due.
Iam struggling currently with the simple thing "serialevent", however, it wont work.

I tested the blanko serialevent example.. i load it to the due board, opened the console and test to write some stuff to the board.
The RX of the programming port is blinkin when i write data, but serialevent never get called.

Any ideas? smiley-sad

IDE: arduino 1.5.1r2
Board: arduino due r3

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

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // 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() {
  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;
    }
  }
}

thanks alot..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried with another DUE board (i had another one, never used before) - just to be sure.
It also wont work with the other DUE, so i dont think its an hardware defect or something..
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8067
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if (inChar == '\n') {
      stringComplete = true;
    }

Is is possible that whatever code is sending text is not sending the expected newline character?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm struggling probably through the same issue where i've been using SerPro protocol open sourced by Alvaro, it has been working for more than 2 years within different MEGA (1280, 2560 rev 2 and rev 3) on different projects. When It will work OK then download on DUE but the SerPro protocol using Serial standard routines does not work. Please note that SerPro is an implementation HDLC on top of USB protocol plus has object oriented programming to add very easy any RX or TX commands coming from a GUI (Mac, PC or Linux) so among other things, has 16-bit CRC detection.
Logged

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Another aspect that might explain the problem, do some of you have been confronted to a endianness issue between MEGA, in fact all classic arduino AVR boards and DUE since we go from 8-bits to 32-bits. I've noticed the DUE compiler did check mistake when using INT on AVR so 16bits but if we leave this on DUE, it thinks of 32-bits or something else.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your suggestion,
i forgot to describe that i tried different newline seperators and for sure, different command line tools.
To be sure i put some logging code directly befinde the serialEvent() function.

Code:
    if (inChar == '\n') {
      stringComplete = true;
    }

Is is possible that whatever code is sending text is not sending the expected newline character?


Today i will try to use a clean installation of the arduino software, without my libaries.
I let you know later about the results.
Logged

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK I've replaced all occurrence such as INT or UNSIGNED INT by int16_t and uint16_t as found in my MEGA running SerPro protocol so it will more portable. Runs fine with my MEGA board but still fails with DUE even though compiles & downloads OK on DUE.

Unless I make an error, it is obvious to me that the Serial functionS set or family (i.e. read(), write(), event(),...) do not work on DUE if called inside an IDE, at least at 115200 bauds !
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1471
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It appears that serialEventRun() is missing in Due. There is only this definition:
Code:
extern void serialEventRun(void) __attribute__((weak));
The idea is that the real serialEventRun() only gets linked if you use Serial.

You can define serialEventRun() and serialEvent() will work.
Code:
void serialEventRun(void) {
  if (Serial.available()) serialEvent();
}
void serialEvent() {
  if (Serial.available()) Serial.print((char)Serial.read());
}
void setup() {
  Serial.begin(9600) ;
}
void loop() {}

Type a line in the serial monitor to demonstrate this works then remove serialEventRun() and run the sketch.

Serial input has garbage when a sketch starts.  The reset done by the serial monitor seems to cause this.  I have not found a reliable way to avoid this problem.

Logged

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 266
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi fat16lib,

I'm bit lost, not sure to understand your last post, have you found a fix or would it better waiting for another IDE release or fix directly from arduino team ?

In my case where I'm porting a big arduino mega project into arduino due, i need to use different Serial functions, not only Serial.begin(baud_rate), Serial.available() and Serial.read() but also functions like Serial.write() and Serial.flush()

Thank you in advance, Albert
Logged

0
Offline Offline
Edison Member
*
Karma: 44
Posts: 1471
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This just proves there is a problem since serialEventRun() is not defined in the arduino-1.5.1r2 release.

A temporary fix, while waiting for the Arduino company, would be to add a function similar to this
Code:
void serialEventRun(void) {
  if (Serial.available()) serialEvent();
}
to a sketch that has a serialEvent() function.  A true fix needs a lot more added to the IDE for Due.

People are reporting other strange behavior for Serial so you may be out of luck until the bugs are ironed out.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void serialEventRun(void) {
  if (Serial.available()) serialEvent();
}

works for serial but what is the code for serialEvent3?
« Last Edit: April 17, 2013, 02:44:52 am by feveran » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

for me this work:

Code:
void serialEventRun(void) {
  if (Serial.available())
          serialEvent();
  if (Serial1.available())
          serialEvent1();
  if (Serial2.available())
          serialEvent2();
  if (Serial3.available())
          serialEvent2();
}

cheers
Logged

Pages: [1]   Go Up
Jump to: