Пример #1
0
    public void DataReceiveFunction()
    {
        Debug.Log("Recive data...");
        while (_contiue)
        {
            try
            {
                CMD_TYPE cmd = (CMD_TYPE)sp.ReadChar();
                Debug.Log(cmd);

                lock (cmdQueue)
                {
                    cmdQueue.Enqueue(cmd);
                }
            }
            catch (TimeoutException)
            {
                continue;
            }
            catch (System.IO.IOException) //热插拔
            {
                Debug.Log("Reconnect the Serial port...");
                ClosePort();
                Thread.Sleep(1000);
                OpenPort();
            }
            catch (System.InvalidOperationException)  //热插拔
            {
                Debug.Log("Reconnect the Serial port...");
                ClosePort();
                Thread.Sleep(1000);
                OpenPort();
            }
        }
    }
        public InspireMotorCommandMessage(uint stickID, CMD_TYPE CMDvalue, CONTRAL_TAB CTValue, uint Para)
        {
            //帧长度
            uint FrameLength = (DataSectorLong(CTValue, CMDvalue) + 2);

            byteList.Add((Byte)(FrameLength));
            //ID号
            byteList.Add((Byte)stickID);
            //指令类型
            byteList.Add((Byte)CMDvalue);
            //指令类型
            byteList.Add((Byte)CTValue);
            //数据段
            for (int i = 0; i < DataSectorLong(CTValue, CMDvalue); i++)
            {
                byteList.Add((Byte)((Para >> i * 8) & 0xFF));
            }
            //校验和
            uint CheckSum = 0;

            for (int i = 0; i < byteList.Count(); i++)
            {
                CheckSum = byteList[i] + CheckSum;
            }
            byteList.Add((Byte)(CheckSum & 0xFF));
        }
Пример #3
0
    /// <summary>
    /// バルブ・ポンプの実行/停止
    /// </summary>
    /// <param name="type">バルプorポンプ</param>
    /// <param name="sw">true…実行、false…停止</param>
    public void SetSendCmd(CMD_TYPE type, bool sw)
    {
        string sendCmd = string.Empty;

        if (sw)
        {
            sendCmd = cmd_A0;
        }
        else
        {
            sendCmd = cmd_80;
        }

        switch (type)
        {
        case CMD_TYPE.VALVE:
            sendCmd += subCmd_Valve;
            break;

        case CMD_TYPE.PUMP:
            sendCmd += subCmd_Pump;
            break;

        case CMD_TYPE.PHOTO_SENSOR:
            sendCmd += subCmd_PhotoelectricSensor;
            break;
        }
        if (sendCmd.Length == CmdLength)
        {
            //Debug.LogWarning("SetSendCmd:" + sendCmd);
            DataSend(sendCmd);
        }
    }
Пример #4
0
        private void OnExit(object sender, EventArgs e)
        {
            if (CMD_TYPE.SVN_ADD == this.executeCmdType)
            {
                this.svnAddFilePaths.Clear();
            }

            if (CMD_TYPE.SVN_COMMIT == this.executeCmdType)
            {
                this.svnCommitFilePaths.Clear();
            }

            if (this.orderQueue.Count > 0)
            {
                this.DoExecute(this.orderQueue.Dequeue());
                return;
            }

            this.executeCmdType = CMD_TYPE.NONE;
            this.CloseProcess();
            Writer.Instance.DelRsa(ConfigIni.GetRsaKeySavePath());
            this.PrintLog("执行完成", Color.Blue);

            if (this.conflictList.Count > 0)
            {
                if (DialogResult.OK == MessageBox.Show("发现冲突文件,点确定后进行SVN还原。注意:还原将删除你本地修改的冲突文件", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error))
                {
                    foreach (string path in this.conflictList)
                    {
                        this.Execute(string.Format("svn revert {0} & exit", path), CMD_TYPE.SVN_CLEANUP);
                    }
                    this.conflictList.Clear();
                }
            }
        }
 public static void MotorAResolver(object sender, SerialDataReceivedEventArgs e)
 {
     //判断是否是群体操作
     try
     {
         byte head1 = (byte)((SerialPort)sender).ReadByte();
         byte head2 = (byte)((SerialPort)sender).ReadByte();
         if (head1 == 0xAA && head2 == 0x55)
         {
             int         FreamLength = ((SerialPort)sender).ReadByte();
             uint        StickId     = (uint)((SerialPort)sender).ReadByte();
             CMD_TYPE    CMDvalue    = (CMD_TYPE)((SerialPort)sender).ReadByte();
             CONTRAL_TAB CTValue     = (CONTRAL_TAB)((SerialPort)sender).ReadByte();
             uint        total       = 0;
             List <byte> listbyte    = new List <byte>();
             for (int i = 0; i < FreamLength - 2; i++)
             {
                 listbyte.Add((byte)((SerialPort)sender).ReadByte());
                 ;
             }
             listbyte.Reverse();
             for (int i = 0; i < listbyte.Count(); i++)
             {
                 total = total << 8;
                 total = total | listbyte[i];
             }
             //CommandMessage message = new CommandMessage(StickId, CMDvalue, CTValue, total);
             //FindStickByID(StickId).ReceiveMessage(message);
         }
     }
     catch (Exception ex)
     {
         string s = ex.ToString();
     }
 }
Пример #6
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        public void AddCmdList(string Cmd, CMD_TYPE Work, object Info)
        {
            // 명령어 목록에 추가
            // 중복검사
            for (int i = 0; i < m_CmdList.Count; i++)
            {
                if (m_CmdList[i] == Cmd)
                {
                    MessageBox.Show("중복된 명령어 입니다.", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            // 내부 목록에 추가
            m_CmdList.Add(Cmd);
            m_CmdTypeList.Add(Work);
            m_CmdInfo.Add(Info);

            // 보이는 목록에 추가
            this.ListView_Command.BeginUpdate();

            ListViewItem Item = new ListViewItem(Cmd);
            Item.SubItems.Add(StringEnum.GetStringValue(Work));
            Item.SubItems.Add(Info.ToString());

            this.ListView_Command.Items.Add(Item);

            this.ListView_Command.EndUpdate();

            // 명령어 목록 갱신 및 적용
            UpdateCmdList();

            // 명령어 목록 저장
            SaveCmdList();
        }
    /// <summary>
    /// キャリブレーション時のオフセット設定
    /// </summary>
    /// <param name="type"></param>
    private void SendCmd_Offset(CMD_TYPE type, int _v)
    {
        string sendCmd = string.Empty;

        switch (type)
        {
        case CMD_TYPE.OFFSET_GYRO_X:
            if (_v >= 0)
            {
                sendCmd = cmd_B0;
            }
            else
            {
                sendCmd = cmd_B1;
            }
            break;

        case CMD_TYPE.OFFSET_GYRO_Y:
            if (_v >= 0)
            {
                sendCmd = cmd_C0;
            }
            else
            {
                sendCmd = cmd_C1;
            }
            break;

        case CMD_TYPE.OFFSET_GYRO_Z:
            if (_v >= 0)
            {
                sendCmd = cmd_D0;
            }
            else
            {
                sendCmd = cmd_D1;
            }
            break;

        case CMD_TYPE.OFFSET_ACCEL_Z:
            if (_v >= 0)
            {
                sendCmd = cmd_E0;
            }
            else
            {
                sendCmd = cmd_E1;
            }
            break;
        }

        sendCmd += Math.Abs(_v).ToString("X4");

        //Debug.LogWarning("[sendCmd] " + sendCmd);
        if (sendCmd.Length == CmdLength)
        {
            DataSend(sendCmd);
        }
    }
    //PPSをセット ※コマンドは送信していない
    public void SetPPS(CMD_TYPE motor, int i)
    {
        MotorSetting _ms = getMotorSetting(motor);

        if (_ms == null)
        {
            return;
        }
        _ms.pps = i;
    }
    //準備OK
    public void ReadyOK(CMD_TYPE motor)
    {
        MotorSetting _ms = getMotorSetting(motor);

        if (_ms == null)
        {
            return;
        }
        _ms.isReady = true;
    }
    //コマンド送信時の動作パターンをセット ※コマンドは送信していない
    public void SetSendPattern(CMD_TYPE motor, SEND_PTN ptn)
    {
        MotorSetting _ms = getMotorSetting(motor);

        if (_ms == null)
        {
            return;
        }
        _ms._ptn = ptn;
    }
    //方向をセット ※コマンドは送信していない
    public void SetDir(CMD_TYPE motor, bool sw)
    {
        MotorSetting _ms = getMotorSetting(motor);

        if (_ms == null)
        {
            return;
        }
        _ms.dir = sw;
    }
Пример #12
0
 /// <summary>
 /// 发送给所有用户
 /// </summary>
 /// <param name="from_user"></param>
 /// <param name="message"></param>
 private void SendToAllClient(CMD_TYPE cmd_type, string message, User from_user = null)
 {
     if (from_user == null)
     {
         from_user = new User();
     }
     for (int i = 0; i < userList.Count; i++)
     {
         SendToClient(cmd_type, from_user, userList[i], message);
     }
 }
    MotorSetting getMotorSetting(CMD_TYPE motor)
    {
        MotorSetting rtn = null;

        foreach (MotorSetting _ms in MotorInfo)
        {
            if (_ms.type == motor)
            {
                rtn = _ms;
            }
        }

        return(rtn);
    }
Пример #14
0
        private void OnErrorDataReceived(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (null == outLine.Data)
            {
                return;
            }

            if (CMD_TYPE.IDENTIFY == this.executeCmdType ||
                CMD_TYPE.HOTUPDTEA == this.executeCmdType)
            {
                return;
            }

            this.CloseProcess();

            Writer.Instance.DelRsa(ConfigIni.GetRsaKeySavePath());

            Console.WriteLine(outLine.Data);
            this.dataReceiveds(outLine.Data, Color.Red);

            this.executeCmdType = CMD_TYPE.NONE;
            this.PrintLog("执行失败,中断操作", Color.Red);

            if (0 == outLine.Data.IndexOf("svn: E160028"))
            {
                if (DialogResult.OK == MessageBox.Show("本地文件过期,点确定进行SVN更新操作", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error))
                {
                    this.SvnUp();
                }
            }
            else if (0 == outLine.Data.IndexOf("svn: E155037"))
            {
                if (DialogResult.OK == MessageBox.Show("SVN被锁定,点确定后进行SVN清理操作", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error))
                {
                    this.SvnClearup();
                }
            }
            else if (0 == outLine.Data.IndexOf("svn: E155009"))
            {
                if (DialogResult.OK == MessageBox.Show("当前打开excel文件冲突,关闭文件后,再点击确定进行清理操作", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error))
                {
                    this.SvnClearup();
                }
            }
            else if (0 == outLine.Data.IndexOf("svn: E"))
            {
                MessageBox.Show("SVN出现未知错误,请在本地手动解决", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public QueryVinceMotorMessage(uint ID, CMD_TYPE FuncCode, STATE_TYPE StateCode, uint StateCodeLength)
        {
            byteList.Clear();
            byteList.Add((byte)ID);
            byteList.Add((byte)FuncCode);
            byteList.Add((byte)0x00);
            byteList.Add((byte)StateCode);
            byteList.Add((Byte)((StateCodeLength >> 8) & 0xFF));
            byteList.Add((Byte)(StateCodeLength & 0xFF));
            //CRC
            uint result = crc_checksum(byteList.ToArray(), byteList.Count());

            byteList.Add((Byte)((result >> 8) & 0xFF));
            byteList.Add((Byte)(result & 0xFF));
        }
Пример #16
0
        public static string GetStringValue(CMD_TYPE Val)
        {
            string Result = null;

            Type type = Val.GetType();

            FieldInfo fi = type.GetField(Val.ToString());
            StringValue[] attrs = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[];

            if (attrs.Length > 0)
            {
                Result = attrs[0].GetStr();
            }

            return Result;
        }
Пример #17
0
        private void DoExecute(Order order)
        {
            this.CloseProcess();

            if (!string.IsNullOrEmpty(order.log))
            {
                this.PrintLog(order.log, Color.Green);
            }

            this.executeCmdType = order.cmdType;
            this.process.Start();
            this.process.StandardInput.WriteLine(order.cmd);
            this.process.BeginOutputReadLine();
            this.process.BeginErrorReadLine();
            this.process.StandardInput.AutoFlush = true;
        }
Пример #18
0
        private void Execute(string cmd, CMD_TYPE cmd_type, string log = null)
        {
            Order order = new Order();

            order.cmdType = cmd_type;
            order.cmd     = cmd;
            order.log     = log;

            if (CMD_TYPE.NONE != this.executeCmdType)
            {
                this.orderQueue.Enqueue(order);
            }
            else
            {
                this.DoExecute(order);
            }
        }
Пример #19
0
        /// <summary>
        /// 向服务端发送消息
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(CMD_TYPE cmd_type, string to_user, string message)
        {
            try
            {
                //添加协议头
                protocol_header = Protocol.Protocol.MakeProtocolMsg(cmd_type, from_user, to_user, message);
                bw.Write(protocol_header);

                //将字符串写入网络流,此方法会自动附加字符串长度前缀
                //bw.Write(message);
                bw.Flush();
            }
            catch
            {
                new NotifyForm("发送失败", true);
            }
        }
Пример #20
0
 /// <summary>
 /// 发送消息给指定的用户
 /// </summary>
 /// <param name="msg_type">消息类型</param>
 /// <param name="from_user">消息发送者</param>
 /// <param name="to_user">接收者</param>
 /// <param name="message">消息内容</param>
 /// <returns></returns>
 private bool SendToClient(CMD_TYPE msg_type, User from_user, User to_user, string message)
 {
     try
     {
         byte[] msg_bytes = Protocol.Protocol.MakeProtocolMsg(msg_type, from_user.MakeAuthCode(), to_user.MakeAuthCode(), message);
         to_user.bw.Write(msg_bytes);
         to_user.bw.Flush();
         return(true);
     }
     catch (ObjectDisposedException e)
     {
         return(false);
     }
     catch (Exception e)
     {
         AddMsgHouseNewMsg(new User(), string.Format("服务端给{0}发送消息失败", to_user.name));
         return(false);
     }
 }
Пример #21
0
        CMD MakeCMD(CMD_TYPE id)
        {
            CMD cmd = new CMD();

            cmd.CMDID = rdb_ShiShi.Checked ? id : id + 0x80;
            //TODO
            cmd.DataMode = 1;

            cmd.Readtype = rdb_Single.Checked ? ReadType.Single :
                           (rdb_Sync.Checked ? ReadType.Sync : ReadType.Seq);

            cmd.AddressMode = rdb_ShiDiZhi.Checked ? ADDRESSMODE.ReadlAddress : ADDRESSMODE.VirtualAdress;

            cmd.NeedReadNoRepied = checkBox15.Checked;



            return(cmd);
        }
        //对应控制表数据长度
        public static uint DataSectorLong(CONTRAL_TAB CTValue, CMD_TYPE CMDvalue)
        {
            if (CMDvalue == CMD_TYPE.CMD_RD)
            {
                return(1);
            }
            switch (CTValue)
            {
            case CONTRAL_TAB.TARGET_POSITON:
                return(2);

            case CONTRAL_TAB.OVERCURRENT:
                return(2);

            case CONTRAL_TAB.DriverID:
                return(1);
            }
            return(0);
        }
Пример #23
0
    /// <summary>
    /// バルブの設定
    /// </summary>
    /// <param name="type">1サイクルの時間/デューティー比/1サイクルを実行する回数</param>
    /// <param name="i"></param>
    public void SetSendCmd(CMD_TYPE type)
    {
        string sendCmd = string.Empty;

        switch (type)
        {
        //1サイクルの時間(ms) ※整数部分
        case CMD_TYPE.ONETIME_INT:
            sendCmd  = cmd_B0;
            sendCmd += ((int)Def_OneTime).ToString("X4");
            break;

        //1サイクルの時間(ms) ※小数部分
        case CMD_TYPE.ONETIME_DEC:
            sendCmd = cmd_B1;
            int _tmp = (int)((Def_OneTime % 1) * 1000);     //1で割った余り=小数点以下の値 → 3桁のusに変換
            sendCmd += _tmp.ToString("X4");
            break;

        //1サイクルのOn時間(ms)  ※1桁目は小数点第1位
        case CMD_TYPE.ONETIME_ON:
            sendCmd  = cmd_B2;
            sendCmd += ((int)(Def_OneTime_On * 10)).ToString("X4");
            break;

        //1サイクルを実行する回数(回)
        case CMD_TYPE.CYCLECNT:
            sendCmd  = cmd_B3;
            sendCmd += Def_CycleCnt.ToString("X4");
            break;

        //1サイクルの時間を初期化
        case CMD_TYPE.ONETIME_RESET:
            sendCmd = cmd_C0 + subCmd_OneTime_Reset;
            break;
        }
        //Debug.LogWarning("[sendCmd] " + sendCmd);
        if (sendCmd.Length == CmdLength)
        {
            DataSend(sendCmd);
        }
    }
        public InspireMotorSingleControlMessage(uint stickID, CMD_TYPE CMDvalue, SINGLE_PARA CTValue)
        {
            byteList.Clear();
            //帧长度
            uint FrameLength = (Byte)0x03;

            byteList.Add((Byte)(FrameLength));
            //ID号
            byteList.Add((Byte)stickID);
            //指令类型
            byteList.Add((Byte)CMDvalue);
            //保留字段
            byteList.Add((Byte)0x00);
            //指令类型
            byteList.Add((Byte)CTValue);
            //校验和
            uint CheckSum = 0;

            for (int i = 0; i < byteList.Count(); i++)
            {
                CheckSum = byteList[i] + CheckSum;
            }
            byteList.Add((Byte)(CheckSum & 0xFF));
        }
Пример #25
0
        /// <summary>
        /// 向服务端发送消息
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(CMD_TYPE cmd_type, string to_user, string message)
        {
            try
            {
                //添加协议头
                protocol_header = Protocol.Protocol.MakeProtocolMsg(cmd_type, from_user, to_user, message);
                bw.Write(protocol_header);

                //将字符串写入网络流,此方法会自动附加字符串长度前缀
                //bw.Write(message);
                bw.Flush();
            }
            catch
            {
                new NotifyForm("发送失败", true);
            }
        }
Пример #26
0
 /// <summary>
 /// 发送消息给指定的用户
 /// </summary>
 /// <param name="msg_type">消息类型</param>
 /// <param name="from_user">消息发送者</param>
 /// <param name="to_user">接收者</param>
 /// <param name="message">消息内容</param>
 /// <returns></returns>
 private bool SendToClient(CMD_TYPE msg_type, User from_user, User to_user, string message)
 {
     try
     {
         byte[] msg_bytes = Protocol.Protocol.MakeProtocolMsg(msg_type, from_user.MakeAuthCode(), to_user.MakeAuthCode(), message);
         to_user.bw.Write(msg_bytes);
         to_user.bw.Flush();
         return true;
     }
     catch (ObjectDisposedException e)
     {
         return false;
     }
     catch (Exception e)
     {
         AddMsgHouseNewMsg(new User(), string.Format("服务端给{0}发送消息失败", to_user.name));
         return false;
     }
 }
Пример #27
0
 /// <summary>
 /// 发送给所有用户
 /// </summary>
 /// <param name="from_user"></param>
 /// <param name="message"></param>
 private void SendToAllClient(CMD_TYPE cmd_type, string message, User from_user = null)
 {
     if (from_user == null) { from_user = new User(); }
     for (int i = 0; i < userList.Count; i++)
     {
         SendToClient(cmd_type, from_user, userList[i], message);
     }
 }
Пример #28
0
        /// <summary>
        /// 按照协议封装消息
        /// </summary>
        /// <param name="msg_type">指令类型CMD_TYPE</param>
        /// <param name="from_user">认证字符</param>
        /// <param name="to_user">发送目的地</param>
        /// <param name="msg">消息内容</param>
        /// <returns></returns>
        public static byte[] MakeProtocolMsg(CMD_TYPE msg_type, string from_user, string to_user, string msg)
        {
            byte[] part1 = new byte[Protocol.CMD_BLOCK_SIZE];
            byte[] part2 = new byte[Protocol.FROM_USER_BLOCK_SIZE];
            byte[] part3 = new byte[Protocol.TO_USER_BLOCK_SIZE];
            byte[] part4 = new byte[4];//msg size
            byte[] part5 = null;
            switch (msg_type)
            {
                case CMD_TYPE.HELLO:
                    part1 = Protocol.HELLO;
                    break;
                case CMD_TYPE.EXIT:
                    part1 = Protocol.EXIT;
                    break;
                case CMD_TYPE.BEAT:
                    part1 = Protocol.BEAT;
                    break;
                case CMD_TYPE.MSG:
                    part1 = Protocol.MSG;
                    break;
                case CMD_TYPE.QUERY:
                    part1 = Protocol.QUERY;
                    break;
                case CMD_TYPE.REAUTH:
                    part1 = Protocol.REAUTH;
                    break;
                case CMD_TYPE.FORWARD:
                    part1 = Protocol.FORWARD;
                    break;
                case CMD_TYPE.CODE:
                    part1 = Protocol.CODE;
                    break;
                default:
                    part1 = Protocol.NULL;
                    break;
            }

            byte[] auth_tmp = System.Text.Encoding.UTF8.GetBytes(from_user);
            if (auth_tmp.Length <= Protocol.FROM_USER_BLOCK_SIZE)
            {
                Array.Copy(auth_tmp, part2, auth_tmp.Length);
            }

            byte[] to_tmp = System.Text.Encoding.UTF8.GetBytes(to_user);
            if (to_tmp.Length <= Protocol.TO_USER_BLOCK_SIZE)
            {
                Array.Copy(to_tmp, part3, to_tmp.Length);
            }

            part5 = Encoding.UTF8.GetBytes(msg);
            part4 = BitConverter.GetBytes(part5.Length);

            List<byte> rlt = new List<byte>();
            rlt.AddRange(part1);
            rlt.AddRange(part2);
            rlt.AddRange(part3);
            rlt.AddRange(part4);
            rlt.AddRange(part5);

            return rlt.ToArray();
        }
Пример #29
0
        /// <summary>
        /// 按照协议解析消息
        /// </summary>
        /// <param name="bytes_in"></param>
        /// <param name="msg_type"></param>
        /// <param name="auth_code"></param>
        /// <param name="to_user"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool ParseProtocolMsg(BinaryReader reader, out CMD_TYPE msg_type, out string auth_code, out string to_user, out string msg)
        {
            bool rlt = false;

            //初始化输出
            msg_type  = CMD_TYPE.NULL;
            auth_code = string.Empty;
            to_user   = string.Empty;
            msg       = string.Empty;

            try
            {
                byte[] msg_bytes = reader.ReadBytes(CMD_BLOCK_SIZE);
                var    msg_str   = BytesToStr(msg_bytes);
                if (msg_str == BytesToStr(Protocol.HELLO))
                {
                    msg_type = CMD_TYPE.HELLO;
                }
                else if (msg_str == BytesToStr(Protocol.EXIT))
                {
                    msg_type = CMD_TYPE.EXIT;
                }
                else if (msg_str == BytesToStr(Protocol.BEAT))
                {
                    msg_type = CMD_TYPE.BEAT;
                }
                else if (msg_str == BytesToStr(Protocol.MSG))
                {
                    msg_type = CMD_TYPE.MSG;
                }
                else if (msg_str == BytesToStr(Protocol.QUERY))
                {
                    msg_type = CMD_TYPE.QUERY;
                }
                else if (msg_str == BytesToStr(Protocol.CODE))
                {
                    msg_type = CMD_TYPE.CODE;
                }
                else if (msg_str == BytesToStr(Protocol.REAUTH))
                {
                    msg_type = CMD_TYPE.REAUTH;
                }
                else if (msg_str == BytesToStr(Protocol.FORWARD))
                {
                    msg_type = CMD_TYPE.FORWARD;
                }
                else if (msg_str == BytesToStr(Protocol.NULL))
                {
                    msg_type = CMD_TYPE.NULL;
                }

                auth_code = Encoding.UTF8.GetString(reader.ReadBytes(Protocol.FROM_USER_BLOCK_SIZE));

                to_user = Encoding.UTF8.GetString(reader.ReadBytes(Protocol.TO_USER_BLOCK_SIZE));

                //消息大小
                Int32  msg_size = reader.ReadInt32();
                byte[] msg_temp = reader.ReadBytes(msg_size);
                msg = Encoding.UTF8.GetString(msg_temp);
                rlt = true;
            }
            catch (IOException e)
            {
                rlt      = true;
                msg_type = CMD_TYPE.EXIT;
                msg      = e.Message;
            }
            catch (Exception e)
            {
                rlt = false;
            }
            return(rlt);
        }
Пример #30
0
        /// <summary>
        /// 按照协议封装消息
        /// </summary>
        /// <param name="msg_type">指令类型CMD_TYPE</param>
        /// <param name="from_user">认证字符</param>
        /// <param name="to_user">发送目的地</param>
        /// <param name="msg">消息内容</param>
        /// <returns></returns>
        public static byte[] MakeProtocolMsg(CMD_TYPE msg_type, string from_user, string to_user, string msg)
        {
            byte[] part1 = new byte[Protocol.CMD_BLOCK_SIZE];
            byte[] part2 = new byte[Protocol.FROM_USER_BLOCK_SIZE];
            byte[] part3 = new byte[Protocol.TO_USER_BLOCK_SIZE];
            byte[] part4 = new byte[4];//msg size
            byte[] part5 = null;
            switch (msg_type)
            {
            case CMD_TYPE.HELLO:
                part1 = Protocol.HELLO;
                break;

            case CMD_TYPE.EXIT:
                part1 = Protocol.EXIT;
                break;

            case CMD_TYPE.BEAT:
                part1 = Protocol.BEAT;
                break;

            case CMD_TYPE.MSG:
                part1 = Protocol.MSG;
                break;

            case CMD_TYPE.QUERY:
                part1 = Protocol.QUERY;
                break;

            case CMD_TYPE.REAUTH:
                part1 = Protocol.REAUTH;
                break;

            case CMD_TYPE.FORWARD:
                part1 = Protocol.FORWARD;
                break;

            case CMD_TYPE.CODE:
                part1 = Protocol.CODE;
                break;

            default:
                part1 = Protocol.NULL;
                break;
            }

            byte[] auth_tmp = System.Text.Encoding.UTF8.GetBytes(from_user);
            if (auth_tmp.Length <= Protocol.FROM_USER_BLOCK_SIZE)
            {
                Array.Copy(auth_tmp, part2, auth_tmp.Length);
            }

            byte[] to_tmp = System.Text.Encoding.UTF8.GetBytes(to_user);
            if (to_tmp.Length <= Protocol.TO_USER_BLOCK_SIZE)
            {
                Array.Copy(to_tmp, part3, to_tmp.Length);
            }

            part5 = Encoding.UTF8.GetBytes(msg);
            part4 = BitConverter.GetBytes(part5.Length);

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

            rlt.AddRange(part1);
            rlt.AddRange(part2);
            rlt.AddRange(part3);
            rlt.AddRange(part4);
            rlt.AddRange(part5);

            return(rlt.ToArray());
        }
Пример #31
0
        private void ComboBoxCalc_KeyUp(object sender, KeyEventArgs e)
        {
            ComboBox cb = sender as ComboBox;

            if (cb == null)
            {
                return;
            }

            Size size = MeasureString(cb.Text);

            cb.Width = size.Width + 17 + 2 + 24;

            MenuItemDelSelected.IsEnabled = false;

            string   cmdResultMsg;
            CMD_TYPE cmdType = CheckCmd(cb.Text, false, out cmdResultMsg);

            if (e.Key == Key.Escape)
            {
                if (cb.Text.Length == 0)
                {
                    this.Hide();
                }
                else
                {
                    cb.Text             = "";
                    mTextResult.Text    = "----";
                    mTextResult.Opacity = 0.5;
                }
            }
            else if (e.Key == Key.Up && mComboBoxCalc.IsDropDownOpen)
            {
                mComboBoxCalc.IsDropDownOpen = false;
            }
            else if (e.Key == Key.Down && !mComboBoxCalc.IsDropDownOpen)
            {
                mComboBoxCalc.IsDropDownOpen = true;
            }
            else if (cmdType != CMD_TYPE.None)
            {
                if (e.Key == Key.Enter && (cmdType & CMD_TYPE.Done) != 0)
                {
                    cmdType = CheckCmd(cb.Text, true, out cmdResultMsg);
                    if ((cmdType & CMD_TYPE.Done) != 0)
                    {
                        cb.Text          = "";
                        mTextResult.Text = "Saved";
                        cmdType          = CMD_TYPE.None;
                    }
                    else
                    {
                        mTextResult.Text = "ERROR?!?!";
                    }
                }
                else
                {
                    mTextResult.Text = cmdResultMsg;
                }

                mTextResult.Opacity =
                    ((cmdType & CMD_TYPE.Done) != 0 || (cmdType & CMD_TYPE.Default) != 0) ? 1:0.5;
            }
            else
            {
                CalText(cb.Text);
                if (e != null && e.Key == Key.Enter)
                {
                    cb.Text             = "";
                    mTextResult.Text    = "Saved";
                    mTextResult.Opacity = 0.5;
                    mCalcList.Add(mCalcCur);
                    mComboBoxCalc.ItemsSource = null;
                    mComboBoxCalc.ItemsSource = mCalcList;
                    mCalcCur = null;
                }
            }

            size = MeasureString(mTextResult.Text);
            mTextResult.Width = size.Width;
            e.Handled         = true;
        }
Пример #32
0
        /// <summary>
        /// 按照协议解析消息
        /// </summary>
        /// <param name="bytes_in"></param>
        /// <param name="msg_type"></param>
        /// <param name="auth_code"></param>
        /// <param name="to_user"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool ParseProtocolMsg(BinaryReader reader, out CMD_TYPE msg_type, out string auth_code, out string to_user, out string msg)
        {
            bool rlt = false;
            //初始化输出
            msg_type = CMD_TYPE.NULL;
            auth_code = string.Empty;
            to_user = string.Empty;
            msg = string.Empty;

            try
            {
                byte[] msg_bytes = reader.ReadBytes(CMD_BLOCK_SIZE);
                var msg_str = BytesToStr(msg_bytes);
                if (msg_str == BytesToStr(Protocol.HELLO))
                {
                    msg_type = CMD_TYPE.HELLO;
                }
                else if (msg_str == BytesToStr(Protocol.EXIT))
                {
                    msg_type = CMD_TYPE.EXIT;
                }
                else if (msg_str == BytesToStr(Protocol.BEAT))
                {
                    msg_type = CMD_TYPE.BEAT;
                }
                else if (msg_str == BytesToStr(Protocol.MSG))
                {
                    msg_type = CMD_TYPE.MSG;
                }
                else if (msg_str == BytesToStr(Protocol.QUERY))
                {
                    msg_type = CMD_TYPE.QUERY;
                }
                else if (msg_str == BytesToStr(Protocol.CODE))
                {
                    msg_type = CMD_TYPE.CODE;
                }
                else if (msg_str == BytesToStr(Protocol.REAUTH))
                {
                    msg_type = CMD_TYPE.REAUTH;
                }
                else if (msg_str == BytesToStr(Protocol.FORWARD))
                {
                    msg_type = CMD_TYPE.FORWARD;
                }
                else if (msg_str == BytesToStr(Protocol.NULL))
                {
                    msg_type = CMD_TYPE.NULL;
                }

                auth_code = Encoding.UTF8.GetString(reader.ReadBytes(Protocol.FROM_USER_BLOCK_SIZE));

                to_user = Encoding.UTF8.GetString(reader.ReadBytes(Protocol.TO_USER_BLOCK_SIZE));

                //消息大小
                Int32 msg_size = reader.ReadInt32();
                byte[] msg_temp = reader.ReadBytes(msg_size);
                msg = Encoding.UTF8.GetString(msg_temp);
                rlt = true;
            }
            catch (IOException e)
            {
                rlt = true;
                msg_type = CMD_TYPE.EXIT;
                msg = e.Message;
            }
            catch (Exception e)
            {
                rlt = false;
            }
            return rlt;
        }
 public InspireMotorBroadcastMessage(CMD_TYPE CMDvalue, CONTRAL_TAB CTValue)
 {
     this.CMDvalue = CMDvalue;
     this.CTValue  = CTValue;
 }