SerialPort DataReceived event problem

I am having a problem with serial port programming. I have a program that uses Com3. I use the DataReceived event handler for receiving characters from this port. When I debug my program through Visual Studio everything works as expected. However, if I disconnect my USB cable and run the Fez off a power supply the data received event does not fire when receiving characters.

To help debugging, I added a task that periodically sends out a debug message on Com3 with the value of the BytesToReceive property of Com3.

The debug string is being transmitted so the port is indeed being opened properly. If I send characters to the PandaII, the BytesToReceive is definitely increasing. So the com port seems to be functioning, its just the DataReceived event is not being called.

Doe anyone know why this would happen?

Did you add the event handler before or after opening the port? Try the other way around

Before, Just found another post about the order, I will try this right away.

Do you know why the Comport.Read event does not work the same in compact framework? Under windows the read function blocks until the timeout period is reached, but in compact framework is simply returns indicating zero bytes read without throwing an exception.

I think there is timeout value you can set to change this

Yes, I set the timeout but the read function simply returns without blocking.

So I re-arranged the assignment of the DataReceived event and it works.

With my application, I have to open and close the serial port many times. If I keep re-assigning this event every time I open the port will it waste resources? Do I have to remove the event before I close the comm port?

I think this is fixed netmf 4.2 when we have it in few months

Question, why do you need to open/close the port lots?

I am building an end of line tester for some of our ECU. Each one has both a CAN interface and a serial port. Unfortunately, not all the ECUs use the same baud rate. I plug the the ECU into the tester harness and it queries what device is connected over the CAN bus. I then have the FEZ dynamically change the com port settings so it can also communicate with the same ECU. I need to close the com port to change he baud rate and then re-open.

fair enough - yes that’s a very specific need, so makes perfect sense.

You can use register access to get to the processor directly and change the baud. No need to close the port.

Interesting, I will have to look into this.
Thanks