Main Site Documentation

SerialPort DataReceived event problem


#1

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?


#2

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


#3

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.


#4

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


#5

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?


#6

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


#7

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


#8

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.


#9

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


#10

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


#11

Interesting, I will have to look into this.
Thanks