SerialPort to send command and to get response
<p>I'm creating a program to work with some equipment using serial port. I have documentation where there're details how to send command by port. It works very well but I should get some information about status of the equipment. I tried everything but I'm not confident in these type of developing. May be I missed something. </p>

<p><strong>From the documentation:</strong> </p>

<p>There are totally 21 commands, 20 commands are of opening, 1 command is of reading status</p>

<p>Open command:</p>

<p>Address Command fixed value（ 0x55） lock number</p>

<p>Address 0XF2 0X55 0X01</p>

<p>Address 0XF2 0X55 0X02</p>

<p>Address 0XF2 0X55 0X03</p>

<p>......</p>

<p>Above commands execution </p>

<p>Success returns :
Address+0X59+0X59</p>

<p>Failure returns: Address+0X5E+0X5E</p>

<pre><code>    /// &lt;summary&gt;
    /// Send open lokcer door command
    /// &lt;/summary&gt;
    /// &lt;param name="boardAddress"&gt;0 means open left slave cabinet，1 means open right slave cabinet&lt;/param&gt;
    /// &lt;param name="command"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public byte[] OpenBox(byte boardAddress, byte command)
    {
        var openCommand = new byte[5];
        openCommand[0] = (byte)(255 - boardAddress);
        openCommand[0] = 0xF2;
        openCommand[1] = 0x55;
        openCommand[2] = command;

        openCommand[3] = 0X00;
        openCommand[4] = 0X00;
        try
        {
            if (!serialPort.IsOpen)
            {
                //serialPort.WriteBufferSize = 5;
                serialPort.Open();
            }
            serialPort.DiscardInBuffer();
            serialPort.Write(openCommand, 0, 3);
            return openCommand;
        }
        catch (ArgumentException ex)
        {
            throw new InvalidOperationException("Locker cell can not be opened.", ex);
        }
    }
</code></pre>

<p>It works very well but last 2 bytes return nothing. They are always 0X00. Also I'm using SerialPortDataReceived but I always get 0 there. </p>

<pre><code> public void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        DBException.WriteLog("");
        DBException.WriteLog("Serial port data received");
        var sp = (SerialPort)sender;
        DBException.WriteLog("Read existing = " + sp.ReadExisting());
        try
        {
            DBException.WriteLog("Length = " + sp.BytesToRead);
            if (serialPort.BytesToRead == 4)
            {
                var readBuffer = new byte[4];
                sp.Read(readBuffer, 0, serialPort.BytesToRead);

                DBException.WriteLog("Read serial port :" + readBuffer.ByteArrayToString());

                //                    RaiseLockerStatusEvent(readBuffer.ByteArrayToString());
            }
        }
        catch (Exception ex)
        {
            DBException.WriteLog(ex);
        }
    }
</code></pre>

<p><strong>QUESTION 1</strong> How to get Success or Failure returns? I think they should be in the last 2 bytes. </p>

<p><strong>QUESTION 2</strong>
How to get all states? I get nothing at the present.</p>

<p><strong>Documentation:</strong></p>

<p>Read lock status command: Address + Data1 + Data2 + Data3
Address: 0XF10X55
Data1 Data2 Data3
0000 lock20 … lock17 lock16 … lock9 lock8 … lock1
The first 4 digits is fixed number of 0000
Sample:
0000 0000 0000 0001 0000 0001 means lock1 and lock9 is open, others are closed
Note: for some type of electronic lock, “1” means close, “0” means open.</p>

<p>My snippet</p>

<pre><code>/// &lt;summary&gt;
    /// Send check machine(cabinet) status command
    /// &lt;/summary&gt;
    /// &lt;param name="address"&gt;0 means check left cabinet, 1 means check right cabinet &lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public byte[] SendCheckLockControlStatueCommand(byte address)
    {
        lock (this)
        {
            var checkCommand = new byte[6];
            checkCommand[0] = Convert.ToByte(255 - address);
            checkCommand[1] = 0xF1;
            checkCommand[2] = 0x55;

            checkCommand[3] = 0x00;
            checkCommand[4] = 0x00;
            checkCommand[5] = 0x00;
            try
            {
                if (!serialPort.IsOpen)
                {
                    //                    serialPort.WriteBufferSize = 5;
                    serialPort.Open();
                }

                //Send inquiry command
                serialPort.DiscardInBuffer();
                //                    serialPort.DiscardOutBuffer();
                serialPort.Write(checkCommand, 0, 6);

                return checkCommand;
            }
            catch (ArgumentException ex)
            {
                throw new InvalidOperationException("Locker cell can not be opened.", ex);
            }
        }
    }
</code></pre>