0
votes

I have a wrapper class that wraps .Net's SerialPort class to perform modem operations and data flow. When I'm reading from the SerialPort object, the data that I previously wrote to the port is found within the receive buffer. Is that a behavior that I can change with the SerialPort object? I haven't found a property for the class that prevents my outgoing data from ending up in the receive buffer.

I use SerialPort.Write() to write the data and this is what I do to read data

private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Task.Factory.Startnew(() => WriteToBuffer());
}

// ... 

private void WriteToBuffer()
{
    string data = string.Empty;

    lock(_serialPort)
    {
        StringBuilder sb = new StringBuilder();

        while(_serialPort.BytesToRead > 0)
        {
            sb.Append(_serialPort.ReadExisting());
        }

        data = sb.ToString();
    }

    // .. then do some additional processing
}

I read the data in a non-blocking manner because I'd rather be paranoid and not miss any data received events and have client code think there was some type of timeout.

I can prevent Hayes AT commands from being copied into the receive buffer by sending ATE0 but any data I send after the modem is connected to a remote modem is still creeping into the receive buffer.

Is there anything I can do? It's hard to just check and see what I wrote last to the buffer and ignore it because sometimes I read one byte at a time and is part of the data returned. For instance if I send something like "ID 123", I might read "ID " with the while loop and find that there are 0 bytes to read so it stops there. I know that I could just call a Thread.Sleep() each time but I think that would not be good practice.

Can I expect this echoing behavior in ALL modems? If so, then I can add code to filter my written data.

1
Sounds like you have something like 'echoing' on. I cant recall if that is available on SerialPort class, but most apps have that option. See if you can turn it off.leppie
Yes that's my problem and the only thing I can "turn off" is echoing of the AT commands. Everything else loops back. And I'm trying to add that behavior to my app, that's the problem.Jeff LaFay
It is called "full duplex" mode. Whatever device you are talking to would have to be configured to half duplex. It is unguessable what that takes, check out the manual for the device. Writing your code to filter the echo-ed command is certainly possible too.Hans Passant
I thought half duplex meant that only one device could transmit at a time and full duplex would mean that both devices could transmit and receive at the same time.Jeff LaFay
I agree, that's not what full duplex means. But he is right that the echoing is coming from the end device and you would have to configure that device to not echo or to filter it out.tcarvin

1 Answers

0
votes

I tried out a handful of other modems and the write data returned in the input buffer just like the original modem model that I tried. My solution was to filter out the written data to correctly read other data coming from the remote modem.