Exemplo n.º 1
0
        //************************************
        //produce data by timer
        //************************************
        private void TimerProduceTick(object sender, EventArgs e)
        {
            var stopWatch = new Stopwatch();
            stopWatch.Start();
            foreach (var item in Global.DtuList)
            {
                var record = new GprsDataRecord();
                _cacheLock.EnterWriteLock();
                try
                {
                    record.Initialize();
                    record.m_userid = item.Key;//消息数据包设置Gprs号码
                    DateTime now = DateTime.Now;
                    DateTimeFormatInfo format = CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat;
                    format.DateSeparator = "/";
                    format.ShortDatePattern = @"yyyy/MM/dd/HH/mm/ss";
                    record.m_recv_date = now.ToString("d", format);//消息数据包设置上报时间
                    item.Value.RecvDate = now;//产生数据时时间

                    string allstring = "T1-" + ((ushort)_rand.Next(1, 100) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "T2-" + ((ushort)_rand.Next(1, 100) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "T3-" + ((ushort)_rand.Next(1, 100) * 0.8).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "T4-" + ((ushort)_rand.Next(1, 100) * 0.79).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "T5-" + ((ushort)_rand.Next(1, 100) * 0.99).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "T6-" + ((ushort)_rand.Next(1, 100) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "F1-" + ((ushort)_rand.Next(1000, 30000) * 6.7).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "F2-" + ((ushort)_rand.Next(2000, 7700) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "A1-" + ((ushort)_rand.Next(1, 100) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "A2-" + ((ushort)_rand.Next(1, 100) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "A3-" + ((ushort)_rand.Next(1, 100) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "P1-" + ((ushort)_rand.Next(1, 1000) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "W1-" + ((ushort)_rand.Next(1, 100) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    allstring += "v1-" + ((ushort)_rand.Next(1, 10000) * 0.89).ToString(CultureInfo.InvariantCulture) + " ";
                    record.m_data_len = (ushort)allstring.Length;
                    //byte[] byteArray =;
                    record.m_data_buf = System.Text.Encoding.Default.GetBytes(string.Copy(allstring));
                }

                finally
                {
                    _cacheLock.ExitWriteLock();
                }
                Global.Dqueue.EnQueueItem(record);

            }
            stopWatch.Stop();
        }
Exemplo n.º 2
0
        // ReSharper restore FunctionNeverReturns
        public void EnQueueItem(GprsDataRecord datain)
        {
            MUnblockHandlerEvent.WaitOne();
            MHandlerFinishedEvent.Reset();

            MCurrentWriteQueue.Enqueue(datain);
            //m_ProducerData.WriteLine(datain); // logging

            MDataAvailableEvent.Set();
            MHandlerFinishedEvent.Set();
        }
Exemplo n.º 3
0
        //************************************************
        //非阻塞模式下数据处理定时器
        //************************************************
        private void Timer3Tick(object sender, EventArgs e)
        {
            //int i;
            var recdPtr = new GprsDataRecord();
            var mess = new StringBuilder(100);

            //读取DTU数据
            if (ServMode == 1)
            {
                if (Gprs.Gprs.do_read_proc(ref recdPtr, mess, true/*checkBox2.Checked*/) >= 0)
                {
                    //RefreshList();
                    switch (recdPtr.m_data_type)
                    {
                        case 0x01:	//注册包
                            var userInfo = new GprsUserInfo();
                            //ushort usPort;
                            if (Gprs.Gprs.get_user_info(recdPtr.m_userid, ref userInfo) == 0)
                            {
                                //已经注册过
                                AddText("\n" + recdPtr.m_userid + "---注册" + "\r\n");
                                //for (i = 0; i <	listView1.Items.Count; i++)
                                //    if (listView1.Items[i].Text == user_info.m_userid)
                                //    {
                                //        listView1.Items[i].SubItems.Add(user_info.m_logon_date);
                                //        listView1.Items[i].SubItems.Add(user_info.m_update_time.ToString());
                                //        listView1.Items[i].SubItems.Add(Gprs.Gprs.inet_ntoa(Gprs.Gprs.ntohl(user_info.m_sin_addr)));
                                //        usPort = user_info.m_sin_port;
                                //        listView1.Items[i].SubItems.Add(usPort.ToString());
                                //        listView1.Items[i].SubItems.Add(Gprs.Gprs.inet_ntoa(Gprs.Gprs.ntohl(user_info.m_local_addr)));
                                //        usPort = user_info.m_local_port;
                                //        listView1.Items[i].SubItems.Add(usPort.ToString());
                                //        return;
                                //    }
                                //没有注册过
                            }
                            //RefreshList();
                            break;
                        case 0x02:	//注销包
                            AddText("\n" + recdPtr.m_userid + "---注销" + "\r\n");
                            //for (i = 0; i <	listView1.Items.Count; i++)
                            //    if (listView1.Items[i].Text == recdPtr.m_userid)
                            //    {
                            //        listView1.Items[i].Remove();
                            //        break;
                            //    }
                            break;
                        case 0x04:	//无效包
                            break;
                        case 0x09:	//数据包
                            //if(checkBox1.Checked)
                            //    AddText("\r\n" + recdPtr.m_userid +"---"+ recdPtr.m_recv_date + "---"+recdPtr.m_data_len.ToString()+"\r\n"
                            //        +StrToHex(recdPtr.m_data_buf,recdPtr.m_data_len)+"\r\n");
                            //else
                            //    AddText("\r\n" + recdPtr.m_userid +"---"+ recdPtr.m_recv_date + "---"+recdPtr.m_data_len.ToString()+"\r\n"
                            //        +System.Text.Encoding.Default.GetString(recdPtr.m_data_buf)+"\r\n");
                            break;
                        case 0x0d:
                            //AddText("\n"+recdPtr.m_userid+"---参数设置成功"+"\r\n");
                            //config.timer3.Enabled = false;++++++++++++++
                            //config.button2.Enabled = true;++++++++++++++
                            MessageBox.Show(Resources.FMain_Timer3Tick_参数设置成功, Resources.FMain_Timer3Tick_信息, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        case 0x0b:
                            //AddText("\n"+recdPtr.m_userid+"---参数查询成功"+"\r\n");
                            //config.readconf();++++++++++++++
                            //config.timer2.Enabled = false;++++++++++++++
                            //config.button1.Enabled = true;++++++++++++++
                            MessageBox.Show(Resources.FMain_Timer3Tick_参数查询成功, Resources.FMain_Timer3Tick_信息, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        case 0x06:
                            //AddText("\n"+recdPtr.m_userid+"---断开PPP连接成功"+"\r\n");
                            break;
                        case 0x07:
                            //AddText("\n"+recdPtr.m_userid+"---停止向DSC发送数据"+"\r\n");
                            break;
                        case 0x08:
                            //AddText("\n"+recdPtr.m_userid+"---允许向DSC发送数据"+"\r\n");
                            break;
                        case 0x0A:
                            //AddText("\n"+recdPtr.m_userid+"---丢弃用户数据"+"\r\n");
                            break;
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void DoubleQueue_OnResponseData(ushort id, GprsDataRecord values)
        {
            //消息经过缓冲处理后的处理函数。(显示、存储)
            //Registers a task that will wait for a WaitHandle and will wait forever (-1 means
            //never expire) and has a state object, and should execute only once++++++++
            //DateTime now = DateTime.Now;
            //DateTimeFormatInfo format = CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat;
            //format.DateSeparator = "-";
            //format.ShortDatePattern = @"yyyy/MM/dd";
            //string timestring = now.ToString("d", format);
            //m_Log.Info(timestring + "_" + values.Id.ToString());//LOG4
            //m_Log.Info(values.m_userid + "---" + values.m_recv_date + "---" + values.m_data_len.ToString() + "\r\n" + global.StrToHex(values.m_data_buf, values.m_data_len) + "\r\n");//LOG4

            ThreadPool.QueueUserWorkItem(ThreadPoolTask, values);
            ////++++++++++++++++++++++++++++++++++++++++++++++++++++
        }
Exemplo n.º 5
0
        //*******************************************
        //消息模式时定义消息的响应函数
        //*******************************************
        protected override void WndProc(ref Message m)
        {
            //int i;

            //响应DTU消息
            if (m.Msg == Gprs.Gprs.WmDtu)
            {
                var recdPtr = new GprsDataRecord();
                var mess = new StringBuilder(100);

                //开发包函数,读取DTU数据
                if (Gprs.Gprs.do_read_proc(ref recdPtr, mess, true/*checkBox2.Checked*/) >= 0)
                {
                    //RefreshList();
                    switch (recdPtr.m_data_type)
                    {
                        case 0x01:	//注册包
                            var userInfo = new GprsUserInfo();
                            //ushort usPort;
                            if (Gprs.Gprs.get_user_info(recdPtr.m_userid, ref userInfo) == 0)//开发包函数,通过ID获取DTU信息
                            {
                                //已经注册过
                                //AddText("\n"+recdPtr.m_userid + "---注册"+"\r\n");
                                //	RefreshList();//刷新终端登陆列表
                            }
                            break;
                        case 0x02:	//注销包
                            //AddText("\n"+recdPtr.m_userid + "---注销"+"\r\n");
                            //收到的是注销包,从终端登陆列表中删除该DTU信息
                            //for (i = 0; i <	listView1.Items.Count; i++)
                            //    if (listView1.Items[i].Text == recdPtr.m_userid)
                            //    {
                            //        listView1.Items[i].Remove();
                            //        break;
                            //    }
                            break;
                        case 0x04:	//无效包
                            break;
                        case 0x09:	//数据包
                            //16进制显示收到的数据
                            //if (checkBox1.Checked)
                            {
                                //DtuStateObject state = new DtuStateObject();
                                //state.Aera_IrradiatedSum = 10;
                                //Dqueue.EnQueueItem(state);
                                //logger.Info("Receive a message from Gprs Modules!.");
                                //AddText("\r\n" + recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
                                //    + StrToHex(recdPtr.m_data_buf, recdPtr.m_data_len) + "\r\n");
                                //

                                Global.Dqueue.EnQueueItem(recdPtr);
                            }
                            // else
                            //显示数据
                            //AddText("\r\n" + recdPtr.m_userid + "---" + recdPtr.m_recv_date + "---" + recdPtr.m_data_len.ToString() + "\r\n"
                            //    + System.Text.Encoding.Default.GetString(recdPtr.m_data_buf) + "\r\n");
                            break;
                        case 0x0d:
                            //收到参数设置成功数据包,取消参数设置超时定时器
                            //AddText("\n"+recdPtr.m_userid+"---参数设置成功"+"\r\n");
                            //config.timer3.Enabled = false;+++++++
                            //config.button2.Enabled = true;+++++++
                            MessageBox.Show(Resources.FMain_Timer3Tick_参数设置成功, Resources.FMain_Timer3Tick_信息, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        case 0x0b:
                            //收到查询参数数据包,取消参数查询超时定时器,并读取各项参数到DEMO
                            //AddText("\n"+recdPtr.m_userid+"---参数查询成功"+"\r\n");
                            //config.readconf();//读取各项参数+++++++
                            //config.timer2.Enabled = false;+++++++
                            //config.button1.Enabled = true;+++++++
                            MessageBox.Show(Resources.FMain_Timer3Tick_参数查询成功, Resources.FMain_Timer3Tick_信息, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        case 0x06:
                            //AddText("\n"+recdPtr.m_userid+"---断开PPP连接成功"+"\r\n");
                            break;
                        case 0x07:
                            //AddText("\n"+recdPtr.m_userid+"---停止向DSC发送数据"+"\r\n");
                            break;
                        case 0x08:
                            //AddText("\n"+recdPtr.m_userid+"---允许向DSC发送数据"+"\r\n");
                            break;
                        case 0x0A:
                            //AddText("\n"+recdPtr.m_userid+"---丢弃用户数据"+"\r\n");
                            break;
                    }
                }
            }
            else
            {
                //缺省消息处理
                base.WndProc(ref m);
            }
        }
Exemplo n.º 6
0
 public static extern int do_read_proc(
     ref GprsDataRecord recdPtr,
     [MarshalAs(UnmanagedType.LPStr)]
     StringBuilder mess,
     bool reply);
Exemplo n.º 7
0
        public void UpdateDtuStateObject(GprsDataRecord recvMessage)
        {
            //var stopWatch = new Stopwatch();
            //stopWatch.Start();

            if (!recvMessage.Equals(null))
            {
                //解析消息,构建DtuStateObject
                //
                    //string HexString = StrToHex(RecvMessage.m_data_buf, RecvMessage.m_data_len);
                    string hexString = Encoding.Default.GetString(recvMessage.m_data_buf);
                    foreach (Field1No s in Enum.GetValues(typeof(Field1No)))//枚举所有字段-有冗余!!!
                    {
                         if (!s.Equals(Field1No.MAX))
                         {
                            string strpa = Global.Patternstrfloat[(ushort)s];
                            var rx = new Regex(strpa, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            var matchetemp = rx.Match(hexString);

                            _cacheLock.EnterWriteLock();

                            try
                            {
                                var x= float.Parse(matchetemp.Groups[s.ToString()].Value);
                                var y = (ushort)s;
                                Field1[y] = x;
                            }
                            catch (Exception)
                            {
                                //
                            }
                            finally
                            {
                                _cacheLock.ExitWriteLock();
                            }
                         }
                    }

                    foreach (Field2No s in Enum.GetValues(typeof(Field2No)))//枚举所有字段-有冗余!!!
                    {
                        if (!s.Equals(Field2No.MAX))
                        {
                            string strpa = Global.Patternstrint[(ushort)s];
                            var rx = new Regex(strpa, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            var matchetemp = rx.Match(hexString);

                            _cacheLock.EnterWriteLock();
                            try
                            {
                                var x = ushort.Parse(matchetemp.Groups[s.ToString()].Value);
                                var y = (ushort)s;
                                Field2[y] = x;
                            }
                            catch (Exception)
                            {
                                //
                            }
                            finally
                            {
                                _cacheLock.ExitWriteLock();
                            }
                        }
                    }

                    foreach (FieldTimeNo s in Enum.GetValues(typeof(FieldTimeNo)))//枚举所有字段-有冗余!!!
                    {
                        if (!s.Equals(FieldTimeNo.MAX))
                        {
                            var strpa = Global.Patternstrdatatime[(ushort)s];
                            var rx = new Regex(strpa, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            var matchetemp = rx.Match(hexString);

                            _cacheLock.EnterWriteLock();
                            try
                            {
                                var x = DateTime.Parse(matchetemp.Groups[s.ToString()].Value);
                                var y = (ushort)s;
                                FieldTime[y] = x;
                            }
                            catch (Exception)
                            {
                                //
                            }
                            finally
                            {
                                _cacheLock.ExitWriteLock();
                            }
                        }
                    }
            }
            //赋值消息接收时间
            _cacheLock.EnterWriteLock();
            try
            {
                var x = DateTime.ParseExact(recvMessage.m_recv_date, "yyyy/MM/dd/HH/mm/ss", new CultureInfo("en-US"));
                FieldTime[(ushort)FieldTimeNo.RecvTime] = x;
                RecvDate = x;
                Global.ParameterList[recvMessage.m_userid].DeltaTime = (x - Global.ParameterList[recvMessage.m_userid].LastUpdatTime).Seconds;
                Global.ParameterList[recvMessage.m_userid].LastUpdatTime = x;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString());
            }
            finally
            {
                _cacheLock.ExitWriteLock();
            }

            //stopWatch.Stop();
        }