Go Down

Topic: SerialEvent() wont work (Read 5179 times) previous topic - next topic

michaelheid

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? :(

IDE: arduino 1.5.1r2
Board: arduino due r3

Code:
Code: [Select]

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..

michaelheid

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..

johnwasser

Code: [Select]

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


Is is possible that whatever code is sending text is not sending the expected newline character?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

selfonlypath

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.

selfonlypath

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.

michaelheid

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: [Select]

    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.

selfonlypath

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 !

fat16lib

It appears that serialEventRun() is missing in Due. There is only this definition:
Code: [Select]
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: [Select]

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.


selfonlypath

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

fat16lib

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: [Select]

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.

feveran

#10
Apr 16, 2013, 01:12 pm Last Edit: Apr 17, 2013, 09:44 am by feveran Reason: 1
Code: [Select]

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


works for serial but what is the code for serialEvent3?

roverman

for me this work:

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


cheers

felixlccc

First time posting. Two issues which I am not sure are already covered, but that I found interesting as a noob. In programming the Arduino Micro, I needed to insert the above serialEventrun() code as well as change the "carriage return" symbol to a single character, for some strange reason.

Go Up