Alex Zhukovskiy
Alex Zhukovskiy

Reputation: 10025

Serial COM port works only in debug

I have a race condition or something like it. I mean if I toggle a breakpoint before reading from COM, everything is good. But when i'm toggling it off, it freezes. writing:

    public void Send(ComMessage message)
    {
        byte[] bytes = message.Serialise();
        if (!_outputPort.IsOpen)
            _outputPort.Open();
        try
        {
            byte[] size = BitConverter.GetBytes(bytes.Length);
            _outputPort.Write(size, 0, size.Length);
            _outputPort.Write(bytes, 0, bytes.Length);
        }
        finally
        {
            if (_outputPort != _inputPort)
                _outputPort.Close();
        }
    }

reading

    private void InputPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
    {
        var port = (SerialPort) sender;

        byte[] sizeBuffer = new byte[sizeof(long)];
        port.Read(sizeBuffer, 0, sizeBuffer.Length);
        int length = BitConverter.ToInt32(sizeBuffer, 0);
        byte[] buffer = new byte[length];

        int i = 0;
        while (i < length)
        {
            int readed = port.Read(buffer, i, length - i);
            i += readed;
        }


        var message = ComMessage.Deserialize(buffer);
        MessageReceived(this, message);
    }

for example, message has 625 bytes length. If I toggle a breakpoint, port.BytesToRead is equal 625, but if I disable it, byte count is 621.

Strange, but it works for a little amount of bytes (for short messages), but doesn't for long.

Please, advice.

Upvotes: 0

Views: 428

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

message has 625 bytes length. If I toggle a breakpoint, port.BytesToRead is equal 625, but if I disable it, byte count is 621.

You never check the first Read to see how many bytes it read. It may have read less than sizeof(long) bytes in. However, that is not the source of your problem, your main problem is you are making a buffer of size long but long is Int64, you are calling ToInt32 (and writing a Int32 in your sender).

The reason your byte count is 261 instead of 265 is because the first 4 bytes of your message is sitting in sizeBuffer[4] through sizeBuffer[7] which you never processed.

To fix this you should be either doing sizeof(int) or even better to make it more obvious that the buffer is for the ToInt32 call, use sizeof(Int32)

Upvotes: 1

Related Questions