当前位置:编程学习 > asp >>

.Net MF网络开发板研究06—以太网转串口

  以太网转串口是工控领域最常见的智能通信模块,有的是一网口转1串口,有的是一网口转4串口,最多的可以达到一转16串口(好像有的最多可以支持32串口)。如果该类模块做的足够完善,可以提供一个windows系统的设备驱动,安装后,在windows系统上就可以看到虚拟出的串口了。不过这样做,虽然简便了开发,但是性能有些问题,所以有的模块还支持直接用TCP或UDP进行连接,不同端口号对应不同的串口,这样编程可以达到一个比较高的数据吞吐量(我在上个公司用Moxa 5630模块开发隧道广告的数据通信时,就遇到类似问题,也就是最大限度地提高单位时间的数据吞吐量)。
       现在我们就用.NET MF网络开发板做一个一网口转1串口示例演示。
简单期间,网络部分的代码,我们可以借用《Socket编程之服务端》中介绍的代码,串口部分的代码,我们可以参考《远程PLC读写控制》和《PC通过Modbus协议远程操控开发板》中的串口代码来进行改写。
核心代码如下:
       public ProcessClientRequest(Socket clientSocket, Boolean asynchronously)
        {
            sp.Open();
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
            //--
            m_clientSocket = clientSocket;
            if (asynchronously) // Spawn a new thread to handle the request.
                new Thread(ProcessRequest).Start();
            else ProcessRequest();
        }
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {        
            if (sp.BytesToRead > 0)
            {
                byte[] bytData = new byte[sp.BytesToRead];
                sp.Read(bytData, 0, bytData.Length);
                string s = new string(System.Text.UTF8Encoding.UTF8.GetChars(bytData));
                System.TinyGUI.Graphics.Print("<<< " + s + "\r\n"); //显示串口接收的数据
                m_clientSocket.Send(bytData);     //把串口接收的数据通过网络发送出去           
            }
        }
        private void ProcessRequest()
        {
            const Int32 c_microsecondsPerSecond = 1000000;
            // 'using' ensures that the client's socket gets closed.
            using (m_clientSocket)
            {
                while (true)
                {
                    // Wait for the client request to start to arrive.
                    Byte[] buffer = new Byte[1024];
                    if (m_clientSocket.Poll(5 * c_microsecondsPerSecond, SelectMode.SelectRead))
                    {
                        // If 0 bytes in buffer, then the connection has been closed,
                        // reset, or terminated.
                        if (m_clientSocket.Available == 0) return;
                        // Read the first chunk of the request (we don't actually do
                        // anything with it).
                        Int32 bytesRead = m_clientSocket.Receive(buffer, m_clientSocket.Available, SocketFlags.None);
                        byte[] bytData = new byte[bytesRead];
                        Array.Copy(buffer, bytData, bytData.Length);
                      
                        string s = new string(System.Text.UTF8Encoding.UTF8.GetChars(bytData));
                        System.TinyGUI.Graphics.Print(">>> "+s+"\r\n"); //显示网络接收的数据                   
                        //---------------------------
                        if (sp.IsOpen)
                        {
                            sp.Write(bytData, 0, bytData.Length); //通过串口发送出去
                            sp.Flush();
                   &

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,