Tx LED blinking, but SerialEvent not called

Hej,

I programmed in C++ a piece of code that sends serial data over the COM port (Windows) on which my Arduino Uno is connected. Whenever my code sends data (a string, ending with NULL), I see the Rx LED on my Arduino Uno blinking, but SerialEvent is not triggered (I programmed blinking of a LED on I/O 4 as first action within SerialEvent). My LOOP is empty.

How can it be that the Rx LED is triggered, but SerialEvent isn't?

Hope to find a solution for this!

Grts,
Gijs.

Did you call Serial.begin()?

Did you call that with the same settings (speed, number of bits, parity) as the application?

You didn’t disable/mess with interrupt?

My LOOP is empty.

You don't have a LOOP. It is not the only thing that is empty.

Why does your thread title talk about the Tx LED, while your post rants about the Rx LED?

Post ALL of your code.

And, shame on you for being so lazy that you "need" the serialEvent() crutch.

Hej septillion,

Thanks for your helpful reply!

Yes, I did call Serial.begin(9600) and specified 9600 as well in my C++ code. See the code below.

I wouldn’t know how to disable or mess with interupt, so I hope I didn’t do that. Can you let me know how to check on that?

My Arduino simplified code: I assume that as soon as something arrives over the serial communication, the Rx LED blinks, what it does, and serialEvent is called, resulting in blinking of the LED on I/O 4, but the latter doesn’t happen.

int LED = 4;

void setup() {
    Serial.begin(9600);
    //Serial.println("<Arduino is ready>");
    pinMode(LED, OUTPUT);
  }

void loop() {}


void serialEvent()
{
              digitalWrite(LED, HIGH);
              delay(200);
              digitalWrite(LED, LOW);
              delay(200);
              digitalWrite(LED, HIGH);
              delay(200);
              digitalWrite(LED, LOW);
              delay(200);
}

In C++ I do:

tstring commPortName(TEXT("COM3"));
Serial serial(commPortName, 9600);
serial.flush();

int bytesWritten = serial.write(Quadrant);

Might it be the serial.flush() command resulting in the flushing of the Rx LED?

Malibuke:
I wouldn’t know how to disable or mess with interrupt, so I hope I didn’t do that. Can you let me know how to check on that?

At least in you’re code below you didn’t mess with interrupts.

Malibuke:
My Arduino simplified code:

The word simplified terrifies me a bit :wink: Especially with the horrible indentation of the code. Did you test it with THIS code exactly?

Malibuke:
Might it be the serial.flush() command resulting in the flushing of the Rx LED?

I don’t know because I don’t really know C++ on a PC… What I do know is that .flush() on the Arduino does nothing more then wait until the transmit buffer is empty aka send out.

But did you test this sketch with the serial monitor? Aka, upload that test and open the serial monitor and send something. Then the led should start blinking (and never stop because you never empty the receive buffer :p).

And if that doesn’t happen, check if you can make the led blink at all.

int LED = 4;

void setup() {
  Serial.begin(9600);
  //Serial.println("<Arduino is ready>");
  pinMode(LED, OUTPUT);
  
  digitalWrite(LED, HIGH);
  delay(100);
  digitalWrite(LED, LOW);
  delay(900);
  digitalWrite(LED, HIGH);
  delay(100);
  digitalWrite(LED, LOW);
  delay(900);
}

void loop() {}


void serialEvent()
{
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(200);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(200);
}

Oww, and a bonus question, which Arduino do you use?

Yes, I did test it with this code, and yes, I forgot to mention, but if I use the serial monitor and type anything, the LED starts blinking and never stops. But if my C++ sends something, showing the Rx led to blink, the LED doesn't start.

I use Arduino UNO.

I'm sorry for the terrible code, but I'm not a programmer, I only use code to make prototypes to behave the way I want, to be able so show the working principle of mechatronical structures.

Are you opening the Serial connection in your Windows application each time that you send data ? If so, then be aware that opening the Serial connection to the Arduino causes it to reset.

I forgot to mention, but if I use the serial monitor and type anything, the LED starts blinking and never stops.

It won't, because the serial data is never actually read. Once there is something in the buffer, it is there until you reset the Arduino.

tstring commPortName(TEXT("COM3"));

Is the Arduino connected to COM3? You don't have the Serial Monitor app open at the same time, do you?

You need to post your COMPLETE C++ app.

@PaulS, that was already said and not a problem :wink:

@Malibuke, to make it look a bit less terrible, press Ctrl+T in the IDE :wink:

But it shows the problem is not on the Arduino making this not an Arduino problem… Apparently you do something wrong in the C application.

Are you indeed using the right COM port?

Is the serial monitor indeed closed at that time?

Can the C++ application tell you if the port is indeed opened?

What library are you actually using for Serial on the PC?

@UKHeliBob, I believe that's it! I moved the opening of the serial port to the beginning of my C++ file and now it works, at least with the simplified code. The LED starts blinking at the moment I send the data (and it never stops, but that's a simplified code issue...).

@septillion, thanks for the Ctrl+T tip! :wink: And for your very helpfull and friendly support!

@PaulS, thanks for the content!