Пример #1
0
        /// ------------------------------------------------------------------------------
        /// <summary>
        /// 名    称: FrmMain_Load(加载窗体事件程序)
        /// 功    能: 在构造函数之后执行的程序
        /// </summary>
        /// <returns>无</returns>
        /// ------------------------------------------------------------------------------
        //3. 加载窗体事件程序
        private void FrmMain_Load(object sender, EventArgs e)
        {
            string s1;

            this.tb_statue.Text = "运行状态:FrmMain_Load开始,调用PCNode类内部函数,寻找PCNode...";
            this.tb_statue.Refresh();

            if (uart1 != null && uart1.IsOpen)
            {
                uart1.close();
            }
            s1    = pcNode1.findPCNode();        //调用PCNode类内部函数,寻找PCNode
            uart1 = pcNode1.PCNode_Uart1;

            this.tb_statue.Text = "运行状态:找到" + s1;
            this.tb_statue.Refresh();

            lbl_PC_Node_State.Text = s1;

            //保留编程参考:绑定DataReceived事件(即串口接收中断处理函数)
            if (uart1 != null)
            {
                UARTport = uart1.port;
                UARTport.DataReceived += new System.IO.Ports. //添加串口对象变量的接收事件的响应函数
                                         SerialDataReceivedEventHandler(this.UARTport_recvData);
            }
            //uart1.setReceInt(2);           //设置触发DataRecieved事件的字节数(目的是关闭该事件)
            //uart1.sendData(B1, B1.Length); //发送握手数据
        }
Пример #2
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);
        }