With the rapid development of hardware in the internet, the number of internet users is growing exponentially. While using computers for network interconnection, various home appliances, instruments, and data acquisition and control equipment in industrial production are also gradually becoming networked to share the vast information resources on the network. This design is based on a fully electronic railway station control system, aiming to achieve information exchange between the railway station actuators and remote monitoring units. The actuators include various execution units and use a CAN bus interface. The monitoring unit is a remote PC controller using an Ethernet interface. This design aims to achieve a transparent connection between the CAN communication link and the Ethernet communication link, ultimately forming a universal CAN bus Ethernet gateway. CAN bus and Ethernet communication typically employ two schemes: one is using an industrial control computer with a network card. This connection method is costly and has a long development cycle. The other is a 32-bit MCU + RTOS scheme. This scheme uses a high-end 32-bit microcontroller, develops software on an RTOS (Real-Time Operating System) platform, and implements TCP/IP protocol processing in the embedded system. Its disadvantages are: the microcontroller is expensive, the development cycle is long, expensive RTOS development software needs to be purchased, and the development skills required of the developers are high. In view of the advantages and disadvantages of the above solutions, the author designed a standalone Ethernet gateway interconnection system, successfully realizing direct interconnection between Ethernet and CAN bus. I. Hardware Design The system circuit principle is shown in Figure 1. This system is divided into two parts, with the microcontroller as the boundary: the CAN bus control part and the Ethernet control part. The system provides an RJ45 interface for connecting to the Ethernet network and provides a CAN bus interface for user use. The system can unpack IP datagrams from the Ethernet and send them to the CAN bus interface, and can also encapsulate data from the CAN bus interface into IP packets and send them to the local area network. [align=center][img=466,560]http://www.e-works.net.cn/images/127875835660625000.GIF[/img] Figure 1 System Circuit Block Diagram[/align] (I) Microcontroller and its External Memory The W78E58B microcontroller is the main control unit, realizing the control of the network chip and CAN controller. The TCP/IP protocol and CAN protocol are embedded in the microprocessor to complete the protocol conversion between the two and realize the transparent transmission of data between the CAN interface and the Ethernet interface. The 62256 is a 32K external RAM used to buffer Ethernet data packets and data transmitted from the CAN bus. Its purpose is to improve the data transmission speed of the microcontroller and process complex TCP/IP protocols. Since the largest Ethernet data packet is 1514 bytes, while the W78E58B microcontroller only has 256 bytes of RAM, it cannot store such a large data packet. It can only be placed in external RAM so that the microcontroller can process data at high speed. When P2.7 is low, the 62256 is selected, therefore its addressing range is 0000H~7FFFH. The W78E58B microcontroller has 32K of internal EEPROM, which can be used to store some configuration information, such as gateway IP address, MAC address, SJA1000 ID network identifier, network mask, and bus timing (BTR0, BTR1), etc. This allows for flexible and convenient modification of gateway parameters to adapt to different environments, and is also beneficial for future expansion. (II) Ethernet Control Section The RTL8019AS is a highly integrated full-duplex 10M/S Ethernet control chip manufactured by Realtek Corporation of Taiwan. It can realize all the functions of the MAC layer based on the Ethernet protocol, and has built-in 16KB SRAM, dual DMA channels and FIFO, which can complete the functions of receiving and sending data packets. In this design, the RTL8019AS uses jumper mode (JP set high), and the data lines use eight-bit mode (IOCS16B is pulled down to ground with a 27kΩ resistor). The lower five address lines SA0-SA4 of the RTL8019AS are connected to P2.0-P2.4 of the microcontroller, SA6 and SA9 are connected to VCC, and all other address lines are grounded. The chip select signal AEN of the RTL8019AS is enabled when P2.6 and P2.7 are both high. The I/O address of the RTL8019AS needs to be mapped to the I/O address of the microcontroller so that the microcontroller can operate the RTL8019AS. According to the circuit design, the mapping relationship between the microcontroller's I/O address and the RTL8019AS I/O address in this system is: 0XC000H~240H, 0XC100H~241H…XDF00H~25FH, a total of 32 bytes. In addition to connecting to the microcontroller, the RTL8019AS also connects four pins of its network transceiver—HD, LD, TRIN+, and TPIN-—to the Ethernet via an external isolation filter LPF to improve the anti-interference capability of network communication. (III) CAN Bus Control Section The SJA1000 is a CAN bus controller with a multi-master structure, bus access priority, broadcast message function, and hardware filtering function. With a simple bus connection, the SJA1000 can complete all functions of the physical layer and data link layer. It has two working modes: BasicCAN and PeliCAN. BasicCAN only supports the standard mode (this article only introduces the BasicCAN mode), supports error analysis function, controls the CAN transceiver, and provides a simple interface for the microcontroller to control the CAN bus. The microcontroller reads and writes to the SAJ1000 just like it reads and writes to its own external RAM. P2.7 is inverted and used as its chip select signal line. Therefore, the address corresponding to SJA1000 is 8000H~807FH. The /INT pin of the SJA1000 is connected to the INT1 pin of the microcontroller, enabling the microcontroller to respond to CAN interrupt requests in real time. The TJA1050 is a CAN transceiver, serving as the interface between the CAN protocol controller and the physical bus, providing differential transmit and receive capabilities for the CAN bus. After the microcontroller configures the CAN controller accordingly, the transceiver automatically completes the corresponding CAN bus actions. It has strong anti-interference capabilities and can support up to 110 nodes. A high-speed optocoupler 6N137 is used to achieve electrical isolation between the SJA1000 and TJA1050, protecting the control system circuitry and ensuring electrical response at a maximum speed of 1M/s. II. Software Design The software of the entire system is divided into two parts: a CAN controller protocol conversion module and an Ethernet controller protocol conversion module. Its program structure is shown in Figure 2. [align=center][img=500,141]http://www.e-works.net.cn/images/127875835849375000.GIF[/img] Figure 2 Main Program Structure Diagram[/align] When the Ethernet application layer has data to send to the CAN node, the Ethernet controller protocol conversion module first parses the complete CAN protocol data packet from the transport layer data message, stores it in data buffer A, and then notifies the main scheduling module, which calls the CAN controller protocol module to send the CAN protocol data packet to the CAN bus. Conversely, when the CAN device has data to send to the user layer, the CAN controller protocol conversion module first stores the complete CAN protocol data packet in data buffer B, and then notifies the main scheduling module, which calls the Ethernet controller protocol conversion module to encapsulate the complete CAN protocol data packet as application layer data and then sends it to the Ethernet application. (I) CAN Controller Protocol Conversion Module The CAN communication protocol has four different frame formats. This system uses the standard frame format, which is shown in Table 1. CAN first receives the arbitration field, determines the frame format of the received signal based on the content of the arbitration field, and then the user writes the corresponding data into the data field for transmission, or reads the received data from the data field. [align=center]Table 1 Frame Transmission Format[img=500,43]http://www.e-works.net.cn/images/127875836048437500.gif[/img][/align] The CAN controller protocol conversion module mainly consists of the SJA1000's register read program CANRead(), write program CANWrite(), initialization program CANInit(), transmit program Txdsub(), and receive program Rxdsub(). The CAN 2.0A protocol is selected to build the CAN bus control network. The initialization of the SJA1000 mainly includes setting the control register CR, acceptance code register ACR, acceptance mask register AMR, bus timing register BTR0/1, and output control register OCR. After initialization, the SJA1000 controller is monitored by the main scheduling module. When data arrives on the CAN bus, the receive subroutine Rxdsub() is called via interrupt to store this frame of data in data buffer B, and then the receive buffer is translated. Similarly, when a data message composed according to the CAN2.0A protocol format is in data buffer A and needs to be sent to the CAN bus, the main scheduling module calls the CAN send subroutine Txdsub() to send it. (II) Ethernet Controller Protocol Conversion Module A standard Ethernet physical transmission frame is shown in Table 2 (unit: bytes). [align=center]Table 2 Physical Transmission Frame Structure of Ethernet[img=500,55]http://www.e-works.net.cn/images/127875836190000000.gif[/img][/align] Except for the length of the data segment, which is variable, the length of other parts is fixed. Ethernet specifies that the maximum length of the entire transmission packet cannot exceed 1514 bytes (14 bytes for DA, SA, TYPE), and the minimum length can be less than 60 bytes. In addition to the 14 bytes of DS, SA, and TYPE, 46 bytes of data must be transmitted. If the data segment is less than 46 bytes, padding is required; the number of padding characters is not included in the length field. If the data exceeds 1500 bytes, it must be split into multiple frames for transmission. The Ethernet controller protocol conversion module is mainly responsible for parsing the complete CAN protocol message from the UDP packet and storing it in data buffer A. Simultaneously, it can also encapsulate the complete CAN protocol message in data buffer B into a UDP datagram and then send it over the Ethernet. The main program flowchart is shown in Figure 3. The microcontroller first initializes the network device. The network card IP address and physical address are stored in the microcontroller's EEPROM. After the microcontroller resets, it first reads this data to initialize the network card. [align=center][img=500,259]http://www.e-works.net.cn/images/127875836516718750.gif[/img] Figure 3 Main Program Workflow Diagram[/align] After the network card initialization is complete, when data comes from the RJ45, the microcontroller analyzes the data packet. If it is an ARP (Physical Address Resolution Protocol) data packet, the program jumps to the ARP processing program. If it is an IP (Internet Protocol) data packet and the transport layer uses UDP (User Datagram Protocol), and the port is correct, the data packet is considered correct. After unpacking the data, it is sent to data buffer A, and finally the data part is output through the CAN interface. Conversely, if the microcontroller receives data from the CAN interface, it packages the data according to the UDP protocol format, sends it to data buffer B, and the RTL8019AS outputs the data to the local area network. This system adopts the UDP protocol at the communication transport layer because the CAN protocol data packet is in short frame format (each data frame is at most 8 bytes). If the TCP transmission protocol is used, transmitting 8 bytes of CAN protocol data requires establishing a connection through a three-way handshake, transmitting the data, and then releasing the connection through another handshake. Such transmission efficiency is undoubtedly a waste of limited network resources. UDP, on the other hand, is a connectionless transmission protocol, which can improve network transmission efficiency and reduce the processing load on the gateway. The reliability shortcomings of the UDP transmission protocol can be improved by adding a verification mechanism. Experimental tests show that this method is effective. This paper introduces a low-cost, high-reliability, and fast hardware and software implementation method for interconnecting an embedded CAN bus with Ethernet. This interconnection scheme ensures the connection between the management and monitoring layer and the production measurement and control layer, facilitates information exchange between the upper and lower layers, is suitable for existing network transmission systems, and has broad application prospects. Especially in the fields of data acquisition and data transmission, it can be widely used in the design of embedded network interface functions.