示例#1
0
文件: Program.cs 项目: luyang14/TinyG
        private static Response SendCommand(XCommand cmd)
        {
            Port.DiscardInBuffer();
            Port.DiscardOutBuffer();
            Port.Write(cmd.Command, 0, cmd.Command.Length);
            if (cmd.Data != null && cmd.DataCount > 0 && cmd.Data.Length >= (cmd.DataIndex + cmd.DataCount))
            {
                Port.Write(cmd.Data, cmd.DataIndex, cmd.DataCount);
            }
            int timeout = 200;

            while (timeout != 0 && Port.BytesToRead < cmd.ResponseLength)
            {
                --timeout;
                Thread.Sleep(10);
            }
            if (Port.BytesToRead >= cmd.ResponseLength)
            {
                byte[] bytes  = new byte[Port.BytesToRead];
                int    readed = Port.Read(bytes, 0, Port.BytesToRead);
                Response.Status = ResponseStatus.Unknown;
                if (cmd.ReplayIndex >= 0)
                {
                    byte replay = bytes[cmd.ReplayIndex];
                    if (replay == XBOOT.REPLY_ACK)
                    {
                        Response.Status = ResponseStatus.Ack;
                    }
                    else if (replay == XBOOT.REPLY_YES)
                    {
                        Response.Status = ResponseStatus.Yes;
                    }
                    else if (replay == XBOOT.REPLY_ERROR)
                    {
                        Response.Status = ResponseStatus.Error;
                    }
                }
                byte[] answer = new byte[cmd.ResponseLength];
                for (int i = 0; i < answer.Length; i++)
                {
                    answer[i] = bytes[i];
                }
                Response.Answer = answer;
            }
            else
            {
                Response.Status = ResponseStatus.Timeout;
            }
            return(Response);
        }
示例#2
0
 private static Response SendCommand(XCommand cmd)
 {
     Port.DiscardInBuffer();
     Port.DiscardOutBuffer();
     Port.Write(cmd.Command, 0, cmd.Command.Length);
     if (cmd.Data != null && cmd.DataCount > 0 && cmd.Data.Length >= (cmd.DataIndex + cmd.DataCount))
         Port.Write(cmd.Data, cmd.DataIndex, cmd.DataCount);
     int timeout = 200;
     while (timeout != 0 && Port.BytesToRead < cmd.ResponseLength)
     {
         --timeout;
         Thread.Sleep(10);
     }
     if (Port.BytesToRead >= cmd.ResponseLength)
     {
         byte[] bytes = new byte[Port.BytesToRead];
         int readed = Port.Read(bytes, 0, Port.BytesToRead);
         Response.Status = ResponseStatus.Unknown;
         if (cmd.ReplayIndex >= 0)
         {
             byte replay = bytes[cmd.ReplayIndex];
             if (replay == XBOOT.REPLY_ACK)
                 Response.Status = ResponseStatus.Ack;
             else if (replay == XBOOT.REPLY_YES)
                 Response.Status = ResponseStatus.Yes;
             else if (replay == XBOOT.REPLY_ERROR)
                 Response.Status = ResponseStatus.Error;
         }
         byte[] answer = new byte[cmd.ResponseLength];
         for (int i = 0; i < answer.Length; i++)
             answer[i] = bytes[i];
         Response.Answer = answer;
     }
     else
         Response.Status = ResponseStatus.Timeout;
     return Response;
 }