system.io.ports.serial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Huish

    system.io.ports.serial


    I have an application that uses the system.io.ports .serial class to
    receive data from a serial device. The device is sending a data stream
    of stx then 15 readable ascii characters then a cr. My datareceived
    event looks like:

    int iBytesToRead = com.thePort.Byt esToRead;

    string data = com.thePort.Rea dExisting();

    // Make sure we are aligned on the <STXcharacter

    if (data[0] != STX)
    {
    com.thePort.Dis cardInBuffer();
    return;
    }

    //if (iBytesToRead != 17)
    // return;

    com.DataReceive d(data);
    com.thePort.Dis cardInBuffer(); // discard the checksum

    When I run the application in the debugger I 17 characters read but when
    I do not run with the debugger I get a variable number of characters -
    most often 8 and being the first n characters sent.

    It is almost like something is interrupting the ReadExisting.

    Any ideas on what is going on?

    thanks

    pete
  • Peter Duniho

    #2
    Re: system.io.ports .serial

    On Mon, 21 Apr 2008 21:00:23 -0700, Peter Huish <huish@ozemail. com.au>
    wrote:
    [...]
    When I run the application in the debugger I 17 characters read but when
    I do not run with the debugger I get a variable number of characters -
    most often 8 and being the first n characters sent.
    >
    It is almost like something is interrupting the ReadExisting.
    I'm no expert on the SerialPort class, but my reading of the
    ReadExisting() method is that it returns immediately, with whatever data
    is present. You should not expect it to wait until the end of the
    transmission.

    Of course this also means it's an error for you to call the
    DiscardInBuffer () method, since that will throw away whatever bytes might
    have come in after ReadExisting() returned but before you call
    DiscardInBuffer (). That sounds like a great recipe for losing data.

    So, instead you need to change your logic, so that there's a layer between
    the reading of the port and the consumption of the data that handles
    delimiting the data.

    Pete

    Comment

    Working...