Exemple #1
0
        static void MainResult()
        {
            //ConsoleWin32Helper.ShowNotifyIcon();
            //ConsoleWin32Helper.DisableCloseButton(Console.Title);
            SerialPortUtils.InitialSerialPort();
            Console.WriteLine("程序启动");
            SocketServer socketServer = new SocketServer(8081, 1024);

            socketServer.Start();
            //var task = Task.Run(async () =>
            //{
            //    Console.WriteLine($" async ThreadId {Thread.CurrentThread.ManagedThreadId}");
            //    await socketServer.BeginListen();
            //});
            //Console.WriteLine($" sync ThreadId {Thread.CurrentThread.ManagedThreadId}");
            Console.ReadKey();
            //while (true)
            //{
            //  Application.DoEvents();
            //  if (ConsoleWin32Helper._IsExit)
            //  {
            //    MonitorInput();
            //    break;
            //  }
            //}
        }
Exemple #2
0
        /// <summary>
        /// 处理接收到的数据
        /// </summary>
        public void ProcessReceivedData(AppSession session, string data)
        {
            string str = data;

            switch (str)
            {
            case "报警数据":
                byte[] bs = null;
                Logger("查询报警数据");
                if (SerialPortUtils.AlarmData.Count > 0)     //有报警数据
                {
                    bs = SerialPortUtils.AlarmData.Dequeue();
                }
                else
                {
                    bs = Encoding.UTF8.GetBytes("0\r\n");
                }
                session.Send(bs, 0, bs.Length);
                break;

            default:      //默认是查询向串口查询教室设备数据
                          //发送数据
                var    dataBuff  = data.StrToHexByte();
                string classroom = Convert.ToString(dataBuff[2], 16) + Convert.ToString(dataBuff[3], 16);
                if (dataBuff.Length > 4)
                {
                    if (dataBuff[4] == 0x1f)      //接收到的是查询命令
                    {
                        SerialPortUtils.SendSearchCmd(dataBuff);
                        //获取串口返回的数据
                        byte[] returnData = GetReturnData(classroom);
                        //将获取到的数据发送回去
                        session.Send(returnData.HexToStr() + "\r\n");
                        Console.WriteLine("服务端给客户端发送完成数据:" + session.RemoteEndPoint);
                    }
                    else           //接收到的是执行命令
                    {
                        Console.WriteLine("接收到执行命令,发送完成");
                        SerialPortUtils.SendCmd(dataBuff);
                    }
                }
                break;
            }
        }
Exemple #3
0
        public byte[] GetReturnData(string classroom)
        {
            //等待数据初始化

            SerialPortUtils.countdown.Wait(TimeSpan.FromSeconds(5));
            SerialPortUtils.countdown.Reset();
            if (!SerialPortUtils.DataDictionary.ContainsKey(classroom))
            {
                Console.WriteLine("-------获取串口数据失败,返回1个长度数据包-----");
                return(new byte[] { 0 });
            }
            Console.WriteLine("=========获取到串口完整数据=======");
            Console.WriteLine($"字典key为:{classroom} 的数据被消费");
            byte[] data = SerialPortUtils.DataDictionary[classroom];
            SerialPortUtils.DataDictionary.Remove(classroom);
            //TODO 用处:测试BUG,
            SerialPortUtils.SendCmd(data);
            return(data);
        }
Exemple #4
0
        /// <summary>
        /// 处理接收到的数据
        /// </summary>
        public void ProcessReceivedData(Socket socket, string classroom, byte[] data)
        {
            string str = Encoding.UTF8.GetString(data, 0, data.Length);

            switch (str)
            {
            case "报警数据":
                byte[] bs = null;
                if (SerialPortUtils.AlarmData.Count > 0)     //有报警数据
                {
                    bs = SerialPortUtils.AlarmData.Dequeue();
                }
                else
                {
                    bs = new byte[] { 0 };
                }
                socket.Send(bs);
                break;

            default:      //默认是查询向串口查询教室设备数据
                          //发送数据
                if (data.Length > 4)
                {
                    if (data[4] == 0x1f)      //接收到的是查询命令
                    {
                        SerialPortUtils.SendSearchCmd(data);
                        //获取串口返回的数据
                        byte[] returnData = GetReturnData(classroom);
                        //将获取到的数据发送回去
                        socket.Send(returnData);

                        Console.WriteLine("服务端给客户端发送完成数据:" + socket.RemoteEndPoint);
                    }
                    else           //接收到的是执行命令
                    {
                        Console.WriteLine("接收到执行命令,发送完成");
                        SerialPortUtils.SendCmd(data);
                    }
                }
                break;
            }
        }