コード例 #1
0
        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="cmd">命令类型</param>
        /// <param name="Packet">返回包</param>
        /// <param name="sendData">要发送的数据</param>
        /// <returns></returns>
        private bool PutCommData(SERCOM_TYPE cmd, Byte[] sendData,Byte[] Packet)
        {
            try
               {
               //Byte[] mstartcode =new Byte[sizeof(UInt16)];
               //uIntToByteArray(FRAMESTARTCODE, ref mstartcode);

               //Int32 index = 0;
               //mstartcode.CopyTo(Packet, index);
               Byte sum = 0;

               Packet[(Int32)Index.startcode] =(Byte)FRAMESTARTCODE;
               sum += Packet[(Int32)Index.startcode];

               //Packet[(Int32)Index.protocol_type] = (Byte)FRAMEPRO_TYPE;
               //sum += Packet[(Int32)Index.protocol_type];

               //Packet[(Int32)Index.command_direction] = (Byte)FRAMECOMM_DIR;
               //sum += Packet[(Int32)Index.command_direction];

               Packet[(Int32)Index.cmdone] = (Byte)cmd;
               sum += Packet[(Int32)Index.cmdone];

               Packet[(Int32)Index.cmdtwo] = (Byte)SERCOM_TYPE.COM_NULL;
               sum += Packet[(Int32)Index.cmdtwo];

               if (sendData != null)
               {
                   Packet[(Int32)Index.length] = (Byte)sendData.Length;
                   sum += (Byte)sendData.Length;
               }
               else
                   Packet[(Int32)Index.length] = 0;

               if (sendData!=null)
               {

                   //sendData.CopyTo(Packet, (Int32)Index.buffer);
                   for (int i = 0; i < sendData.Length; i++)
                   {
                       Packet[((Int32)Index.buffer)+i]=sendData[i];
                       sum += (Byte)sendData[i];
                   }
               }

               if (sendData != null) { Packet[CONTAINER_LENGTH-1 + sendData.Length] = sum; }
               else { Packet[CONTAINER_LENGTH-1] = sum; }

               if (sum==0)
               {
                   HDIC_Message.ShowWarnDialog(null, "没有内容,无法组建发送包");
                   return false;
               }
               }
               catch (System.Exception ex)
               {
               HDIC_Message.ShowWarnDialog(null, "组建发送包的时候出现错误:"+ex.ToString());
               return false;
               }
               return true;
        }
コード例 #2
0
        /// <summary>
        /// 命令总入口
        /// </summary>
        /// <param name="cmd">发送命令的类型</param>
        /// <param name="dataLength">发送命令行中数据长度</param>
        /// <param name="dataAck">发送的数据</param>
        /// <param name="dataReq">返回的数据</param>
        /// <returns></returns>
        public Int32 Command(SERCOM_TYPE Reqcmd,Byte[] dataReq,Int32 MReceiveLength,ref Byte[] cmdlineAck)
        {
            mReadBuffer.Clear();
            Int32 errCode = 0;
            Int32 ReqCount = 0;
            ReceiveLength = MReceiveLength;
            if (ReceiveBytes != null)
            {
                Array.Clear(ReceiveBytes, 0, ReceiveBytes.Length);
            }
            mSpSlot.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(dataReceived);

            //////////////////////////////////////////////////////////////////////接收握手信号
            if (Reqcmd == SERCOM_TYPE.COM_NULL)
            {
                if (!mSemaphore.WaitOne(60000))//设置接收握手信号 等待时间是1分钟
                {
                    return -120; // timeout
                }
                //string a = "";
            }

            if (Reqcmd != SERCOM_TYPE.COM_NULL)
            {
                // flush serial port
                ////mSpSlot.DiscardInBuffer();
                //mReadBuffer.Initialize();
                //////////////////////////////////////////////////////////////////////////发送
                ReqCount = CONTAINER_LENGTH;
                if (dataReq != null)
                {
                    ReqCount += dataReq.Length;
                }
                Byte[] packet = new Byte[ReqCount];
                bool ReqData = PutCommData(Reqcmd, dataReq, packet);//组建发送命令包
                if (!ReqData)
                {
                    return -100;
                }
                try
                {
                    mSpSlot.Write(packet, 0, packet.Length);       //发送命令
                }
                catch (Exception ex)
                {
                    return -110;
                }
                //////////////////////////////////////////////////////////////////////////接收
                if (!mSemaphore.WaitOne(2000))//超时
                {
                    return -120; // timeout
                }
            }

                cmdlineAck = ReceiveBytes;

                return errCode;
        }