In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to realize CAN bus communication in MicroPython". In daily operation, I believe that many people have doubts about how to realize CAN bus communication in MicroPython. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to realize CAN bus communication in MicroPython". Next, please follow the editor to study!
Preface of 0x00
CAN is the abbreviation of controller local area network (ControllerAreaNetwork,CAN) and is the international standardized serial communication protocol of ISO. The structure of CAN bus is simple, only two wires are connected to the outside, and error detection and management modules are integrated inside.
This article takes the TPYBoardv102 development board as an example to introduce how to use the CAN controller of STM32 to realize the CAN communication between the two development boards.
0x01 experimental equipment
2 TPYBoardv102 development boards
Some DuPont lines
CAN bus transceiver module VP230 (SN65HVD230) 2
Preparation for 0x02
1. CAN controller of TPYBoardv102.
There are two CAN controllers on the v102, CAN1 and CAN2, with the corresponding pins as follows:
CAN1 (RX)
Y3
CAN1 (TX)
Y4
CAN2 (RX)
Y5
CAN2 (TX)
Y6
Although there are two CAN controllers on V102, in order to complete the CAN communication between the two boards, it also needs to be equipped with the CAN transceiver module. The CAN transceiver is the interface between the CAN controller and the physical bus, and its main function is to convert the logic level of the CAN controller into the differential level of the CAN bus and transmit data on two bus cables with differential voltage.
two。 Go to tpyboard's official website to see the introduction of the CAN class library.
Http://docs.tpyboard.com/zh/latest/library/pyb.CAN/?highlight=pyb.CAN
Take a look at the official example code, which is basically enough.
From pyb import CANcan = CAN (1, CAN.LOOPBACK) can.setfilter (0, CAN.LIST16, 0, (123,124,125,126)) # set a filter to receive messages with id=123, 124,125 and 126can.send # send a message with id 123can.recv (0) # receive message on FIFO 0
0x03 hardware connection
I use CAN1 for both V102. The specific diagram of the connection is as follows:
TPYBoard v102 (CAN1)
CAN transceiver module
3.3V
VCC
GND
GND
Y3 (RX)
RX
Y4 (TX)
TX
Just connect H to H and L to L between the two transceivers.
0x04 source code
The first board is waiting to be received and then sent when the data is received.
Import pybfrom pyb import CANcan = CAN (1, CAN.NORMAL) can.setfilter (0, CAN.LIST16, 0, (123,124,125,126)) # set a filter to receive messages with id=123, 124,125 and 126while True: if can.any (0): data=can.recv (0) # receive message on FIFO 0 print ('DATA:',data) can.send
The second board is sent first, and then waiting to be received.
# main.py-- put your code hereafter included from pyb import CANcan = CAN (2, CAN.NORMAL) can.setfilter (0, CAN.LIST16, 0, (222221231241)) # set a filter to receive messages with id=123, 124,125 and 126can.send ('messageboxes, 123) # send a message with id 123while True: if can.any (0): data=can.recv (0) # receive message on FIFO 0 print (' DATA:',data)
0x05 effect diagram
At this point, the study on "how to achieve CAN bus communication in MicroPython" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.