关注官方微信

中文.

Products

Products

Upper Computer TCP Programming Method Guide

Published Time:2010-12-26 Author: Shanghai ZLAN Views: 7245
1. Summary

Using the ZLSN serial port to TCP/IP module of Shanghai ZLAN can conveniently implement the TCP networking in lower computer. As just through serial port to receive and send data can realize the TCP/IP data send-receiving. Then to the host like PC and server, how to realize communication with serial port to TCP/IP module through TCP? Basically there are three ways:

1) Use virtual serial port: at this time, the user use ZLVircom to virtually create a com port in the upper computer, and this com port will establish the network connection with TCP/IP to serial port module at the driving bottom. The upper computer communicates with the TCP/IP module via a serial port (COM). For more details, please refer to .

2) Using Modbus TCP protocol: use the TCP/IP module with Modbus TCP to Modbus RTU such as ZLAN5142, ZLSN2140, ZLSN3140 and so on can realize directly convert Modbus TCP protocol to RTU protocol. In this way, the user can use the common Modbus TCP protocol, such as configuration software, OPC software, etc. These software support Modbus TCP, so that users can use these industrial control software to develop. For more details, please refer to .

3) TCP network software programing: the third method is the main method introduced in this paper, and the user develops TCP software to communicate with TCP/IP to serial port module communication through the development environment of VC, VB, Delphi and C++Builder. Almost all common operating systems support the TCP/IP stack, and the operating system provides a common programming specification called the socket interface to invoke the TCP/IP stack of operating system. In practice, the socket interface on Windows is an API interface, and the carried out of end user function directly calls the TCP/IP function of the DLL library provided by the operating system directly.

In addition, if the user needs to implement some specific management of the ZLSN module. For example, search module in the LAN, read the parameters of the module, modify the parameters of the module, ZLAN also provides the dedicated device management function library DLL.

2. The TCP/IP base

We know that TCP/IP serial port module ZLSN2002 basically have work modes of TCP server, TCP client and UDP, for different work mode, PC software also need to choose a different TCP/IP working mode corresponding to it: when module is TCP server the host should be TCP client, module is TCP client the host should be TCP server, module is UDP the host also should be in UDP mode. Please refer to .

3. Socket function and TCP/UDP

Socket is a common TCP/UDP call interface, with main functions as shown in the table:

We call through the difference of socket function. To implement the upper computer work in different modes:
1) TCP client mode: the upper computer calls the socket function “connect” to initiate the connection to the TCP/IP to serial port module to realize the function of the TCP client.
2) TCP server mode: the upper computer calls the socket function “listen” to realize the TCP server function, which is to wait for the connection of ZLSN2002 module on a specific port.
3) UDP mode: no need to listen and initiate the connection, directly call “send” and “recv” function to send and receive.

4. Socket programming examples

This provides the free socket communication source code program . In this code, very simple code is used to implement the data transceiver when the upper computer is used as the TCP client. Let users have an initial impression of socket programming.

5. CSocket programming example of VC

In Microsoft Visula C++, it is already encapsulated again for the underlying socket interface, which is more advanced CAsyncSocket and CSocket class. Using these two classes makes it easier to program. Here provide free code in .

6. Notice for network programming

The method of sending large amounts of data without lost: some users send mass data to ZLSN module by computer, at this time due to the network sending data too fast, and the output of the serial port speed is limited, after a period of time there will certainly lead to send not successfully. So in general, the user needs to check the return value of send(), and only if the return value is the same as the expected amount of data sent. Otherwise, you must continue to call send() to send unsent data. Through this test, you can ensure that the data sent is not lost.