SerialPort.Read () does not end

1

I'm trying to know the position of an engine through a COM port thanks to the method Read(Byte(), Int32, Int32)

Whatever the serial port, when I try to read with SerialPort.Read(buffer,offset,length) , it never stops returning.

This method reads several bytes of the SerialPort input buffer and writes them to a byte array at the specified offset position. I do not know if this is because there is not enough data or for another reason. Then, the program is closed.

Is it a problem with the driver?

I've read this msdn link. microsoft that

  

The Read method returns zero only after reaching the end of the sequence. Otherwise, Read always reads at least one byte of the sequence before returning. If there is no data available in the stream after a call to Read, the method will be blocked until at least one byte of data can be returned.

I have to know where this engine is to move it when I move a camera that is on another COM port, and use it with a timer. I think this is what blocks the program, but I'm not sure.

How can I do to know its position without blocking the program? Do I have to use Read() only when I move the camera?

    
asked by ThePassenger 03.08.2016 в 10:01
source

2 answers

1

If the engine is capable of sending information to the serial port when it moves, you can use the SerialPort.DataReceived event to get the serial port information.

You will find an example in the following link.

SerialPort.DataReceived

Keep in mind that it is executed in a thread different from the main one, if you have to call some method of your program you must do it using .Invoke

This is the function I use to read recurring data from an RFID reader:

private void rs_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        if (this.rs.BytesToRead > 0)
        {
            char[] n = new char[1];
            this.rs.Read(n, 0, 1);

            if (n[0] == 0x0a)
            {
            }
            else if (n[0] == 0x07)
            {
                //MessageBox.Show("Boton pulsado");
            }
            else if (n[0] == 0x1b)
            {
            }
            else if (n[0] == 0x02)
            {
                try
                {
                    this.TagRfid = this.rs.ReadLine();
                    if (this.Parent != null && this.DatosRecibidos != null)
                    {
                        this.Parent.Invoke(this.DatosRecibidos, new Object[] { this.TagRfid });
                    }
                }
                catch (Exception ex)
                {
                    this.UltimError = ex.Message;
                }
            }
        }
    }

If you are only going to read the port buffer, you must first make sure that there is information pending, the BytesToRead property will tell you if there is something pending to be read, although according to what Read functions maybe they are not able to read it.

I would use ReadByte using some loop and verify the received bytes one by one.

    
answered by 13.10.2016 в 16:30
0

Well it seems correct that "do not end" until there is information to return. Make sure you are using the correct settings for communication with your device (port number, speed, etc.). And to check the communication and configuration you could easily use a tool like Docklight ( link ) or similar.

    
answered by 13.10.2016 в 13:01