Sign in to follow this  

[.net] System.IO.Ports.SerialPort, never fires DataReceived.

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is incredibly frustrating. In the code snippet below, sending byte 0x47 should result in the connected hardware returning a packet of state information... I was wanting to use that to confirm that the app and the device are talking. A break point on the Write() call is hit so I know that the port opened, etc, and the write call doesn't throw an exception. A break point at the top of OnDataReceived is NEVER triggered... it is simply never called. I noticed a million people online who had similar sounding problems, but most of theirs appears to be related to GUI stuff. Code that is exactly equivalent in C++ (using the code project CSerialEx code) works fine. Any idea what I might be doing wrong in this little snippet?
try
{
    _serialPort = new System.IO.Ports.SerialPort(
        portName, 9600, System.IO.Ports.Parity.Odd, 8, StopBits.One);
    _serialPort.Open();
    _serialPort.Handshake = Handshake.XOnXOff;
}
catch (System.Exception ex)
{
    _serialPort = null;
    return false;
}

_dataReceivedEventHandler =
        new System.IO.Ports.SerialDataReceivedEventHandler(this.OnDataReceived);
_serialPort.DataReceived += _dataReceivedEventHandler;

// Confirm that we are talking to the device...
_isCommunicationConfirmed = false;
_serialPort.Write(new byte[] { 0x47 }, 0, 1);
System.Threading.Thread.Sleep(50);
if (_isCommunicationConfirmed == false)
{
    Uninitialize();
    return false;
}

Share this post


Link to post
Share on other sites
Have you actually tested to see if the hardware is doing what you expect it should be doing?

I do some work with microcontrollers and interfacing with computers and I spent days trying to find why my computer app was not receiving the data properly, then found out the microcontroller was not responding at all to the app.

theTroll

Share this post


Link to post
Share on other sites
Recently I moved a project to use the SerialPort class in .NET 2.0. It didn't work for me at all. [url=http://www.microsoft.com/technet/sysinternals/utilities/portmon.mspx]PortMon[/url] is a useful tool for analysing Com Port communication. The output seemed quite different to using the API code I was using previously, rather than figure it out I just went back to using my API code. But there are probably some settings that will fix it.

Here is the SerialPort class I use. I ported it to C# a while ago from some VB6 code.

[url=http://members.iinet.net.au/~freeaxs/source/SerialPort.zip]SerialPort[/url]

Share this post


Link to post
Share on other sites
@TheTroll... The same sequence of commands, written in C++, does exactly what I expect it to do. So the hardware is either a) not receiving the data or b) the DataReceived event of the .NET SerialPort class is not being fired.

@HeadKaze... thanks, I'll take a look. But to be honest, I've already decided to use mixed-mode C++/CLI for this and just keep the working code.

It's a shame that MS can't properly implement what is basically their own CLR spec... unless, of course, I'm doing something wrong.

Oh, well... thanks!

Share this post


Link to post
Share on other sites

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this