www.pudn.com > Modbus.rar > DataWrap.cs, change:2013-04-22,size:3362b


using System; 
using System.Collections.Generic; 
//using System.Linq; 
using System.Text; 
using System.Collections; 
using System.Threading; 
 
namespace Modbus数据采集工具 
{ 
    class DataWrap 
    { 
        private ArrayList m_tagList; 
        private Queue m_dataQueue; 
        private int m_count; 
        private int m_size; 
        private int m_yusu=0; 
        private AutoResetEvent m_sendEvent; 
        private int m_startIndex; 
 
        public DataWrap(ref ArrayList tagList, ref Queue dataQueue, ref AutoResetEvent sendEvent, int startIndex) 
        { 
            m_tagList = tagList; 
            m_dataQueue = dataQueue; 
            m_sendEvent=sendEvent; 
            m_startIndex=startIndex; 
 
            int count = m_tagList.Count / 256; 
            m_size = m_tagList.Count; 
            if (0==m_tagList.Count%256) 
            { 
                m_count = count; 
                m_yusu = 0; 
            }  
            else 
            { 
                m_count = count + 1; 
                m_yusu = m_tagList.Count % 256; 
            } 
            //创建线程 
 
            Thread tdataWrap = null; 
            tdataWrap = new Thread(new ThreadStart(this.dataWrap)); 
            tdataWrap.IsBackground = true; ; 
            tdataWrap.Start(); 
        } 
        private void dataWrap() 
        { 
            while(true) 
            { 
                for (int i = 0; i < m_count; i++ ) 
                { 
                    int nLength = 0; 
                    //生成header 
                    string msg=CreatHeader(i*256, out nLength); 
 
                    CreateWrapData(ref msg, i * 256, nLength); 
                    m_dataQueue.Enqueue(msg); 
                    //set event 
                    m_sendEvent.Set(); 
                } 
                Thread.Sleep(Form1.nTime); 
            } 
        } 
        private string CreatHeader(int nStart, out int nLength) 
        { 
            if (m_size-nStart<256) 
            { 
                nLength = m_size - nStart; 
            } 
            else 
            { 
                nLength = 256; 
            } 
            //2/1/WP/ALL/52480/256 
            string sHeader = string.Empty; 
            sHeader = string.Format("1/1/WP/ALL/{0}/{1}", m_startIndex+nStart, nLength); 
            return sHeader; 
        } 
 
        private bool CreateWrapData(ref string msg, int nStart, int length) 
        { 
            try 
            { 
                for (int i = nStart; i < nStart + length; i++) 
                {    
                    msg += ","; 
                    TAG tag=(TAG)m_tagList[i]; 
                    string sValue = string.Empty;    
                    if (tag.type == typeof(int) || tag.type == typeof(Int16)||tag.type==typeof(char)) 
                    { 
                        sValue=string.Format("{0:d}",tag.value); 
                    } 
                    else if (tag.type == typeof(float) || tag.type == typeof(double)) 
                    { 
                        sValue = string.Format("{0:f}", tag.value); 
                    }  
                    msg += sValue; 
                } 
            } 
            catch (System.Exception ex) 
            { 
                Console.WriteLine(ex.Message); 
            } 
            return true; 
        } 
 
    } 
}