public void WriteHexData(string strHex)
        {
            byte[] buf = ShujuChuli.HexStringToBytes(strParam.ToUpper());
            try
            {
                if (comm.IsOpen)
                {
                    comm.Write(buf, 0, buf.Length);

                    Console.WriteLine(strParam);
                    Log.Debug("CmdHex-->" + strParam);//问题调查时可用
                }
                else
                {
                    ConnectPort();
                    try
                    {
                        if (comm.IsOpen)
                        {
                            //  recEvent.WaitOne();
                            comm.Write(buf, 0, buf.Length);
                        }
                    }
                    catch (Exception)
                    {
                        comm = new SerialPort();
                        //   MessageBox.Show(ex.Message);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 当前位置获取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button13_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            EquipmentCmd.Instance.SendCommand(button.Text, "", out res);
            string resHexs = ByteToHexString(EquipmentCmd.Instance.resPort);

            if (resHexs.IndexOf("34 0A 16 06 05 ") != -1)
            {
                dangQianWeiZhi_X = ShujuChuli.DiGaoBaWei(ShujuChuli.HexStringToBytes((ShujuChuli.GetIndexString(resHexs, 5)))[0], ShujuChuli.HexStringToBytes((ShujuChuli.GetIndexString(resHexs, 6)))[0]);
                dangQianWeiZhi_Y = ShujuChuli.DiGaoBaWei(ShujuChuli.HexStringToBytes((ShujuChuli.GetIndexString(resHexs, 7)))[0], ShujuChuli.HexStringToBytes((ShujuChuli.GetIndexString(resHexs, 8)))[0]);
            }


            txtXianShi.AppendText(DateTime.Now.ToString("hh时mm分ss秒") + "  " + button.Text + "-->" + res + "    " + resHexs + "\r\n");
        }
        public bool SendCmd(string strCmd, string strPram, out string str)
        {
            delay = DelayCmd.WhetherCmd(strCmd);

            //解析发送过来的字符串命令
            string strHex  = StrPramToHexPram.StrToHex(strCmd);
            string sendHex = (strHex + " " + ShujuChuli.StrToHex(param) + " 81");

            relationship.RelShip(strCmd);

            if (strHex != "")
            {
                if (delay == true)//命令是一条延迟命令
                {
                    lock (delaylook)
                    {
                        SendHex(sendHex);//发送命令
                        recStr = recDataStr;
                        Log.Debug(strCmd + "-->命令返回-->" + recStr);
                        delyEvent.WaitOne();//等待延迟数据返回
                        return(true);
                    }
                }
                else
                {
                    SendHex(sendHex);//发送命令

                    recStr = recDataStr;
                    Log.Debug(strCmd + "-->命令返回-->" + recStr);
                    return(true);
                }
            }
            else
            {
                recStr = "status=NOT";
                return(true);
            }
        }
Пример #4
0
        /// <summary>
        /// 解析接受到的Hex数据
        /// </summary>
        /// <param name="strHex">串口数据回传的Hex数据,根据串口回传数据作判断,并回传判断结果</param>

        public static string RecDataToHexPram(string strHex)
        {
            string strRec = "status=OK";

            string str = strHex.Replace(" ", "");
            //取出特定的数据
            string strLength  = str.ToUpper().Substring(2, 2);                //数据长度
            string strAddress = str.ToUpper().Substring(4, 2);                //设备地址
            string strModule  = str.ToUpper().Substring(6, 2);                //模块
            string strPort    = str.ToUpper().Substring(8, 2);                //端口
            string strStr     = str.ToUpper().Substring(10, str.Length - 12); //取出数据

            #region 01模块 检测
            if (strModule == "01" && strStr == "FF")//检测
            {
                strRec = "status=NO";
            }
            #endregion

            #region  1站取放电机可以运动
            else if (strAddress == "11" && strModule == "04" && strPort == "0C" && strStr == "FF")
            {
                strRec = "status=NO";
            }
            #endregion

            #region 03模块 光源调节
            //else if (strAddress == "13" && strModule == "03")//光源亮度调节
            //{
            //    strRec = "status=" + Convert.ToInt32(strStr, 16).ToString();
            //}
            else if (strAddress == "14" && strModule == "04") //颜色读取
            {
                string data = strStr.Substring(0, 2);         //取出00
                if (data == "00")
                {
                    string R = ShujuChuli.DiGaoHexStringToString(strStr.Substring(2, 4));
                    string G = ShujuChuli.DiGaoHexStringToString(strStr.Substring(6, 4));
                    string B = ShujuChuli.DiGaoHexStringToString(strStr.Substring(10, 4));
                    strRec = "R=" + R + ";" + "G=" + G + ";" + "B=" + B;
                }
            }
            #endregion

            #region 6站电机
            else if (strModule == "06" && strPort == "01")
            {
                string X         = ShujuChuli.DiGaoHexStringToString(strStr.Substring(0, 4));
                string Y         = ShujuChuli.DiGaoHexStringToString(strStr.Substring(4, 4));
                string speed     = ShujuChuli.HexStringToInt(strStr.Substring(8, 2));
                string jiaSpeed  = ShujuChuli.HexStringToInt(strStr.Substring(10, 2));
                string jianSpeed = ShujuChuli.HexStringToInt(strStr.Substring(12, 2));
                string xiLv      = ShujuChuli.DiGaoHexStringToString(strStr.Substring(14, 4));
                strRec = "X=" + X + ";" + "Y=" + Y + ";" + "Speed=" + speed + ";" + "Acceleration=" + jiaSpeed + ";" + "Deceleration=" + jianSpeed + ";" + "Slope=" + xiLv;
            }
            else if (strModule == "06" && strPort == "05")
            {
                string X = ShujuChuli.DiGaoHexStringToString(strStr.Substring(0, 4));
                string Y = ShujuChuli.DiGaoHexStringToString(strStr.Substring(4, 4));
                strRec = "X=" + X + ";" + "Y=" + Y;
            }
            #endregion


            return(strRec);
        }
        public bool ConnectPort()
        {
            //握手状态指示
            bool connectOK = false;

            #region 握手

            while (ws)
            {
                if (comm.IsOpen)
                {
                    comm.Close();
                    comm.Dispose();
                }
                else
                {
                    int a = 0;
                    for (int i = 0; i < 5; i++)
                    {
                        string[] ports = SerialPort.GetPortNames();
                        foreach (string port in ports)
                        {
                            comm.PortName = port;
                            comm          = new SerialPort(port);

                            comm.BaudRate = 19200;
                            comm.Parity   = Parity.None;
                            comm.StopBits = StopBits.One;
                            comm.DataBits = 8;


                            // comm.DataReceived += new SerialDataReceivedEventHandler(comm_DataReceived);//串口数据回调函数

                            //comm.DataReceived +=comm_DataReceived;
                            try
                            {
                                comm.Open();

                                //开启一个线程读取串口数据
                                //把读取线程设置为后台线程

                                _readThread.Start();


                                byte[] sbuf = ShujuChuli.HexStringToBytes(str_woshou);
                                comm.Write(sbuf, 0, sbuf.Length);

                                ChaZhaoChuanKouCiShu = woShouEvent.WaitOne(2000);//等待返回函数将mEvent置为mEvent.Set();
                                if (ChaZhaoChuanKouCiShu == true)
                                {
                                    //将串口名称加载在指定的控件上
                                    // strPort = port;
                                    connectOK = true;
                                    ws        = false;
                                    break;
                                }
                                else
                                {
                                    _readThread.Abort();
                                    comm.Close();
                                    comm.Dispose();
                                }
                            }
                            catch (Exception)
                            {
                                _readThread.Abort();
                                comm.Close();
                                comm.Dispose();
                                //现实异常信息给客户。
                                //  MessageBox.Show(ex.Message);
                            }
                        }
                        a += 1;
                        if (ChaZhaoChuanKouCiShu == true)
                        {
                            //  MessageBox.Show("无法找到串口!请确认硬件问题.");
                            ws = false;
                            break;
                        }
                        if (a == 5)
                        {
                            ws = false;
                            comm.Close();
                            connectOK = false;
                            break;
                        }
                    }
                }
            }
            #endregion
            return(connectOK);
        }