Difference between revisions of "Template:RS485 CAN HAT B RS"
From Waveshare Wiki
(→RS485) |
(→RS485) |
||
Line 1: | Line 1: | ||
− | ==RS485== | + | ==How to use RS485== |
===Hardware Connection=== | ===Hardware Connection=== | ||
Detailed connection: | Detailed connection: |
Revision as of 11:27, 21 October 2022
How to use RS485
Hardware Connection
Detailed connection:
- Running the C and python demo requires an additional USB TO RS485 bidirectional converter to channel 1 to see the effect.
- In Python, test.py requires a connection between channel 1 and channel 2.
Func | BCM | Description |
---|---|---|
5V | 5V | 5V Power Input |
GND | GND | Ground |
SCLK_1 | D21 | SPI clock input |
MOSI_1 | D20 | SPI data input |
MISO_1 | D19 | SPI data output |
CE_1 | D18 | Data/command selection |
INT_1 | D24 | interrupt output |
Test
- Download and run the test demo
sudo apt-get install p7zip-full wget https://www.waveshare.com/w/upload/9/92/RS485_CAN_HAT_B.zip unzip RS485_CAN_HAT_B.zip sudo chmod 777 -R RS485_CAN_HAT_B.zip cd RS485_CAN_HAT_B/
C Program
cd c make clean make sudo ./main
The demonstration here is: The connection between channel 1 of RS485_0 and the A.B connection of USB TO RS485 is as follows:
Connect the USB TO RS485 to your computer, open the serial port assistant, choose the corresponding serial port, and set the baud rate as 115200.
- Execute the C program, the module returns the data that the computer sends to execute the C program, as shown below:
(Note: You need to add a carriage return and line feed, otherwise the data will not be returned; the program directory of the example is based on the actual.)
Python
cd python cd examples sudo python3 main.py
The hardware connection of this program and the C program is the same as the phenomenon:
You can try the following test demo when you don't have USB TO RS485:
- Connect the A, B of channel 1 to the A, B of channel 2.
- Run test.py and you can get the result below:
sudo python3 test.py
Modbus Application Example
This example requires a Raspberry Pi, an RS485 CAN HAT, and a Mobus RTU Relay.
Hardware Connection
- Install the HAT to the Raspberry Pi, and connect the RS485 interface of the HAT to the Modbus's RS485 interface by A-A and B-B.
- Power the Raspberry Pi and the Modbus module respectively.
Download Demo
wget https://www.waveshare.com/w/upload/b/ba/RS485_Modbus_RTU_Relay_Code.zip
- Enter the path after unzipping:
unzip RS485_Modbus_RTU_Relay_Code.zip cd RS485_Modbus_RTU_Relay_Code/Modbus_RTU_Relay_Code/Python3
- Run the program
sudo python main.py
- After running the program, the relay relays are sequentially turned on from CH1 to CH8 and then turned off from CH1 to CH8 according to the number and loop continuously.
- For more programs, you can refer to Modbus RTU Relay module wiki and pay attention to change the serial port number of the program.