Ethernet to serial port is the most common intelligent communication module in the industrial control field. Some are one network port to one serial port, and some are one network port to four serial ports, up to 16 serial ports can be reached, as if some support up to 32 serial ports ). If this type of module is complete enough, you can provide a device driver for the windows system. After installation, you can see the virtual serial port on the windows system. However, although the development is simple, there are some performance problems, so some modules also support direct TCP or UDP connection, different port numbers correspond to different serial ports, this programming can achieve a relatively high data throughput. I encountered a similar problem when I used the Moxa 5630 module in my previous company to develop data communication for tunnel advertisements, that is, to maximize the data throughput per unit time ).
Now we will use the. net mf network development board to provide an example of converting one port to one serial port. In a simple period, we can use the code described in Socket programming server and serial code, we can refer to the serial port code in remote PLC read/write control and PC Remote Control Development Board through Modbus protocol to rewrite. The core code is as follows: 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"); // display the data received by the serial port m_clientSocket.Send (bytData ); // send the data received by the serial port over the network} 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"); // display the data received by the Network // ------------------------- if (sp. isOpen) {sp. write (bytData, 0, bytData. length); // send it out through the serial port sp. flush () ;}// -----------------------------} deploy the code on the Development Board and run the code. We can directly test the code using our TCP Client tool as follows: http://www.sky-walker.com.cn/MFRelease/Tools/YFTCPClient.rar ). Open any serial port debugging tool, set the baud rate to 115200, and prepare to communicate with the TCP Client. The Development Board has two serial ports, among which COM1 is set as the Debug port, so it cannot be used. We use the COM2 port ). The test procedure is shown as follows: 650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1S14RJ5-0.jpg "/> You can see the information sent by the TCP Client tool. The serial port debugging assistant has received the information. Similarly, the TCP Client can receive the data sent by the serial port debugging assistant. The following figure shows the running status of the Development Board: 650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1S14R226-1.jpg "/> In fact, what we compare with" niu "is that one network port can be switched to one of the six serial ports to connect Zigbee, the other is RS485, and there are also several IO) for example, the system is based on STM32F207 and is under debugging. 650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1S14Q346-2.jpg "/> Source code: http://www.sky-walker.com.cn/MFRelease/Sample/YFMFTCP2Serialport.rarMF Introduction: http://blog.csdn.net/yefanqiu/article/details/5711770 Official Website: http://www.sky-walker.com.cn/650 ) This. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1S14U413-3.png "/>
This article is from the "ye fan Studio" blog, please be sure to keep this source http://yfsoft.blog.51cto.com/1635641/657623