Ejemplo n.º 1
0
Archivo: Comm.cs Proyecto: fdev1/Japp
        /// <summary>
        /// Sends a commmand and returns without receiving a response
        /// </summary>
        /// <param name="command"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public void SendCommandSilent(JappCommand command, params byte[] arguments)
        {
            try
            {
                if (arguments.Count() > 0)
                {
                    //
                    // make sure the command is not too big
                    //
                    Debug.Assert(arguments.Count() <= 0xFFU - 0x2U);
                    //
                    // prepare the command
                    //
                    byte[] cmd = new byte[arguments.Count() + 2];
                    cmd[0] = (byte)((int)command);
                    cmd[1] = (byte)arguments.Count();
                    for (int i = 0; i < arguments.Count(); i++)
                    {
                        cmd[i + 2] = arguments[i];
                    }
                    //
                    // send the command
                    //
                    this.serialPort.Write(cmd, 0, cmd.Count());

                    //for (int i = 0; i < cmd.Count(); i++)
                    //  this.serialPort.Write(new byte[] { cmd[i] }, 0, 1);
                }
                else
                {
                    //
                    // send the command without arguments
                    //
                    this.serialPort.Write(new byte[] { (byte)((int)command) }, 0, 1);
                }
            }
            catch (TimeoutException)
            {
                throw new CommTimeoutException();
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new CommException(ex);
            }
            finally
            {
            }
        }
Ejemplo n.º 2
0
Archivo: Comm.cs Proyecto: fdev1/Japp
        /// <summary>
        /// Sends a command with arguments to the programmer.
        /// </summary>
        /// <param name="command">The command to send.</param>
        /// <param name="arguments">The command argumments or data.</param>
        /// <returns></returns>
        public CommResponse SendCommand(JappCommand command, params byte[] arguments)
        {
            byte         len;
            CommResponse response = new CommResponse();

            try
            {
                if (arguments.Count() > 0)
                {
                    //
                    // make sure the command is not too big
                    //
                    Debug.Assert(arguments.Count() <= 0xFFU - 0x2U);
                    //
                    // prepare the command
                    //
                    byte[] cmd = new byte[arguments.Count() + 2];
                    cmd[0] = (byte)((int)command);
                    cmd[1] = (byte)arguments.Count();
                    for (int i = 0; i < arguments.Count(); i++)
                    {
                        cmd[i + 2] = arguments[i];
                    }
                    //
                    // discard input buffer
                    //
                    //this.serialPort.DiscardInBuffer();


                    //
                    // send the command
                    //
                    this.serialPort.Write(cmd, 0, cmd.Count());

                    //for (int i = 0; i < cmd.Count(); i++)
                    //  this.serialPort.Write(new byte[] { cmd[i] }, 0, 1);
                }
                else
                {
                    //
                    // send the command without arguments
                    //
                    this.serialPort.Write(new byte[] { (byte)((int)command) }, 0, 1);
                }
                //Thread.Sleep(1);
                //
                // wait for the response
                //
                while (this.serialPort.BytesToRead == 0)
                {
                    ;
                }
                response.Response = (JappResponse)this.serialPort.ReadByte();
                //
                // get the 2nd byte of the response. This byte indicates the length
                // of the rest of the message so we also initialize response.Data to
                // an array of this length
                //
                len           = (byte)this.serialPort.ReadByte();
                response.Data = new byte[len];
                //
                // if there's any data in the response then copy it to the
                // Data array
                //
                for (int i = 0; i < len; i++)
                {
                    while (this.serialPort.BytesToRead == 0)
                    {
                        ;                                                               // spin cpu until we got bytes to read
                    }
                    response.Data[i] = (byte)this.serialPort.ReadByte();
                }
                //
                // discard any data left in buffer
                //
                this.serialPort.DiscardInBuffer();
            }
            catch (TimeoutException)
            {
                throw new CommTimeoutException();
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new CommException(ex);
            }
            finally
            {
            }
            //
            // return the response
            //
            return(response);
        }
Ejemplo n.º 3
0
Archivo: Comm.cs Proyecto: fdev1/Japp
 /// <summary>
 /// Sends a command to the programmer.
 /// </summary>
 /// <param name="command">The command to send.</param>
 /// <returns></returns>
 public CommResponse SendCommand(JappCommand command)
 {
     return(this.SendCommand(command, new byte[] { }));
 }