Esempio n. 1
0
        /// ------------------------------------------------------------------------------
        /// <summary>
        ///函数名:
        ///函数执行状态:
        ///参数说明:
        ///
        ///功能概要:
        /// </summary>
        /// ------------------------------------------------------------------------------
        public byte[] PCNodeSendReturn(byte[] SendByteArray, int cnt, int time)
        {
            byte[] recvData = null;
            int    i;

            try
            {
                for (i = 0; i < cnt; i++)
                {
                    uart1.setReceInt(200);                               //测试串口接收中断使用
                    uart1.sendData(SendByteArray, SendByteArray.Length); // 通过串口发送数据
                    System.Threading.Thread.Sleep(time);
                    recvData = uart1.recvData();                         // 通过串口接收数据
                    if (recvData.Length >= 1)
                    {
                        break;
                    }
                }
            }
            catch
            {
                return(recvData); //产生错误,返回false
            }
            return(recvData);     // 返回接收数据数据的首地址
        }
Esempio n. 2
0
        /// ------------------------------------------------------------------------------
        /// <summary>
        /// 名    称: btnVerifyRF_Click(单击“验证RF收发”按钮)
        /// 功    能: 单击“验证RF收发”按钮 ,产生响应
        /// </summary>
        /// <returns>无</returns>
        /// ------------------------------------------------------------------------------
        private void btnVerifyRF_Click(object sender, EventArgs e)
        {
            // 1. 临时变量声明
            byte n;             //测试数据字节数
            int  i;
            //byte len;           //[命令+数据]字节数(总帧长度-1B帧头 - 1B len - 1B帧尾)
            string s1;
            byte   hw_Addr;

            ////this.Txt_recv1.Text = "";
            //this.Txt_recv2.Text = "";

            //this.Txt_recv1.Refresh();
            //this.Txt_recv2.Refresh();


            btnVerifyRF.Enabled = false;

            // 2. 临时变量赋值
            //n = Convert.ToByte(tbTxtDataLen.Text);
            hw_Addr = Convert.ToByte(hwAddr.Text);

            //byte[] writeDataArray = System.Text.Encoding.Default.GetBytes(tbWriteData2.Text);

            //if (n > 50||n<1)
            //{
            //    this.tb_statue.Text = "发送字节数不合适,合理范围是1~50";
            //    return;
            //}

            //len = (byte)(n + 1);
            //len = (byte)(n + 1);    //增加一个是否检测能量场的字节

            byte[] SendByteArray = new byte[6];

            // 组帧
            try
            {
                SendByteArray[0] = (byte)'P';      //帧头

                SendByteArray[1] = (byte)3;        //帧长

                SendByteArray[2] = hw_Addr;        //被访问的硬件过滤地址

                //if(0 < int.Parse(tbWriteData2.Text) || int.Parse(tbWriteData2.Text)>255 )
                //{
                //    this.tb_statue.Text = "请输入0~255的数";
                //    this.tb_statue.Refresh();
                //    btnVerifyRF.Enabled = true;
                //    return;
                //}

                try
                {
                    SendByteArray[3] = byte.Parse(nodeSelect.Text);
                }
                catch
                {
                    this.tb_statue.Text = "请选择要发送的节点号";
                    this.tb_statue.Refresh();
                    btnVerifyRF.Enabled = true;
                    return;
                }

                try
                {
                    SendByteArray[4] = byte.Parse(tbWriteData2.Text);
                }
                catch
                {
                    this.tb_statue.Text = "请输入0~255的数";
                    this.tb_statue.Refresh();
                    btnVerifyRF.Enabled = true;
                    return;
                }


                SendByteArray[5] = (byte)'C';      //帧尾

                //
                //发送、接收、显示
                //sendRecv(SendByteArray,1,5500);
                uart1.setReceInt(100);   //测试串口接收中断使用
                //uart1.sendData(SendByteArray, SendByteArray.Length);  // 通过串口发送数据
                sendRecv(SendByteArray, 2, 300);

                //System.Threading.Thread.Sleep(time);

                //s1 = s1 + "已经发送,Data 为"+ byte.Parse(tbWriteData2.Text) + ",等待接收中断...收到TargetNode回送信息!";
                //this.tb_statue.Text = s1;
                //this.tb_statue.Refresh();


                btnVerifyRF.Enabled = true;
            }
            catch
            {
                //this.Txt_recv1.Text = "有误:可能没串口!";
                this.tb_statue.Text = "发送消息失败,没有找到第" + nodeSelect.Text + "号TargetNode!";
                this.tb_statue.Refresh();
                btnVerifyRF.Enabled = true;
            }
        }
Esempio n. 3
0
        /// ------------------------------------------------------------------------------
        /// <summary>
        /// 功    能:寻找PCNode。
        /// 形    参:无
        /// <returns>无</returns>
        /// -----------------------------------------------------------------------------
        public string findPCNode()
        {
            int    i, j;
            string s1;

            int[] uartNoArray;   //
            System.IO.Ports.SerialPort uartport;
            //1.临时变量

            bool result = false;     //

            byte[] recvData = null;  ////串口接收返回的信息

            byte[] shakeframe = { (byte)'S',        11, (byte)'T',       0xa,       0x0,
                                  (byte)'W', (byte)'h', (byte)'o', (byte)'a', (byte)'r',(byte)'e',
                                  (byte)'y', (byte)'o', (byte)'u', (byte)'?',
                                  (byte)'E', (byte)'N' };

            //初始化内部调用函数临时变量
            s1 = string.Empty;    //初始化临时字符串(置空)

            //创建_port1对象
            uartport = new System.IO.Ports.SerialPort();
            //查询所有串口,串口数在uartNoArray[0]中

            uartNoArray = Uart.findCOM();
            //没有找到串口情况
            if (uartNoArray[0] == 0)
            {
                return(s1 = "没有找到USB串口");
            }

            //有找到串口情况,遍历搜寻串口
            for (i = uartNoArray[0]; i >= 1; i--)
            {
                string comName = "COM" + uartNoArray[i].ToString(); //串口名称
                //
                uart1 = new Uart(uartport, comName, 9600);          //初始化串口工具类
                // pcNode1 = new PCNode(uart1);            //初始化KW01操作工具类

                //PE和PC节点握手
                //s1 = shake(shakeframe);        //与PC节点握手

                //2.打开串口
                result = uart1.open();
                if (!result)
                {
                    continue;
                }

                uart1.setReceInt(200);                                  //设置触发DataRecieved事件的字节数(目的是关闭该事件)

                result = uart1.sendData(shakeframe, shakeframe.Length); //发送握手数据

                if (!result)
                {
                    uart1.close(); continue;
                }
                //
                System.Threading.Thread.Sleep(100);   //线程休眠0.1s

                recvData = uart1.recvData();          //接收数据


                if (recvData == null || recvData.Length == 0 || recvData[0] != 'M')
                {
                    uart1.close(); continue;
                }

                //将握手成功,数组转换成字符串,返回
                for (j = 3; j < recvData[1] + 2; j++)
                {
                    s1 += (char)recvData[j];
                }

                s1 = comName + ":" + s1;
                break;
            }
            if (i == 0)
            {
                s1 = "有USB串口,但无PCNode";
            }

            return(s1);
        }