Example #1
0
        /// ------------------------------------------------------------------------------
        /// <summary>
        /// 名    称: UARTport_recvData(串口接收事件的响应函数)
        /// 功    能: 串口接收事件的响应
        /// </summary>
        /// <returns>无</returns>
        /// ------------------------------------------------------------------------------
        //-------------------------------串口接收事件的响应函数---------------------------
        private void UARTport_recvData(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int i;
            //int tmp, brt;
            string s1 = string.Empty;

            Thread.Sleep(100);
            PublicVar.g_ReceiveByteArray = uart1.recvData();    //调用串口接收函数
            if (PublicVar.g_ReceiveByteArray == null)
            {
                return;
            }
            //if (PublicVar.g_ReceiveByteArray.Length > 0)
            //{
            //    this.Txt_recv2.Text = "Interrupt--- " + System.Text.Encoding.Default.GetString(PublicVar.g_ReceiveByteArray);
            //    if (PublicVar.g_ReceiveByteArray[2] == 'M')
            //    {
            //        tmp = (int)(PublicVar.g_ReceiveByteArray[4] * 255 + PublicVar.g_ReceiveByteArray[3]);
            //        brt = (int)(PublicVar.g_ReceiveByteArray[6] * 255 + PublicVar.g_ReceiveByteArray[5]);

            //    }
            //}
            for (i = 0; i < PublicVar.g_ReceiveByteArray.Length; i++)
            {
                this.Txt_recv2.Text += "-" + PublicVar.g_ReceiveByteArray[i].ToString("D3") + " ";
            }
        }
Example #2
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);     // 返回接收数据数据的首地址
        }
Example #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);
        }