www.pudn.com > Modbus.rar > DataSender.cs, change:2013-04-09,size:6454b


using System; 
using System.Collections.Generic; 
//using System.Linq; 
using System.Text; 
using System.Collections; 
using System.Threading; 
using System.Net; 
using System.Net.Sockets; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 
 
namespace Modbus数据采集工具 
{ 
    class DataSender 
    { 
          ArrayList m_tagList; 
          Queue m_sendDataQueue;//待发送数据 
          string m_dstIP; 
          ushort m_dstPort; 
          string m_srcIP; 
          private AutoResetEvent m_sendEvent; 
 
          public delegate void logDelegate(string msg); 
          public event logDelegate logEvent; 
          public UdpClient sender;  
          public DataSender(ref AutoResetEvent sendEvent, ref Queue sendDataQueeue, ref ArrayList tagList, string dstIP, ushort dstPort, string srcIP) 
          { 
              m_sendEvent = sendEvent; 
              m_sendDataQueue = sendDataQueeue; 
              m_tagList = tagList; 
              m_dstIP = dstIP; 
              m_dstPort = dstPort; 
              m_srcIP = srcIP; 
              sender= new UdpClient(); 
 
              Thread tdataSend = null; 
              tdataSend = new Thread(new ThreadStart(this.SendData)); 
              tdataSend.IsBackground = true; ; 
              tdataSend.Start(); 
          }  
        private void SendData() 
        { 
            ModbusReader._log.DebugFormat("SendData start"); 
 
            try 
            { 
                //UdpClient listener = new UdpClient(); 
                IPAddress ipAddress = IPAddress.Parse(m_dstIP); 
                byte[] dstbuffer = new byte[2048]; 
 
                while (true) 
                { 
                    m_sendEvent.WaitOne(); 
                    //发送数据 
                    while (m_sendDataQueue.Count != 0) 
                    { 
                        string msg = (string)m_sendDataQueue.Dequeue(); 
                        Console.WriteLine(msg); 
                        if (msg == null || msg.Length <= 0) 
                        { 
                            continue; 
                        } 
                        logEvent(msg); 
 
                        int nlen = 0; 
                        //压缩数据 
                        CompressData(ref msg, ref dstbuffer, ref nlen); 
 
                        #region test 
                        //byte[] bdstBuffer = new byte[2048]; 
                        //Buffer.BlockCopy(dstbuffer, 16, bdstBuffer, 0, nlen - 16); 
                        //int n = 2048; 
                        //ZLib.uncompress(bdstBuffer, ref n, bdstBuffer, nlen - 16); 
                        ////string sss = BitConverter.ToString(bdstBuffer); 
                        //string sss = System.Text.Encoding.Default.GetString(bdstBuffer); 
                        #endregion 
                        Send(ref dstbuffer, ipAddress, nlen); 
                    } 
                } 
            } 
            catch (System.Exception ex) 
            { 
                //m_dataQueue.Enqueue(ex.Message); 
                //Console.WriteLine("{0}\n", ex.Message); 
                ModbusReader._log.DebugFormat(ex.Message); 
            } 
 
             
        } 
 
        private bool CompressData(ref string msg, ref byte[] dstBuffer, ref int ndstLen) 
        { 
            byte[] tempbuffer=new byte[2048]; 
            byte[] msgBuffer = System.Text.Encoding.Default.GetBytes(msg); 
            E_REALTIME e_realtime=new E_REALTIME(); 
 
            // 
            IPAddress dstIpAdd = IPAddress.Parse(m_dstIP); 
            byte[] dstIpBuffer = dstIpAdd.GetAddressBytes(); 
            Array.Reverse(dstIpBuffer); 
            e_realtime.dstIP = BitConverter.ToUInt32(dstIpBuffer, 0); 
             
            IPAddress srcIpAdd = IPAddress.Parse(m_srcIP); 
            byte[] srcIpBuffer = srcIpAdd.GetAddressBytes(); 
            Array.Reverse(srcIpBuffer); 
            e_realtime.srcIP = (uint)BitConverter.ToInt32(srcIpBuffer, 0); 
 
            e_realtime.farmID = 0; 
 
            int dstLength = tempbuffer.Length; 
            if (ZLib.compress(tempbuffer, ref dstLength, msgBuffer, msgBuffer.Length) == ZLibError.Z_OK) 
            { 
                e_realtime.nLen = (uint)dstLength; 
 
                byte[] e_realBuff = StructToBytes(e_realtime); 
                Buffer.BlockCopy(e_realBuff, 0, dstBuffer, 0, 16); 
                Buffer.BlockCopy(tempbuffer, 0, dstBuffer, 16, dstLength); 
                ndstLen = dstLength + 16; 
#region test 
                //byte[] bdstBuffer = new byte[2048]; 
                //int n=2048; 
                //ZLib.uncompress(bdstBuffer, ref n, tempbuffer ,dstLength); 
                ////string sss = BitConverter.ToString(bdstBuffer); 
                //string sss = System.Text.Encoding.Default.GetString(bdstBuffer); 
#endregion 
                return true; 
            } 
          return false; 
        } 
        //// <summary> 
        /// 结构体转byte数组 
        /// </summary> 
        /// <param name="structObj">要转换的结构体</param> 
        /// <returns>转换后的byte数组</returns> 
        public byte[] StructToBytes(object structObj) 
        { 
            //得到结构体的大小 
            int size = Marshal.SizeOf(structObj); 
            //创建byte数组 
            byte[] bytes = new byte[size]; 
            //分配结构体大小的内存空间 
            IntPtr structPtr = Marshal.AllocHGlobal(size); 
            //将结构体拷到分配好的内存空间 
            Marshal.StructureToPtr(structObj, structPtr, false); 
            //从内存空间拷到byte数组 
            Marshal.Copy(structPtr, bytes, 0, size); 
            //释放内存空间 
            Marshal.FreeHGlobal(structPtr); 
            //返回byte数组 
            return bytes; 
        } 
        private void Send(ref byte[] buffer,IPAddress ipAddress, int nLen) 
        { 
             
            IPEndPoint groupEP = new IPEndPoint(ipAddress, m_dstPort); 
            try 
            { 
                //Console.WriteLine("Sending   datagram   :   {0}", message); 
                if (nLen>0) 
                { 
                    sender.Send(buffer, nLen, groupEP); 
                } 
                 
            } 
            catch (Exception e) 
            { 
                Console.WriteLine(e.ToString()); 
            } 
        }    
    } 
}