Gateway to serial communication based on TCP/IP

Publisher:温馨如家Latest update time:2012-08-10 Source: ofweek Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Introduction

Modern remote measurement and control tasks require remote monitoring, control and remote data transmission of various industrial systems. Traditional centralized measurement and control systems can no longer meet the needs of complex, remote (off-site) and large-scale distributed measurement and control tasks. Moreover, most of the industrial control and communication equipment use serial port devices that comply with the RS232 standard. As Internet technology is popularized in various fields around the world, how to forward serial port data to the network to achieve remote control of equipment and remote data transmission has become an urgent problem to be solved. At the same time, considering the cost issue, it is impossible to completely eliminate the previous equipment. Therefore, this paper proposes a TCP/IP-based serial port conversion gateway solution.

The serial port conversion gateway makes it possible to transmit serial port data streams to Ethernet data streams. It can connect to RS232 serial port devices, select and process serial port data, and convert the data stream of the RS232 interface into Ethernet data streams, so that networked data processing can be performed to achieve networking of serial data. With this solution, there is no need to eliminate the original serial port equipment. Multiple devices can be connected to the network at the same time, which can not only improve the utilization rate of equipment, but also save networking costs and simplify the wiring complexity based on the existing network.

2 Hardware structure The hardware platform is composed

of STC 89C58 single-chip microcomputer and RTL8019-AS Ethernet controller. The driver of the network device is written to realize the Ethernet communication function. The hardware structure of the system is shown in Figure 1. The system module can meet the remote monitoring work requirements of electrical equipment with RS-232 interface. It is a practical network communication module. The module uses the network interface chip RTL8019AS to enable the measurement and control equipment to access the local area network in a low-cost and simple way . The main performance of the network interface chip RTL8019AS meets the Ethernet II and IEEE 802.3 standards, full-duplex, and the transmission and reception can reach a rate of 10MB/s at the same time. The remote PC sends data to the Ethernet interface, the Ethernet interface stores the data in RAM, the on-site serial port device communicates with the microcontroller, and takes out data from RAM as the control command of the on-site equipment, thereby changing the working status of the on-site equipment.

Figure 1 System structure diagram

There are two RAM areas inside the rtl8019as, one is 32 bytes, the address is 0x0000-0x0001f, which is used to store the Ethernet physical address. The address of a 16k RAM is 0x4000-ox7fff. To receive and send data packets, this RAM must be read and written through DMA. The rtl8019as can be divided into remote DMA interface, local DMA interface, MAC logic, data encoding and decoding logic and other ports. The rtl8019as has 32-bit input and output addresses. The remote DMA address offset 0x10h-0x17h can be used as a remote DMA port. Just use one of them, and 0x10h is generally used. Remote DMA is the CPU reading data from the RAM of the network card to the system RAM, or sending data to the RAM of the network card. Local DMA is the network card receiving data uploaded from the Ethernet, or sending data in the network card RAM to the Ethernet (the network card automatically completes after the startup command).

The local DMA completes the data exchange between the control line and the network cable, and the processor CPU only needs to operate the remote DMA to send and receive data. When the processor wants to send data to the network, it first sends a frame of data to the send buffer in the rtl8019as through the remote DMA channel, and then issues a transmission command. After the rtl-8019as completes the transmission of the previous frame, it starts to send this frame. The data received by the rtl8019as is compared with the mac address and the crc check, and then stored in the receive buffer by the fifo. After receiving a full frame, the main processor is notified by interrupt or query register flag.

3 Software Design

3.1 Rtl8019as Initialization

When using rtl8019as as a network management chip, 8019 must be initialized

{page_select(0);
cr=0x4c;
pstart=0x80;
bnry=0x4c;
tpsr=0x40; //tpsr, send start page register
isr=0xff; //clear all interrupt flags
rcr=0xe0; //rcr, monitor mode, do not receive data packets
tcr=0xe2; //tcr, look back mode
dcr=0xc8; //data configuration register
imr=0x00; //interrupt flag mask register clear 0
page_select(1);
curr=0x4d;
cr=0x22; //start rtl8019as to start working
page_select(0);
cr=0x22; //start rtl8019as to start working
isr=0xff; //clear all interrupt flags
rcr=0xcc; //write the physical address of rtl8019as to the mar register
tcr=0xe0;
}
[page]

3.2 Data Encapsulation

To transmit data in the Ethernet, the serial port data must be encapsulated according to the Ethernet frame format, which is a layer-by-layer encapsulation process. The application adds the appl header to the device data to form the application data, and the TCP protocol adds the TCP header to the application data, encapsulating the data layer by layer. Finally, the rtl8019as chip adds the Ethernet header to the data. The encapsulation process is shown in Figure 2.

Figure 2 Schematic diagram of data encapsulation

The format of the encapsulated data transmission is as follows:

(1) Preamble: Generated by the RTL8019AS chip itself, used to synchronize the clocks of the two parties and specify the transmission rate.

(2) Destination address DA: The destination address of the Ethernet data frame transmission, which is a 48-bit binary address. When all 1s are set, it indicates a broadcast address.

(3) Source address SA: The 48-bit source address of the Ethernet data frame transmission, indicating the starting point of the frame data, that is, the address of the sender.

(4) Frame format type: The type field indicates the type of the frame data. For example, the data type of the IP packet is 0800h, and the data type of the ARP packet is 0806h.

(5) Data: Ethernet stipulates that the maximum length of the entire data packet is 1514 bytes, and here it is stipulated that the data field cannot exceed 1500 bytes.

(6) CRC and padding: Ethernet stipulates that the entire data packet must be greater than 60 bytes. If it does not meet 60 bytes, any data will not make up 60 bytes.

3.3 Data processing framework

The data information from the client reaches the microcontroller through the RJ-45 Ethernet interface and the network interface chip. The microcontroller implements the address resolution protocol (ARP), Internet control message protocol (ICMP ) , IP protocol and user datagram protocol (UDP) through the embedded TCP/IP protocol stack, thereby completing the parsing and unpacking of network data. The following program framework is used when programming network data processing:

{ if(Ethernet header frame type==0x0806)
{arp handler}
if(Ethernet header frame type==0x0835)
{parp handler}
if(Ethernet header frame type==0x0800)
{ip handler}
}


In normal operation, the task of the TCP/IP protocol conversion module is mainly to encapsulate the serial frame of the Ethernet send buffer in the UDP packet and pass it to the IP layer; at the same time, it receives the Ethernet data frame and unpacks it to the upper layer, separates the application layer data, and then the data parsing and processing are handed over to the multi-serial port sending module. After receiving the serial data, the microcontroller encapsulates it and writes it into the Ethernet send buffer for packaging and transmission; at the same time, it receives the data of the Ethernet application layer, parses it and sends it from the serial port. For this design, the specific form of the communication data is not important. Its main task is to receive/send, encapsulate/unpack serial frames, and provide a general gateway interface for serial devices.

4 Conclusion

This article introduces the gateway-to-serial communication based on TCP/IP, and uses the RTL8019AS chip to realize the online communication of serial devices. It is well adapted to low-speed, serial port industrial occasions. It builds a bridge between serial port electrical equipment and the network, realizes serial port equipment and monitoring and transparent data transmission, as well as network control of equipment and distributed management of information. It can be widely used in distributed measurement and control networks based on Ethernet. Through it, we can realize remote monitoring of various real-time signals, instruments , civil facilities and other targets, and unified management of various equipment. This will greatly improve work efficiency, improve the working environment, and enhance people's production and living standards.

Reference address:Gateway to serial communication based on TCP/IP

Previous article:MCU music player C language program
Next article:Realizing fireworks function with dot matrix screen based on MCS-51 single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 20:23

Application of TCP/IP protocol stack with open source code in remote monitoring
Introduction: At present, with the development of the Internet, more and more industrial measurement and control equipment have adopted network access function as their default configuration to realize remote monitoring and information distributed processing of equipment. The author has participated in the dev
[Microcontroller]
Application of TCP/IP protocol stack with open source code in remote monitoring
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号