• sales

    +86-0755-88291180

2-CH UART TO ETH User Guide

Introduction

The 2-CH UART TO ETH provides an easy way to communicate between UART TTL and RJ45 Ethernet, allowing bi-directional transparent data transmission. It is easy to use, along with features like fast data rate, low power consumption, high stability, suits applications such as safety & security, IoT, and so on.

Features

  • Embedded Ethernet MAC and PHY layers.
  • Bi-direction transparent data transmission between UART and Ethernet.
  • 10/100M, full-duplex/half-duplex auto-negotiation Ethernet interface, 802.3-compliant.
  • Auto-MDI/MDIX, detect and switch cable type automatically.
  • Supports DHCP auto-obtained IP and DNS domain access.
  • Network parameter configuration like chip operating mode, port, IP, via host computer software or UART command.
  • Four operating modes: TCP CLIENT, TCP SERVER, UDP CLIENT, UDP SERVER.
  • 2-CH UART concurrent operation, standalone transparent transmission for each channel.
  • Full-duplex or half-duplex UART communication supports RS485 RX/TX auto switch.
  • Supports virtual serial port software (provided).
  • KEEPALIVE mechanism support.

Specifications

  • Operating voltage:3.3V/5V
  • Operating current: 140mA
  • Operating mode: TCP/UDP
  • UART baud rate: 300bps !921.6Kbps
  • UART TTL level: 3.3V/5V compatible
  • Storage temperature: -55°C ~ 125°C
  • Operating temperature: -40°C ~ 85°C
  • Dimensions: 53.00 x 22.00mm

Pinout

CH9121
5V/3V3power positive, use any pin of 5V/3.3V pins for power supply
GNDground
RST1external reset input, low active
RESETrestore to factory setting, detected when chip power on, low active
TXD1asynchronous UART 1 data output (enabled by default)
TXD2asynchronous UART 2 data output (disabled by default)
RXD1asynchronous UART 1 data input (enabled by default)
RXD2asynchronous UART 2 data input (disabled by default)
CFG0UART config mode, low level to enter configuration, high level to exit configuration
RUNOperating status indicating pin
DIR1UART 1 RS485 RX/TX switch (connects to external RS485 controller)
DIR2UART 2 RS485 RX/TX switch (connects to external RS485 controller)


CH9121 is the chip for network serial port transparent transmission, which can realize the two-way transparent transmission of serial data and network data. It supports 4 working modes such as TCP CLIENT, SERVER, UDP CLIENT and SERVER, and the serial port baud rate is 300bps~921600bps. Before use, it is necessary to configure the network parameters and serial port parameters of the chip through the host computer software NetModuleConfig.exe or serial commands. After the configuration is completed, CH9121 will save the parameters to the internal storage space. After the chip is reset, CH9121 will work according to the saved configuration values.

Default configuration

The UART2 is disabled, UART1 works in TCP CLIENT mode by default. The default setting of ETH is as below:

  • Device IP:192.168.1.200
  • Subnet Mask: 255.255.255.0
  • Gateway: 192.168.1.1
  • Device Port: 2000
  • Target IP: 192.168.1.100
  • Target port: 1000

The default setting of the UART1 is as below:

  • Baud rate: 9600
  • Timeout: 0
  • data bit: 8
  • Stop bit: 1
  • Parity: None
  • Clearing buffer: Never

TCP CLIENT

1. Select SSCOM to select the protocol type as TCPServre, and then view the target IP and target port number through SSCOM (that is, 1 and 2 in the figure).

2. Run NetModuleConfig.exe to set the module parameters, as follows:
(1). Click "Search Device", and the device list will show the modules in the subnet;
(2). Double-click the module in the device list, modify the parameters on the left, and then click "Configure Device Parameters";
(3). After the configuration is completed, the module will restart, wait a while, click "Search Device", find the module, and view the configuration result.
(4). Modify the module parameters according to actual needs, for example: set the network mode to TCP CLIENT, destination IP, destination port and TCP SERVER.
The IP and port are the same, and then modify the baud rate and other parameters as required. Then click Configure Device Parameters.

3. After the configuration is complete, click on SSCOM to connect to it.

TCP SERVER

1. Run NetModuleConfig.exe to set the module parameters, as follows:
(1). Click "Search Device", the device list will show the modules in the subnet;
(2). Double-click the module in the device list, modify the network mode to TCPServre on the left, then modify the module parameters as required, and finally click "Configure Device Parameters";

2. Connect to the module configured as TCP SERVER through SSCOM, the connection will be shown as follows:

UDP CLIENT/SERVER

1. Run NetModuleConfig.exe to set the module parameters, as follows:
(1). Click "Search Device", the device list will show the modules in the subnet;
(2). Double-click the module in the device list, modify the network mode to UDP CLIENT/Servre on the left, then modify the module parameters as required, and finally click "Configure Device Parameters";

2. Connect the module configured as UDP CLIENT/Servre through SSCOM (the UDP mode of SSCOM will automatically connect, no matter your mode is configured as CLIENT/Servre), the connection will be shown as follows:

Working with Pico

Hardware connection

You can connect according to the following table.

Correspondence of Pico connection pins
ETHPicoFunction
5VVSYSPower input
GNDGNDPower ground
RXD1GP0Serial data input
TXD1GP1Serial data output
RXD2GP4Serial data input
TXD2GP5Serial data output
CFG0GP14Network configuration enable pin
RST1GP17Reset

Direct connection




Program download

Open the Raspberry Pi terminal and execute:

sudo apt-get install p7zip-full
cd ~
sudo wget  https://www.waveshare.com/w/upload/3/37/2-CH_UART_TO_ETH_CODE.7z
7z x Pico_ETH_CH9121_CODE.7z -o./2-CH UART TO ETH_CODE
cd ~/2-CH UART TO ETH_CODE
cd Pico/c/build/

Demo

C

  • The following tutorial is for the operation on the Raspberry Pi, but due to the multi-platform and portable characteristics of cmake, it can be successfully compiled on the PC, but the operation is slightly different and needs to be judged by the user.

To compile, make sure to be in the c directory:

cd ~/2-CH UART TO ETH_CODE/Pico/C/

2-CH UART TO ETH_CODE/Pico/C/Serial Port Parameter Configuration:Used to configure the mode through the serial port.
2-CH UART TO ETH_CODE/Pico/C/RX_TX:Used to send and receive information, and return what is received.
Enter one of the folders to create and enter the build directory, and add the SDK: Where ../../pico-sdk is the directory of your SDK. There is a build in our sample program, just enter it directly.

cd build
export PICO_SDK_PATH=../../pico-sdk
(Note: Be sure to write the path where your own SDK is located)

Execute cmake to automatically generate Makefile

cmake ..

Executing make to generate executable files, the first compilation time is relatively long

make -j9

After the compilation is complete, the uf2 file will be generated. Press and hold the button on the Pico board, connect pico to the USB port of the Raspberry Pi via the Micro USB cable, and then release the button. After connecting, the Raspberry Pi will automatically recognize a removable disk (RPI-RP2). Copy the main.uf2 file in the build folder to the recognized removable disk (RPI-RP2).

cp main.uf2 /media/pi/RPI-RP2/

Python

Use in PC environment

  • Press and hold the BOOTSET button on the Pico board, connect pico to the USB port of the computer through the Micro USB cable, and release the button after the computer recognizes a removable hard disk (RPI-RP2).
  • Copy the rp2-pico-20210418-v1.15.uf2 file in the python directory to the recognized removable disk (RPI-RP2)
  • Open Thonny IDE (note: use the latest version of Thonny, otherwise there is no support package for Pico, the latest version under Windows is v3.3.3)
  • Click Tools->Settings->Interpreter, select Pico and the corresponding port as shown in the figure


This example provides two programs:
Serial Port Parameter Configuration.py:This program is used to configure the mode through the serial port.
RX_TX.py:This is used to send and receive information, and return what is received.

  • File->Open->RX_TX.py, click to run, as shown in the figure below:


Use in the Raspberry Pi environment

  • The process of flashing the firmware is the same as on Windows. You can choose to copy the rp2-pico-20210418-v1.15.uf2 file into pico on your PC or Raspberry Pi.
  • Open Thonny IDE on the Raspberry Pi Mountain (click the Raspberry logo -> Programming -> Thonny Python IDE ), you can check the version information in Help->About Thonny

To ensure that your version has the Pico support package, you can also click Tools -> Options... -> Interpreter to select MicroPython (Raspberry Pi Pico and ttyACM0 port
as the picture shows:

If your current Thonny version does not have pico support package, enter the following command to update Thonny IDE

sudo apt upgrade thonny

Click File->Open...->python/RX_TX.py and run the script

Code analysis

C

Configure the parameters through the serial port (modify according to your needs):

  • type of data
#define UCHAR unsigned char
#define UBYTE uint8_t
#define UWORD uint16_t
#define UDOUBLE uint32_t
  • Module initialization:
void CH9121_init(void);
  • This program only has a simple configuration. If you need to configure other functions, you can refer to the serial port control command and configure it yourself:
UCHAR CH9121_Mode            //Mode selection
UCHAR CH9121_LOCAL_IP[4]     //Local IP
UCHAR CH9121_GATEWAY[4]      //Gateway
UCHAR CH9121_SUBNET_MASK[4]  //Subnet mask
UCHAR CH9121_TARGET_IP[4]    //Target IP
UWORD CH9121_PORT1           //Local port
UWORD CH9121_TARGET_PORT     //Target port
UDOUBLE CH9121_BAUD_RATE     //Serial port baud rate
  • According to the serial port control command, the following functions can be used to configure the parameters:
void CH9121_TX_4_bytes(UCHAR data, int command);  //Used for mode, whether the port is random, whether the port is disconnected from the network, whether to clear the serial port data, whether to open DHCP, //whether to open the serial port 2

void CH9121_TX_5_bytes(UWORD data, int command);  //Used to set the port number of the serial port
void CH9121_TX_7_bytes(UCHAR data[], int command);//Used to set IP, subnet mask, gateway
void CH9121_TX_BAUD(UDOUBLE data, int command);   //Used to set the baud rate of the serial port
void CH9121_Eed();  //Update configuration parameters to EEPROM, execute configuration, reset 9121, leave configuration mode

Python

Users only need to modify the values shown below in Serial Port Parameter Configuration.py to configure the serial port parameters of the module:

MODE = 1  #0:TCP Server 1:TCP Client 2:UDP Server 3:UDP Client
GATEWAY = (169, 254, 88, 1)    # GATEWAY
TARGET_IP = (169, 254, 88, 17) # TARGET_IP
LOCAL_IP = (169,254,88,70)     # LOCAL_IP
SUBNET_MASK = (255,255,255,0)  # SUBNET_MASK
LOCAL_PORT1 = 5000             # LOCAL_PORT1
LOCAL_PORT2 = 4000             # LOCAL_PORT2
TARGET_PORT = 3000             # TARGET_PORT
BAUD_RATE = 115200             # BAUD_RATE

Provide WiringPi, RPI (Python) library demos.

Working with RPI

Hardware Connection

Please refer to the pin correspondence table below

Raspberry Pi connection pin correspondence
ETHRaspberry Pi
Wiring encodingBoard physical pin number
5V5V5V
GNDGNDGND
RXD1158
TXD11610
CFG0416
RST1518

Direct connection



Enable UART

Execute the following command to enter the Raspberry Pi configuration:

sudo raspi-config

Choose Interfacing Options -> Serial -> No -> Yes:
You need to disable the login shell and enable the srial port hardware:


Reboot Raspberry Pi:

sudo reboot

Install library

  • Install BCM2835 libraries
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz
tar zxvf bcm2835-1.71.tar.gz 
cd bcm2835-1.71/
sudo ./configure
sudo make
sudo make check
sudo make install
#For more details, please refer to http://www.airspayce.com/mikem/bcm2835/
  • Install WiringPi libraries
sudo apt-get install wiringpi

#For Pi 4, you need to update it:
cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
#You will get 2.52 information if you install it correctly
  • Install Python function library
#python2
sudo apt-get update
sudo apt-get install python-pip
sudo apt-get install python-pil
sudo apt-get install python-numpy
sudo pip install RPi.GPIO
sudo pip install spidev
#python3
sudo apt-get update
sudo apt-get install python3-pip
sudo apt-get install python3-pil
sudo apt-get install python3-numpy
sudo pip3 install RPi.GPIO
sudo pip3 install spidev

Download test program

Open the Raspberry Pi terminal and execute:

sudo apt-get install p7zip-full
wget https://www.waveshare.com/w/upload/3/37/2-CH_UART_TO_ETH_CODE.7z
7z x 2-CH UART TO ETH_CODE. 7z -O./2-CH UART TO ETH_CODE
sudo chmod 777 -R 2-CH UART TO ETH_CODE
cd 2-CH UART TO ETH_CODE/RPi

Demo usage

C

cd WiringPi
  • Serial Port Parameter Configuration: Used to configure the mode through the serial port.
  • RX_TX: Used to send and receive information, and return what is received.

If you configure the parameters through the upper computer, you can directly run RX_TX to test whether there is a problem with the transmission and whether the packet is lost. Otherwise, you can use the Serial Port Parameter Configuration to configure the device parameters. (Functions will be introduced later)

#Configure device parameters
cd Serial Port Parameter Configuration
make clean
make
./CH9121_Config
#Run the send and receive program
cd ..
cd RX_TX
make clean
make
./CH9121_RX_TX

Python

cd Python
  • Serial Port Parameter Configuration.py: Used to configure the mode through the serial port.
  • RX_TX.py: Used to send and receive information, and return what is received.
python3 Serial Port Parameter Configuration.py #You can directly run the following statement through the configuration parameters of the host computer, otherwise modify the parameters in the Serial Port Parameter Configuration.py according to your needs, and then run it.
python3 RX_TX.py

Code Analysis

C

Configure the parameters through the serial port (modify according to your needs):

  • This program only has a simple configuration. If you need to configure other functions, you can refer to the serial port control command and configure it yourself:
uint8_t CH9121_Mode            //Mode selection
uint8_t CH9121_LOCAL_IP[4]     //Local IP
uint8_t CH9121_GATEWAY[4]      //Gateway
uint8_t CH9121_SUBNET_MASK[4]  //Subnet mask
uint8_t CH9121_TARGET_IP[4]    //Target IP
uint16_t CH9121_PORT1          //Local port
uint16_t CH9121_TARGET_PORT    //Target port
uint32_t CH9121_BAUD_RATE      //Serial port baud rate
  • According to the serial port control command, the following functions can be used to configure the parameters:
void CH9121_TX_4_bytes(UCHAR data, int command);  //Used for mode, whether the port is random, whether the port is disconnected from the network, whether to clear the serial port data, whether to open DHCP, //whether to open the serial port 2

void CH9121_TX_5_bytes(UWORD data, int command);  //Used to set the port number of the serial port
void CH9121_TX_7_bytes(UCHAR data[], int command);//Used to set IP, subnet mask, gateway
void CH9121_TX_BAUD(UDOUBLE data, int command);   //Used to set the baud rate of the serial port
void CH9121_Eed(); //Update configuration parameters to EEPROM, execute configuration, reset 9121, leave configuration mode

Python

Users only need to modify the values ​​shown below in Serial Port Parameter Configuration.py to configure the serial port parameters of the module:

MODE = 1 #0: TCP Server 1: TCP Client 2: UDP Server 3: UDP Client
GATEWAY = (169, 254, 88, 1)    # GATEWAY
TARGET_IP = (169, 254, 88, 17) # TARGET_IP
LOCAL_IP = (169,254,88,70)     # LOCAL_IP
SUBNET_MASK = (255,255,255,0)  # SUBNET_MASK
LOCAL_PORT1 = 5000             # LOCAL_PORT1
LOCAL_PORT2 = 4000             # LOCAL_PORT2
TARGET_PORT = 3000             # TARGET_PORT
BAUD_RATE = 115200             # BAUD_RATE

This example has been tested on Arduino uno. Just connect to Arduino uno as shown in the table below.

Working with Arduino

Hardware Connection

You can connect according to the following table.

Arduino connection pin correspondence
ETHArduinoFunction
5V5VPower input
GNDGNDPower Ground
RXD1TXSerial data input
TXD1RXSerial data output
CFG0D2Network configuration enable pin
RST1D4Reset

Direct connection



Install and compile software (Windows tutorial)

arduino IDE installation tutorial

Run the program

Download the program in the information we provide, unzip it, and then enter the 2-CH UART TO ETH_CODE/Arduino directory
If you don't want to configure the parameters through the host computer, you can run the project in Serial_Port_Parameter_Configuration and double-click to open the .ino file.
Choose your development board and the corresponding port.

Change the corresponding parameters according to your needs, and then compile and download (you don't need to connect the RXD and TXD of ETH to the Arduino during the download process, otherwise the download will fail), as shown below:

After configuration, you can compile and download the project in RX_TX, double-click to open the .ino file.
After the download is successful, run SSCOM to connect to the ETH module, and you can send data to ETH, and ETH will return everything you sent to it.
(Remember to connect RXD and TXD to Arduino)

Code Analysis

Configure the parameters through the serial port (modify according to your needs):

  • type of data
#define uint8_t unsigned char
#define uint16_t short unsigned int
#define uint32_t long unsigned int
  • This program only has a simple configuration. If you need to configure other functions, you can refer to the serial port control command and configure it yourself:
uint8_t CH9121_Mode            //Mode selection
uint8_t CH9121_LOCAL_IP[4]     //Local IP
uint8_t CH9121_GATEWAY[4]      //Gateway
uint8_t CH9121_SUBNET_MASK[4]  //Subnet mask
uint8_t CH9121_TARGET_IP[4]    //Target IP
uint16_t CH9121_PORT1          //Local port
uint16_t CH9121_TARGET_PORT    //Target port
uint32_t CH9121_BAUD_RATE      //Serial port baud rate
  • According to the serial port control command, the following functions can be used to configure the parameters:
void CH9121_TX_4_bytes(UCHAR data, int command);  //Used for mode, whether the port is random, whether the port is disconnected from the network, whether to clear the serial port data, whether to open //DHCP,whether to open the serial port 2

void CH9121_TX_5_bytes(UWORD data, int command);  //Used to set the port number of the serial port
void CH9121_TX_7_bytes(UCHAR data[], int command);//Used to set IP, subnet mask, gateway
void CH9121_TX_BAUD(UDOUBLE data, int command);   //Used to set the baud rate of the serial port
void CH9121_Eed(); //Update configuration parameters to EEPROM, execute configuration, reset 9121, leave configuration mode

STM32

The sample program we provide is based on STM32F103RBT6, and the connection method provided is also the pin of the corresponding STM32F103RBT6. If there is a need to transplant the program, please connect according to the actual pin

Hardware Connection

STM32F103ZET connection pin correspondence
ETHSTM32
5V5V
GNDGND
RXD1PC10
TXD1PC11
CFG0PC12
RST1PD2

Take our STM32F103RBT6 as an example, the connection is as follows:




Software Description

The routine is developed based on the HAL library. Please download the program in the data, find the STM32 program file directory, and open STM32\Serial_Port_Parameter_Configuration\MDK-ARM
CH9121.uvprojx under the directory, you can see the program.

Open main.c, you can see the program that can be modified by the user, modify the corresponding parameters according to your own needs, and then recompile and download.
(This function is mainly used to configure the serial port parameters. If you use the upper computer configuration, you can directly run the program in the RX_TX file to test the data transmission and reception, whether it will lose packets, etc.)

After the parameters are configured, download the program in the RX_TX folder to STM32.
After the download is successful, run SSCOM to connect to the ETH module, and you can send data to ETH, and ETH will return everything you sent to it.

Code Analysis

  • This program only has a simple configuration. If you need to configure other functions, you can refer to the serial port control command and configure it yourself:
uint8_t CH9121_Mode            //Mode selection
uint8_t CH9121_LOCAL_IP[4]     //Local IP
uint8_t CH9121_GATEWAY[4]      //Gateway
uint8_t CH9121_SUBNET_MASK[4]  //Subnet mask
uint8_t CH9121_TARGET_IP[4]    //Target IP
uint16_t CH9121_PORT1          //Local port
uint16_t CH9121_TARGET_PORT    //Target port
uint32_t CH9121_BAUD_RATE      //Serial port baud rate
  • According to the serial port control command, the following functions can be used to configure the parameters:
void CH9121_TX_4_bytes(UCHAR data, int command);  //Used for mode, whether the port is random, whether the port is disconnected from the network, whether to clear the serial port data, whether to open DHCP, //whether to open the serial port 2
void CH9121_TX_5_bytes(UWORD data, int command);  //Used to set the port number of the serial port
void CH9121_TX_7_bytes(UCHAR data[], int command);//Used to set IP, subnet mask, gateway
void CH9121_TX_BAUD(UDOUBLE data, int command);   //Used to set the baud rate of the serial port
void CH9121_Eed(); //Update configuration parameters to EEPROM, execute configuration, reset 9121, leave configuration mode