Hi all,

I´m having troubles programming with C# for windows CE. I need to listen a serial port, I have the program for windows and it works fine but when I program it to windows CE I dont know why it does not recognize any event function.

I normally use the event for the onRx but under the CE platform I think it does not work, does anyone know how to do that?

Regards,

First, you need to set what events you want to be notified of by called SetCommMask. If you're only interested in whether data is available, you can call this with SetCommMask(mySerialPort, EV_RXCHAR);.
Once you have set up the events you're interested in, you need to use the method WaitCommEvent. It will fill an LPDWORD which you can do a bit-comparison against for EV_RXCHAR

...
LPDWORD eventStatus;
if(WaitCommEvent(mySerialPort, &eventStatus, 0))
{
    if(eventStatus & EV_RXCHAR)
    {
        // Read data
    }
}
...

The above is for C++ whoops! I will leave it here in case you want to write a native library

With relation to C#, did you make sure to install the .NET 3.5 Compact Framework? There are bugs in 2.0 with respect to Serial/Comm ports.

Ketsuekiame,

Yes I installed the .NET 3.5, but how can I be sure that is installed correct?

` private void DisplayText(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + RxString; //AppendText(RxString);
}

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        RxString = serialPort1.ReadExisting();
        this.Invoke(new EventHandler(DisplayText));
    }`

This works at Windows but not at windows CE.

Can you post the code where you set up the serial port and the events please?

Ketsuekiame,

I also used the example from the page csharp.simpleserial.com thinking that was somethin wrong with my code but no.

Running at windows is ok but when I compile to WinCE ARM it opens but when I click on Start it shows errors.

You're going to need to post the code where you setup the port and the exact error message otherwise I'm not going to be able to help you further due to a lack of information.

Ketsuekiame, sorry for my delay I was traveling.

Below is my code

private System.IO.Ports.SerialPort serialPort1;

private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM5";
            serialPort1.BaudRate = 9600;

            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }

The error shows when I click at start button, NullReferenceException.

Well at a glance, this shouldn't work on Windows either. You haven't created the SerialPort object anywhere, nor have you assigned the event to the port.

Is this everything?

You would need something somewhere which did;

serialPort1 = new SerialPort();
serialPort1.DataReceived += serialPort1_DataReceived;

Do you have that code somewhere?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.