`
mmdev
  • 浏览: 12928740 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

【.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();

}

//---------------------------

}

}

}

把代码部署开发板后执行,我们直接用我们的TCP Client工具进行测试(下载地址如下:http://www.sky-walker.com.cn/MFRelease/Tools/YFTCPClient.rar)。

打开任意一个串口调试工具,设置波特率115200,准备和TCP Client进行通信。

(开发板有两个串口,其中COM1我们把它设为Debug口,所以不能使用了,我们用的是COM2口)。

测试过程图如下:

可以看出TCP Client工具所发出的信息,串口调试助手已经收到,同样,串口调试助手所发的数据,TCP Client也可以收到。

此时开发板运行状态图如下:

其实,我们比较“牛”一点的是一个网口可以转6个串口(其中一个连接Zigbee,一个是RS485,还带若干IO)的物联网网关模块(如下图),该系统基于STM32F207,正在调试过程中。

---------------------------------------------------------------------------------------------------------------------------------------------

本文源码:http://www.sky-walker.com.cn/MFRelease/Sample/YFMFTCP2Serialport.rar

MF简介:http://blog.csdn.net/yefanqiu/article/details/5711770

官方网站:http://www.sky-walker.com.cn/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics