SerialPort发送命令并获得响应

时间:2016-03-01 14:17:13

标签: c# serial-port

我正在创建一个使用串口工作的程序。我有文档,其中详细说明了如何通过端口发送命令。它运作良好,但我应该获得有关设备状态的一些信息。我尝试了一切,但我对这种类型的开发没有信心。可能是我错过了什么。

来自文档:

共有21个命令,20个命令是打开的,1个命令是读状态

打开命令:

地址命令固定值(0x55)锁定号

地址0XF2 0X55 0X01

地址0XF2 0X55 0X02

地址0XF2 0X55 0X03

...

以上命令执行

成功回归: 地址+ 0×59 + 0×59

失败返回​​:地址+ 0X5E + 0X5E

    /// <summary>
    /// Send open lokcer door command
    /// </summary>
    /// <param name="boardAddress">0 means open left slave cabinet,1 means open right slave cabinet</param>
    /// <param name="command"></param>
    /// <returns></returns>
    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);
        }
    }

效果很好,但最后2个字节没有返回任何内容。它们总是0X00。我也在使用SerialPortDataReceived,但我总是在那里得到0。

 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);
        }
    }

问题1 如何获得成功或失败?我认为它们应该在最后2个字节中。

问题2 如何获得所有州?我现在一无所获。

文档

读取锁定状态命令:地址+ Data1 + Data2 + Data3 地址:0XF10X55 Data1 Data2 Data3 0000 lock20 ... lock17 lock16 ... lock9 lock8 ... lock1 前4位数字是固定数字0000 样品: 0000 0000 0000 0001 0000 0001表示lock1和lock9已打开,其他已关闭 注意:对于某种类型的电子锁,“1”表示关闭,“0”表示打开。

我的代码段

/// <summary>
    /// Send check machine(cabinet) status command
    /// </summary>
    /// <param name="address">0 means check left cabinet, 1 means check right cabinet </param>
    /// <returns></returns>
    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);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

永远不要像你那样使用异步DataReceived事件!您需要决定:要么同步还是异步通信。如果您想要后者,请不要在DataReceived中致电甚至等待

相反,将收到的内容放入缓冲区并在邮件完成后立即处理。

此外,对ReadExisting的调用已经从串行端口读取所有内容,因此Read很可能阻止!

它应该看起来像这样:

List<byte> receivedBinaryData = new List<byte>();

public void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
    var sp = (SerialPort)sender;
    string availableData = sp.ReadExisting();
    byte[] inputAsASCII = Encoding.ASCII.GetBytes(availableData);

    DBException.WriteLog("");
    DBException.WriteLog("Serial port data received");
    DBException.WriteLog("Read existing = " + availableData);
    try
    {
        // Add the bytes to the receivedBinaryData List
        receivedBinaryData.AddRange(inputAsASCII);

        // If the message length is reached or exceeded, do something
        if (receivedBinaryData.Length >= 4)
           ....
    }
    catch (Exception ex)
    {
        DBException.WriteLog(ex);
    }
}