xml地图|网站地图|网站标签 [设为首页] [加入收藏]
实现多种数据类型解析,最新海南南方七星彩投
分类:编程

这是一款最新海南南方七星彩投注网站系统源码,以及七星彩网站建设开发,也可以出租,带源码
网站是采用php+MySQL的。

1、IProducerConsumerCollection (线程安全接口)
  此接口的所有实现必须都启用此接口的所有成员,若要从多个线程同时使用。

更新内容,v4.2.2版本:
style="font-size: 16px;">1.增加Modbus协议读取多个寄存器,并且按多种数据类型解析数据。
2.Modbus Serial和Modbus TCP两个驱动合并成一个驱动。
style="font-size: 16px;">3.修改数据库结构,保存配置信息。
style="font-size: 16px;">4.优化ServerSuperIO核心代码,应用过程中的潜在问题。

v4.2.2 下载地址:官方下载

网站系统有-总代理-代理-会员

图片 1图片 2


如果需要的朋友,可以了解一下,Q  22-4071-3732 ,扣 :二二四零七一三七三二  ,(加Q请备注一下,谢谢)

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace ConsoleApp1
{
    public class SafeStack<T> : IProducerConsumerCollection<T>
    {
        // Used for enforcing thread-safety
        private object m_lockObject = new object();

        // We'll use a regular old Stack for our core operations
        private Stack<T> m_sequentialStack = null;

        //
        // Constructors
        //
        public SafeStack()
        {
            m_sequentialStack = new Stack<T>();
        }

        public SafeStack(IEnumerable<T> collection)
        {
            m_sequentialStack = new Stack<T>(collection);
        }

        //
        // Safe Push/Pop support
        //
        public void Push(T item)
        {
            lock (m_lockObject) m_sequentialStack.Push(item);
        }

        public bool TryPop(out T item)
        {
            bool rval = true;
            lock (m_lockObject)
            {
                if (m_sequentialStack.Count == 0) { item = default(T); rval = false; }
                else item = m_sequentialStack.Pop();
            }
            return rval;
        }

        //
        // IProducerConsumerCollection(T) support
        //
        public bool TryTake(out T item)
        {
            return TryPop(out item);
        }

        public bool TryAdd(T item)
        {
            Push(item);
            return true; // Push doesn't fail
        }

        public T[] ToArray()
        {
            T[] rval = null;
            lock (m_lockObject) rval = m_sequentialStack.ToArray();
            return rval;
        }

        public void CopyTo(T[] array, int index)
        {
            lock (m_lockObject) m_sequentialStack.CopyTo(array, index);
        }



        //
        // Support for IEnumerable(T)
        //
        public IEnumerator<T> GetEnumerator()
        {
            // The performance here will be unfortunate for large stacks,
            // but thread-safety is effectively implemented.
            Stack<T> stackCopy = null;
            lock (m_lockObject) stackCopy = new Stack<T>(m_sequentialStack);
            return stackCopy.GetEnumerator();
        }


        //
        // Support for IEnumerable
        //
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<T>)this).GetEnumerator();
        }

        // 
        // Support for ICollection
        //
        public bool IsSynchronized
        {
            get { return true; }
        }

        public object SyncRoot
        {
            get { return m_lockObject; }
        }

        public int Count
        {
            get { return m_sequentialStack.Count; }
        }

        public void CopyTo(Array array, int index)
        {
            lock (m_lockObject) ((ICollection)m_sequentialStack).CopyTo(array, index);
        }
    }
}

 目       录

网站演示图:

SafeStack

3. Modbus协议,读取多个寄存器,实现多种数据类型解析... 2

图片 3 

图片 4图片 5

3.1           概述... 2

图片 6 

using System;
using System.Collections.Concurrent;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            TestSafeStack();

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }

        // Test our implementation of IProducerConsumerCollection(T)
        // Demonstrates:
        //      IPCC(T).TryAdd()
        //      IPCC(T).TryTake()
        //      IPCC(T).CopyTo()
        static void TestSafeStack()
        {
            SafeStack<int> stack = new SafeStack<int>();
            IProducerConsumerCollection<int> ipcc = (IProducerConsumerCollection<int>)stack;

            // Test Push()/TryAdd()
            stack.Push(10); Console.WriteLine("Pushed 10");
            ipcc.TryAdd(20); Console.WriteLine("IPCC.TryAdded 20");
            stack.Push(15); Console.WriteLine("Pushed 15");

            int[] testArray = new int[3];

            // Try CopyTo() within boundaries
            try
            {
                ipcc.CopyTo(testArray, 0);
                Console.WriteLine("CopyTo() within boundaries worked, as expected");
            }
            catch (Exception e)
            {
                Console.WriteLine("CopyTo() within boundaries unexpectedly threw an exception: {0}", e.Message);
            }

            // Try CopyTo() that overflows
            try
            {
                ipcc.CopyTo(testArray, 1);
                Console.WriteLine("CopyTo() with index overflow worked, and it SHOULD NOT HAVE");
            }
            catch (Exception e)
            {
                Console.WriteLine("CopyTo() with index overflow threw an exception, as expected: {0}", e.Message);
            }

            // Test enumeration
            Console.Write("Enumeration (should be three items): ");
            foreach (int item in stack)
                Console.Write("{0} ", item);
            Console.WriteLine("");

            // Test TryPop()
            int popped = 0;
            if (stack.TryPop(out popped))
            {
                Console.WriteLine("Successfully popped {0}", popped);
            }
            else Console.WriteLine("FAILED to pop!!");

            // Test Count
            Console.WriteLine("stack count is {0}, should be 2", stack.Count);

            // Test TryTake()
            if (ipcc.TryTake(out popped))
            {
                Console.WriteLine("Successfully IPCC-TryTaked {0}", popped);
            }
            else Console.WriteLine("FAILED to IPCC.TryTake!!");
        }
    }
}

3.2           Modbus基础知识... 2

图片 7 

Program

3.3           读取多个寄存器,多种数据类型解析... 4

图片 8

2、ConcurrentStack类:安全堆栈

3.4           实现效果展示... 6

图片 9图片 10

3.5           未来驱动的规划... 7

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Task t = RunProgram();
            t.Wait();
        }

        static async Task RunProgram()
        {
            var taskStack = new ConcurrentStack<CustomTask>();
            var cts = new CancellationTokenSource();

            var taskSource = Task.Run(() => TaskProducer(taskStack));

            Task[] processors = new Task[4];
            for (int i = 1; i <= 4; i++)
            {
                string processorId = i.ToString();
                processors[i - 1] = Task.Run(
                    () => TaskProcessor(taskStack, "Processor " + processorId, cts.Token));
            }

            await taskSource;
            cts.CancelAfter(TimeSpan.FromSeconds(2));

            await Task.WhenAll(processors);
        }

        static async Task TaskProducer(ConcurrentStack<CustomTask> stack)
        {
            for (int i = 1; i <= 20; i++)
            {
                await Task.Delay(50);
                var workItem = new CustomTask { Id = i };
                stack.Push(workItem);
                Console.WriteLine("Task {0} has been posted", workItem.Id);
            }
        }

        static async Task TaskProcessor(
            ConcurrentStack<CustomTask> stack, string name, CancellationToken token)
        {
            await GetRandomDelay();
            do
            {
                CustomTask workItem;
                bool popSuccesful = stack.TryPop(out workItem);
                if (popSuccesful)
                {
                    Console.WriteLine("Task {0} has been processed by {1}", workItem.Id, name);
                }

                await GetRandomDelay();
            }
            while (!token.IsCancellationRequested);
        }

        static Task GetRandomDelay()
        {
            int delay = new Random(DateTime.Now.Millisecond).Next(1, 500);
            return Task.Delay(delay);
        }

        class CustomTask
        {
            public int Id { get; set; }
        }
    }
}

 

Program

2.1    概述

      在发布《标准Modbus和非标准协议的使用、测试以及驱动开发》之后,有些网友反馈在Modbus协议驱动方面是否可以实现读多个寄存器和按多种数据类型解析数据。一是平时工作比较忙,二是有些犯懒。在传感器及仪器仪表行业用到Modbus协议的情况比较多,但是大部分使用都比较简单,可能往往只需要一个命令读取数据,对返回数据进行解析。我认为针对这种情况开发一个简单的驱动非常简单,加之网友的水平参差不齐,也没有针对网友的反馈进行积极的回应。但是从一个产品的角度考虑,确实应该进行完善,也确实有市场方面的需求,有很多公司是借鉴Modbus协议形式进行自定义协议,情况比较复杂。所以,利用出差的这段时间,针对Modbus驱动实现读取多个寄存器和按多种数据类型解析数据,以满足不同应用现场的需求。

3、ConcurrentQueue类:安全队列

2.2    Modbus基础知识

      Modbus采用的通讯机制为呼叫应答方式,或叫主从方式(master/slave)。主机发送请求命令,从机接到命令后进行校验数据的完整性,以及确定是否发给自己的命令,校验成功后,返回指定的数据信息,完成一次完整的通讯。呼叫应答通讯机制如下图:

图片 11

       Modbus协议依靠网络传输层定义了一个简单的数据传输协议,串口通讯(Modbus Serial)的RTU和ASCII模式以及网络通讯(Modbus TCP)模式在协议编码和操作方面有所区别。基本协议格式如下图:

图片 12

      下位机地址和功能码,示意图:

图片 13

 

       白皮书介绍:下载地址。

图片 14图片 15

2.3    读取多个寄存器,多种数据类型解析

       主要针对【寄存器个数】和【解析数据方式】两个选择进行升级,在设备驱动中增加监测点的时候,可以选择这两项内容。如下图:

图片 16

        1个寄存器个数代码2个字节,转换成字节数的公式为:读取数据字节数=寄存器个数*2,根据实际情况设置。

        解析数据方式为24种,主要针对short、ushort、int、uint、float和double数据类型进行解析,由于传感器与电脑的高低位排序有所差异,所以针对同一种数据类型分不同的解析方式。解析数据方式,如下图:

图片 17

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Task t = RunProgram();
            t.Wait();
        }

        static async Task RunProgram()
        {
            var taskQueue = new ConcurrentQueue<CustomTask>();
            var cts = new CancellationTokenSource();

            var taskSource = Task.Run(() => TaskProducer(taskQueue));

            Task[] processors = new Task[4];
            for (int i = 1; i <= 4; i++)
            {
                string processorId = i.ToString();
                processors[i - 1] = Task.Run(
                    () => TaskProcessor(taskQueue, "Processor " + processorId, cts.Token));
            }

            await taskSource;
            cts.CancelAfter(TimeSpan.FromSeconds(2));

            await Task.WhenAll(processors);
        }

        static async Task TaskProducer(ConcurrentQueue<CustomTask> queue)
        {
            for (int i = 1; i <= 20; i++)
            {
                await Task.Delay(50);
                var workItem = new CustomTask { Id = i };
                queue.Enqueue(workItem);
                Console.WriteLine("插入Task {0} has been posted ThreadID={1}", workItem.Id, Thread.CurrentThread.ManagedThreadId);
            }
        }

        static async Task TaskProcessor(
            ConcurrentQueue<CustomTask> queue, string name, CancellationToken token)
        {
            CustomTask workItem;
            bool dequeueSuccesful = false;

            await GetRandomDelay();
            do
            {
                dequeueSuccesful = queue.TryDequeue(out workItem);
                if (dequeueSuccesful)
                {
                    Console.WriteLine("读取Task {0} has been processed by {1} ThreadID={2}",
                                        workItem.Id, name, Thread.CurrentThread.ManagedThreadId);
                }

                await GetRandomDelay();
            }
            while (!token.IsCancellationRequested);
        }

        static Task GetRandomDelay()
        {
            int delay = new Random(DateTime.Now.Millisecond).Next(1, 500);
            return Task.Delay(delay);
        }

        class CustomTask
        {
            public int Id { get; set; }
        }
    }
}

2.4    实现效果展示

       使用Modbus TCP进行演示,连接Modbus Slave,127.0.0.1:502,读取1个数据点,按“short,2字节,顺序:先高后低”解析数据,如下图:

图片 18

Program

2.5    未来驱动的规划

       ServerSuperIO相当于系统建设中数据交互的中枢,将来对下连接各种PLC及传感器,也在积极规划开发基于各种PLC的驱动;对上连接WEB组态以及管理系统。所有都通过配置完成,为中小企业提供一站式的项目解决方案,降低成本、提高效率。

       最近国产自主安全可控的redcore browser浏览器融资2.5个亿,实际上使用是的Google的开源内核,假设有“创新”的成分,估计也只是皮毛。尽管ServerSuperIO算不上高大上,但是也算是积累了10年的工作经验,算是真正的自主安全可控。哈哈……。


 1.[连载]《C#通讯(串口和网络)框架的设计与实现》

 2.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍

 2.应用SuperIO(SIO)和开源跨平台物联网框架ServerSuperIO(SSIO)构建系统的整体方案

 3.C#工业物联网和集成系统解决方案的技术路线(数据源、数据采集、数据上传与接收、ActiveMQ、Mongodb、WebApi、手机App)

 5.github地址:

 6.助力中小企业级连云端,促进工业互联网平台蓬勃发展,全套解决方案。

 物联网&集成技术 QQ群:54256083 

4、ConcurrentDictionary类
  ConcurrentDictionary类写操作比使用锁的通常字典(Dictionary)要慢的多,而读操作则要快些。因此对字典要大量的线程安全的读操作,ConcurrentDictionary类是最好的选择
  ConcurrentDictionary类的实现使用了细粒度锁(fine-grained locking)技术,这在多线程写入方面比使用锁的通常的字典(也被称为粗粒度锁)

图片 19图片 20

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var concurrentDictionary = new ConcurrentDictionary<int, string>();
            var dictionary = new Dictionary<int, string>();

            var sw = new Stopwatch();

            sw.Start();
            for (int i = 0; i < 1000000; i++)
            {
                lock (dictionary)
                {
                    dictionary[i] = Item;
                }
            }
            sw.Stop();
            Console.WriteLine("Writing to dictionary with a lock: {0}", sw.Elapsed);

            sw.Restart();
            for (int i = 0; i < 1000000; i++)
            {
                concurrentDictionary[i] = Item;
            }
            sw.Stop();
            Console.WriteLine("Writing to a concurrent dictionary: {0}", sw.Elapsed);

            sw.Restart();
            for (int i = 0; i < 1000000; i++)
            {
                lock (dictionary)
                {
                    CurrentItem = dictionary[i];
                }
            }
            sw.Stop();
            Console.WriteLine("Reading from dictionary with a lock: {0}", sw.Elapsed);

            sw.Restart();
            for (int i = 0; i < 1000000; i++)
            {
                CurrentItem = concurrentDictionary[i];
            }
            sw.Stop();
            Console.WriteLine("Reading from a concurrent dictionary: {0}", sw.Elapsed);
        }

        const string Item = "Dictionary item";
        public static string CurrentItem;
    }
}

Program

本文由澳门新葡亰手机版发布于编程,转载请注明出处:实现多种数据类型解析,最新海南南方七星彩投

上一篇:没有了 下一篇:微信营销系统
猜你喜欢
热门排行
精彩图文