IO.Ports.SerialPort - issue with .BytesToRead and DataReceived Event

Hi Folkes!

I'm currently developing an infrared IR-communication software for PocketPC's with Windows Mobile 2003 SE and Windows Mobile 5.0
The communication is based on:    PocketPC virtual serial-port --> IR    and    IR --> Device serial-port.
I wrote a communication-library for windows first, which worked perfectly with a real serial conncetion (no use or IR).

The same code should work for the virtual serial-port and IR, since it's only a simple IR serial-bridge.

Here comes the issue:

It's possible to write data from the PocketPC to the device with .Write()
It's possible to read data from the device with .Read() and similar.

BUT:
it's not possible to dertermine how may bytes are in the receive-buffer with .BytesToRead (always 0) !
Second, the event-handler DataReceived is not called too!

Interesting, i thought.

After may hours of tinkering I decided to use the "OpenNETCF.IO.Serial" (www.opennetfc.org) component.
Same settings... and it worked flawlessly.

I'm very curious about the fact that .BytesToRead is always zero, after the device sent data to the PocketPC.
And also that it's possible to read the data with .Read().
By the way, the app refuses to close after the Form1_Closed event-handler. Strange.

I have used several PocketPC's:
   HP iPAQ hx4700 (Win Mobile 2003 SE)
   Qtek MDA Compact mobile Phone (Win Mobile 2003 SE)
   Qtek MDA 2 Compac mobile Phone (Win Mobile 5.0) 

I have tried the .NET Compact Framewort 2.0 and 2.0 SP1 Beta.

According to the .NET Framework idea, the code for windows (at least for IO.Ports.SerialPort) should work on the PocketPC too - right

So can anyone help me out
I'm looking forward to your replies.

       - Christian Friesnegg

 

HERE IS THE WORKING CODE with OpenNET Compact Framework used:

Imports OpenNETCF.IO

Public Class Form1

Private WithEvents oSerial As Serial.Port

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'create and open serial-port
oSerial = New Serial.Port("COM3:")

oSerial.RThreshold = 1
oSerial.Settings.BaudRate = Serial.BaudRates.CBR_9600
oSerial.Settings.ByteSize = 8
oSerial.Settings.Parity = Serial.Parity.even
oSerial.Settings.StopBits = Serial.StopBits.one

oSerial.IREnable = False 'I can't see an effect if set True/False, so I left this False.

oSerial.Open()

End
Sub

Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

oSerial.Close()

End Sub

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

Dim aSend(4) As Byte

aSend(0) = &H14 'command write 1 byte
aSend(1) = &H4 'address-high
aSend(2) = &HC0 'address-low
aSend(3) = &H12 'data
aSend(4) = &HC2 'XOR checksum

'send the packet
oSerial.Output = aSend

'wait some time for the reply
Threading.Thread.Sleep(100)

'check if we have some bytes in the receive buffer
Debug.WriteLine("InBuffer Count: " & oSerial.InBufferCount)

'get the received bytes
Dim aRec() As Byte
aRec = oSerial.Input

Debug.WriteLine("Read " & aRec.Length & " bytes.")

End Sub

End Class

 

HERE IS THE BAD CODE with .NET Compact Framework used:

Imports System.IO.Ports

Public Class Form1

Private WithEvents oSerial As SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'create and open serial-port
oSerial = New SerialPort("COM3")

oSerial.ReceivedBytesThreshold = 1
oSerial.BaudRate = 9600
oSerial.DataBits = 8
oSerial.Parity = Parity.Even
oSerial.StopBits = StopBits.One

oSerial.Open()

End Sub

Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

oSerial.Close()

End Sub

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

Dim aSend(4) As Byte

aSend(0) = &H14 'command write 1 byte
aSend(1) = &H4 'address-high
aSend(2) = &HC0 'address-low
aSend(3) = &H12 'data
aSend(4) = &HC2 'XOR checksum

'send the packet
oSerial.Write(aSend, 0, aSend.Length)

'wait some time for the reply
Threading.Thread.Sleep(100)

'check if we have some bytes in the receive buffer
Debug.WriteLine("Bytes to Read: " & oSerial.BytesToRead) 'THIS IS ALWAYS ZERO!!

Dim iReadBytes As Integer

'get the received bytes
Dim aRec(2) As Byte
iReadBytes = oSerial.Read(aRec, 0, 3) 'THIS WORKS, the device has sent 3 bytes to the PocketPC!!

Debug.WriteLine("Read " & iReadBytes & " bytes.")

End Sub

End Class




Answer this question

IO.Ports.SerialPort - issue with .BytesToRead and DataReceived Event

  • Mindaugas Lukošius

    Ok, I miss-titled the thread... it should read only "issue with .BytesToRead"

    I know that I have to write a DataReceived function and hook it to the DataReceived event, however that's not exactly the problem with the posted example source.

    The situtation is that I'm reading synchonously from the serial interface (so there is no need to write a DataReceived event handler).

    It seems that the implementation can not handle virtual serial-ports correctly.
    The port uses IrCOMM protocol to communicate via infrared with an IR to serial converter.
    With the OpenNET CF there is no problem, I can read and write to the port (even when no ".IREnable" is set!)

    The .NET serial seems to have difficulties...
    No event is fired, NO EVENT - no DataReceived, no ErrorReceived, no PinChanged event!!

    When the same code is used with a real serial-interface and Windows everything works as expected.

    I have no idea why that should not work. Any suggestions



  • Musafir

    I haven't tried using NetCF on virtual serial ports as you are attempting. Have you tried using the System.Net.Sockets.IrDAClient class

  • Matt Deane

    Hi Brian,

    I'm afraid I can't try your code with my hardware since I use a special IrDA-to-RS232 converter IC.
    This IC needs the special IrCOMM protocol used by virtual IR ports on PocketPC's.
    Therefore I can't connect to the converter IC directly.

    However I was able to recompile the OpenNetCF.IO.Serial library (library version 1.4) with VS2005.
    I've used the recompiled version for serial handling and use the new OpenNetCF 2.0 for other things.

    I would recommend you to get the OpenNetCF.IO.Serial library from
    http://www.opennetcf.org/PermaLink.aspx guid=e74529b0-4447-4293-b880-03628bc7a14d
    and try to compile once again.

    - Chris



  • normalguy202

    Hi Chris,

    Thanks for that. I used that link and got the code compliling and use the dll in my project.

    However, I'm a little confused!

    I thought to use the IRCOMM protocol you must use the WinSOCK API and therefore use sockets, the irdaCLient class and allthat. But you seem to be trying to use the Serial virtual ports way which was abandoned by Microsoft since windows 95, right

    As I say, I'm a bit confused. I had my mind made up that I had to use Sockets, until I read this thread.

    Do clarify if you can.

    I've been trying to use the serial port way and it's not working either. I'm trying to open COM3, I send some data but my device doesn't receive anything.

    here's a bit of the code I'm using:

    port = new Port("COM3", portSettings);

    port.IREnable = true;

    port.Settings.BaudRate = BaudRates.CBR_9600;

    port.Settings.ByteSize = 8;

    port.Settings.Parity = Parity.none;

    port.Settings.StopBits = StopBits.one;

    port.RThreshold = 1; // get an event for every 1 byte received

    port.InputLen = 1; // calling Input will read 1 byte

    port.SThreshold = 1; // send 1 byte at a time

    byte[] outputData = UnicodeEncoding.ASCII.GetBytes ("A2A2A2A2A2, \r\n");

    try

    {

    port.Open();

    port.Output = outputData;

    MessageBox.Show("Data Sent\n\n");

    port.Close();

    }

    catch (System.Exception ex)

    {

    MessageBox.Show("Couldn't Send" + ex.Message);

    port.Close();

    return;

    }



  • Kevin Hoffman

    you should study how to use "delegate" before you continue your working.

    In fact, you should define a data-received function for DataReceived, and add it to DataReceived.


  • Ultrawhack

    Exactly same probleme here.

    When using ircomm on an HP iPAQ BytesToRead (always 0) and DataReceived don't work at all.

    ReadByte() works just fine.

    Any solution




  • palmirag

    I had a look at it and tried to do some communication, however the IrDA <-> serial converter need the special protocol generated by the virtial serial port.
    So using System.Net.Sockets.IrDAClient won't work in this case.

    Really interesting that the OpenNet CF implementation of the serial-interface class works and the IO.Ports.SerialPort doesn't.



  • Dj4Killer

    Hi Christian,

    Hope you are still there!

    I'm trying to 'connect' to an irCOMM receiver (MCP2140 from microchip), from an Acer N50 PDA with pocket pc 2003, quite similar to something you have done from what I see.
    I've been using VS 2005 and C# , using the class IrDAClient. The line that never works is the .Connect part for me.

    IrDAEndPoint ep = new IrDAEndPoint(di.DeviceAddress, ServiceName);
    // connect using 9-wire mode to MCP 2140
    try
    {
    //--------------------------------------------
    // Connect
    //--------------------------------------------
    cli.Client.Connect(ep);
    MessageBox.Show("Connected\n\n");
    return;
    }
    catch (SocketException exSoc)
    {
    MessageBox.Show("Couldn't connect on service "
    + ServiceName + ": "
    + exSoc.ErrorCode);
    return;
    }

    As using this code, I always get the same exception "Host software aborts connection" Socket error 10053, I decided to dig a little deeper and see what's goin on. Strange thing is I DO connect correctly to a cell phone or laptop!
    So I tried to use the OpenCF.IO.Serial library as you have, to see if I could tweek some settings to adjust to my device.
    I'm quite new to socket programming so not sure if this will work.

    I always get an error when I try to compile my application saying "Unable to open file C:\....etc...\MyOpenCF.dll -- The version 2.0 is not a compatable version"

    Do you know if you can use the OpenCF.IO.Serial library with the CF2.0 and VS2005

    Any experiences or code snippets would be very much appreciated!

    Brian.




  • IO.Ports.SerialPort - issue with .BytesToRead and DataReceived Event