Share this

CAN/TCP Embedded Gateway Design

2026-04-06 05:59:16 · · #1
Abstract: To solve the data exchange problem between CAN and Ethernet, two different networks, this paper introduces the design method of an embedded gateway for CAN fieldbus and Ethernet data communication, realizes data exchange, and gives the corresponding hardware and software principles. Keywords: CAN bus; Ethernet; TCP/IP protocol 0 Application Background: In enterprise network integration systems, fieldbus is generally used inside the workshop, while Ethernet is used in the upper production management layer, and the two are connected by an industrial control computer. The fieldbus is connected to the industrial control computer through a PCI card, and then the industrial control computer is connected to the enterprise management layer through a network card. This connection method is costly and has a long development cycle. In view of these situations, this paper proposes a design scheme of an embedded CAN-Ethernet gateway interconnection system based on a microprocessor, which successfully realizes direct data transmission between Ethernet and CAN fieldbus networks. 1 Hardware Design: There are various hardware implementation schemes. For the TCP/IP protocol, the TCP/IP protocol stack can be implemented in software, or an integrated TCP/IP protocol stack chip, such as W3100A, can be used directly. For the CAN protocol, a microprocessor with integrated CAN protocol, such as PIC18f258, can be used, or a separate protocol chip, such as SJA1000, can be used. This paper uses a P89C51RD2 microprocessor, an SJA1000 CAN controller, a P82c250 CAN transceiver, and an RTL8019AS network chip to form a protocol gateway. The CAN network uses the SJA1000 independent controller, a replacement for the PHLIPS PCA82C200 controller. The Ethernet protocol chip is the RTL8019AS manufactured by Realtek in Taiwan. This Ethernet controller chip integrates the Media Access Control (MAC) sublayer and physical layer performance, facilitating the design of ISA bus-based systems and simple interfacing with general-purpose microcontrollers. Furthermore, it offers advantages such as NE2000 compatibility, good software portability, and low cost. It provides a simple interface for the microcontroller to control Ethernet, allowing the microcontroller to perform Ethernet operations simply by reading and writing data. A 62256 microcontroller is added to the circuit for storing relayed data, and an x5045 microcontroller is added for chip reset and storing network address, CAN node address, and other information. Only the hardware connection diagram of the RTL8019AS is shown here. Figure 1 RTL8019 Wiring Diagram 2. Software Design: The software design is divided into CAN network software design, TCP/IP software design, and CAN-to-Ethernet communication program design. 2.1 CAN Protocol: CAN protocol programming is relatively simple, requiring only read and write operations on the corresponding registers of the SJA1000. In this mode, the message identification code is 11 bits. After being filtered by the acceptance filter, only messages that meet the conditions can be received and stored in the SJA1000 receive buffer. The smaller the identification code value, the higher the priority. If a message conflict occurs on the bus, the message with higher priority will occupy the bus. The maximum data transmitted between CAN nodes is 10 bytes at a time. The following is the SJA1000 data reception flowchart: Figure 2 CAN Bus Data Reception Flowchart 2.2 Ethernet Driver Writing: The operation of the RTL8019 is relatively simple. The driver program only needs to write the data to be sent into the chip in a certain format and start the send command. The RTL8019 will automatically convert the data packets into physical frame format for transmission on the physical channel. Conversely, after receiving physical signals, the 8019 restores them to data and stores them in the chip's RAM in a specified format for the host program to access. In short, the 8019 performs the conversion between data packets and electrical signals. The Ethernet protocol is automatically handled by the chip hardware and is transparent to the programmer. The driver has three functions: chip initialization, packet reception, and packet transmission. The RTL8019AS is responsible for receiving and sending data on the network. To enable it to start and be ready to receive or send data, the corresponding registers must be initialized. These registers include CR, DCR, RBCR, PSTART, PSTOP, ISR, IMR, PAR0-PAR5, MAR0-MAR7, CURR, TCR, and RCR. Void Intnet() { Reg00=0x21; // Put the chip in stop mode, then set the registers Delay_MS(10); // Delay for 10 milliseconds to ensure the chip enters stop mode page(0); Reg0a=0x00; // Clear rbcr0 Reg0b=0x00; // Clear rbcr1 Reg0c=0xe0; // RCR, monitor mode, do not receive data packets Reg0d=0xe2; // TCR, loop back mode Reg01=0x4c; Reg02=0x80; Reg03=0x4c; Reg04=0x40; // TPSR, send start page register Reg07=0xff; // Clear all interrupt flags, interrupt status register Reg0f=0x00; // Clear interrupt mask register to 0, disable interrupts Reg0e=0xc8; // Data configuration register, 8-bit DMA mode page(1); Reg07=0x4d; Reg08=0x00; Reg09=0x00; Reg0a=0x00; Reg0b=0x00; Reg0c=0x00; Reg0d=0x00; Reg0e=0x00; Reg0f=0x00; Reg00=0x22; //This starts the chip working SetMacID(); //Write the chip's physical address to the MAR register page(0); Reg0c=0xcc; //Set the chip to normal mode and connect to the external network Reg0d=0xe0; Reg00=0x22; //Start the chip to work Reg07=0xff; //Clear all interrupt flags} The data sending program first puts the data into RAM according to a certain format, sets the starting page address and sending length of the data, and then fills in the sending command. The chip will automatically convert the data into physical frame format and transmit it on the physical channel. Simultaneously, the sent result is written to the status register. This register can be queried to determine if the data was successfully sent, allowing for subsequent processing. The main program detects new data arrivals through polling or receiving interrupts. Once received, the data is placed in a circular queue defined by the pstart and pstop registers. 2.3 TCP/IP Protocol: TCP/IP is a layered protocol, with each layer implementing a specific function, corresponding to one or more transport protocols. Each layer is implemented as a relatively independent data packet relative to its lower layer. The protocol layer structure is shown in the figure: [align=center] Figure 3 Schematic diagram of Ethernet data demultiplexing[/align] The figure is designed according to the four-layer reference model of the TCP/IP protocol, and actually represents the Ethernet data reception process, while also embodying the idea of ​​Ethernet data demultiplexing. After the host receives an Ethernet data frame, it begins to ascend from the data link layer of the protocol, stripping the headers added by each layer of the protocol, leaving only the user data. This part of the work is completed by the user program. The Rtl8019AS is only responsible for receiving data into its internal data buffer. When programming, the following program framework is used: if (Ethernet datagram arrives) { if (Ethernet header frame type == 0x0806) { ARP handler} if (Ethernet header frame type == 0x0805) { ARP handler} if (Ethernet header frame type == 0x0800) { Distinguish between TCP and UDP packets based on the IP packet protocol type} } When sending data, it is necessary to encapsulate the data layer by layer, that is, add the corresponding header as an identifier for each layer it passes through. The specific principle is to add the current layer's identifier to the user data, then send it to the next layer of the protocol, then add the next layer's protocol identifier, and continue to transmit it downwards. This process is repeated until the actual transmitted data is obtained. Based on the above introduction, the idea of ​​data demultiplexing is reflected in the receiving process, and the idea of ​​data encapsulation is reflected in the Ethernet data transmission process. 2.4 Data exchange between CAN and Ethernet: The data exchange principle is simply to layer the data received from Ethernet. The data is retrieved from the TCP, verified, analyzed, and then transmitted according to CAN mode. Conversely, upon receiving data from the CAN network, information such as address and number is removed, and the remaining data is encapsulated and transmitted according to the TCP/IP protocol. Upon entering an external interrupt triggered by CAN data reception, the data is stored in a buffer, and a custom protocol is used to determine if it is the last frame. If not, the data is saved, and the interrupt is exited; otherwise, other interrupts are disabled, and the Ethernet transmission subroutine is called to send the data to Ethernet. Upon entering an interrupt triggered by Ethernet, valid data from the TCP is retrieved and analyzed. If transmission is required, other interrupts are disabled, the data is packaged, and the CAN transmission subroutine is called. The module's IP address or CAN node number can also be reset via commands. 3. Conclusion: Since the CAN bus can only transmit 8 valid bytes at a time, a custom protocol was used in the CAN network for effective Ethernet communication. Simultaneously, to implement functions such as setting the IP address or changing the node number, a custom protocol was defined at the application layer based on the TCP protocol. This module achieves basic data exchange between CAN and Ethernet. Testing has yielded good results. With the increasing prominence of the advantages of distributed measurement and control systems, the application of CAN/TCP embedded gateways in practice will become more and more widespread. References: [1] UPS monitoring system based on embedded web server, Microcomputer Information, 2005, No. 13 [2] RTL8019AS datasheet, http://www.realtek.com.cn
Read next

CATDOLL Cici 109CM TPE (Soft Silicone Head with ivory Tone)

Height: 109cm Weight: 15.6kg Shoulder Width: 26cm Bust/Waist/Hip: 52/50/57cm Oral Depth: 3-5cm Vaginal Depth: 3-13cm An...

Articles 2026-02-22