not able to get seriaEvent working

Post here first, or if you can't find a relevant section!
Post Reply
jonghaboy
Posts: 9
Joined: Fri Mar 13, 2020 7:08 am

not able to get seriaEvent working

Post by jonghaboy »

i have been trying to get the serialEvent() call working with no success

i have read this closed issue https://github.com/stm32duino/Arduino_C ... issues/584
and tried to follow exactly but am having trouble

i am sending data through serial3 ports and am trying to listen to it with serialEvent

i am not getting anything.

i know its not a wiring issue because the Serial.available code which is commented works just fine

but when i try to use serial event, nothing works

can anyone help?

here is my build_opt.h code

Code: Select all

-DENABLE_HWSERIAL3
and here is my main code

Code: Select all

bool toggle = true;
pinNumber_T pin1 = PA2;

void serialEvent3()
 {
  Serial.write(Serial3.read());
  Serial.println("++++++++++++++++++++++++++++++++++++++++");
  if(toggle)
    digitalWrite(pin1, HIGH);
  else
    digitalWrite(pin1, LOW);
  toggle = !toggle;
 }

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial3.begin(115200);
  pinMode(pin1, OUTPUT);
}

void loop() {
 //  put your main code here, to run repeatedly:
// if(Serial3.available())
//   {
//    digitalWrite(pin1,HIGH);
//   Serial.println(char(Serial3.read()));
//    delay(200);
//    digitalWrite(pin1,LOW);
//    Serial.println(char(Serial3.read()));
//   delay(200); 
//    }
    Serial.println("not getting data");
    //delay(200);
}
User avatar
fpiSTM
Posts: 1754
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: not able to get seriaEvent working

Post by fpiSTM »

First your code can't build by default as pinNumber_T is udefined. So I guess your code is not complete.

Anyway after update it to build, you're right it's not work.
It seems the default void serialEventRun(void) is not properly attached. :shock:
That's why it's not work.
Defining it in the sketch solve the issue, anyway this should work per default, this is a regression.

As a workaround, add this to your sketch:

Code: Select all

void serialEventRun(void)
{
#if defined(HAVE_HWSERIAL3)
  if (serialEvent3 && Serial3.available()) {
    serialEvent3();
  }
#endif
}
Note: I've reopened and update your issue on GitHub as this is a regression.
jonghaboy
Posts: 9
Joined: Fri Mar 13, 2020 7:08 am

Re: not able to get seriaEvent working

Post by jonghaboy »

i forgot to include this in the sample code
#include <stdint.h>
#include "common.h"


and common has piNumber_T defined so that wasn't the problem

i have added the solution you have given me but still nothing works

here is the common.h

Code: Select all

#pragma once

////////// External Files //////////
#include <stdint.h>

#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
////////// END: External Files //////////



typedef uint8_t pinNumber_T;



////////// Hardware Definition //////////
const pinNumber_T BALL_SENSOR_PIN = 5;

//const pinNumber_T VIBRATION_SENSOR_PIN = PA2;
//
//
//const pinNumber_T LED_BAR_PIN = PA3;
//const pinNumber_T LED_BAR_PIN_GREEN = PA4;
//const pinNumber_T LED_BAR_PIN_RED = PA5;
//
//const pinNumber_T LED_BAR_2_PIN = PA6;
//const pinNumber_T LED_BAR_2_PIN_GREEN = PA7;
//const pinNumber_T LED_BAR_2_PIN_RED = PA8;

////////// End: Hardware Definition //////////

here is my whole code now

Code: Select all

#include <stdint.h>
#include "common.h"


bool toggle = true;
pinNumber_T pin1 = PA2;
//HardwareSerial Serial3(PA10, PA9);

void serialEvent3()
 {
  Serial.write(Serial3.read());
  Serial.println("++++++++++++++++++++++++++++++++++++++++");
  if(toggle)
    digitalWrite(pin1, HIGH);
  else
    digitalWrite(pin1, LOW);
  toggle = !toggle;
 }


void serialEventRun(void)
{
#if defined(HAVE_HWSERIAL3)
  if (serialEvent3 && Serial3.available()) {
    serialEvent3();
  }
#endif
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial3.begin(115200);
  pinMode(pin1, OUTPUT);
}

void loop() {
 //  put your main code here, to run repeatedly:
// if(Serial3.available())
//   {
//    digitalWrite(pin1,HIGH);
//   Serial.println(char(Serial3.read()));
//    delay(200);
//    digitalWrite(pin1,LOW);
//    Serial.println(char(Serial3.read()));
//   delay(200); 
//    }
    Serial.println("not getting data");
    delay(200);
}

User avatar
fpiSTM
Posts: 1754
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: not able to get seriaEvent working

Post by fpiSTM »

With this I get it working.
Try the default SerialEvent example in Aruduino menu.
User avatar
fpiSTM
Posts: 1754
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: not able to get seriaEvent working

Post by fpiSTM »

I've tested your code (I've made some clean) and it works:

Code: Select all

void serialEvent3()
{
  Serial.write(Serial3.read());
  Serial.println("++++++++++++++++++++++++++++++++++++++++");
  digitalToggle(LED_BUILTIN);
}

void serialEventRun(void)
{
#if defined(HAVE_HWSERIAL3)
  if (serialEvent3 && Serial3.available()) {
    serialEvent3();
  }
#endif
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial3.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {

}
User avatar
fpiSTM
Posts: 1754
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: not able to get seriaEvent working

Post by fpiSTM »

Post Reply

Return to “General discussion”