Template: UART TO ETH for RPi use
Provide WiringPi, RPI (Python) library demos.
Working with RPI
Hardware Connection
Please refer to the pin correspondence table below:
ETH | Raspberry Pi | |
Wiring encoding | Board physical pin number | |
5V | 5V | 5V |
GND | GND | GND |
RXD1 | 15 | 8 |
TXD1 | 16 | 10 |
CFG0 | 4 | 16 |
RST1 | 5 | 18 |
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
Open the /boot/config.txt file and find the following configuration statement to enable the serial port, if not, add it at the end of the file:
enable_uart=1
Reboot to take effect.
Install Library
If you use the bookworm system, you can only use lgpio library, bcm2835 and wiringPi can't be installed and used.
BCM2835
#Open the Raspberry Pi terminal and run the following command 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, you can refer to the official website at: http://www.airspayce.com/mikem/bcm2835/
WiringPi
#Open the Raspberry Pi terminal and run the following command cd sudo apt-get install wiringpi #For Raspberry Pi systems after May 2019 (earlier than that can be executed without), an upgrade may be required: wget https://project-downloads.drogon.net/wiringpi-latest.deb sudo dpkg -i wiringpi-latest.deb gpio -v # Run gpio -v and version 2.52 will appear, if it doesn't it means there was an installation error # Bullseye branch system using the following command: git clone https://github.com/WiringPi/WiringPi cd WiringPi . /build gpio -v # Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error
lgpio
#Open the Raspberry Pi terminal and run the following command wget https://github.com/joan2937/lg/archive/master.zip unzip master.zip cd lg-master sudo make install # You can refer to the official website for more: https://github.com/gpiozero/lg
- Install the 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 Demo
Open the Raspberry Pi terminal and execute:
sudo apt-get install p7zip-full wget https://files.waveshare.com/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 demo 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