【Hardware Peripheral Use】——can

can basic concept

CAN is the abbreviation of Controller Area Network, that is, Controller Area Network. It is a multi-master serial communication protocol used to connect computers, sensors, actuators, and other devices.

Commonly used in the fields of automobiles, industrial automation, and the Internet of Things, CAN has the characteristics of high reliability, high efficiency, and low cost. Since the CAN bus can support communication between multiple devices, it is widely used in many occasions.

insert image description here

can communication

The use of CAN communication can be roughly divided into the following steps:

  1. Confirm CAN hardware: First, you need to confirm whether the hardware device to be used supports CAN communication.

  2. Install the driver: Install the CAN driver that matches the hardware device on the computer.

  3. Configure CAN parameters: configure CAN parameters as required, such as baud rate, filter and timer, etc.

  4. Write a program: According to the difference of operating system and programming language, write a program to realize the CAN communication function.

  5. Test procedure: Make sure the CAN communication function is normal through the test procedure.

When writing a CAN communication program, it is necessary to understand the basic structure and data format of the CAN protocol, including CAN frame (Frame) type, identifier, data field, control bits and other information. In addition, you also need to pay attention to issues such as data transmission order, byte alignment, and error handling.

how to use

pyb.can

For details, see the official document http://docs.tpyboard.com/zh/latest/library/pyb.CAN/?highlight=pyb.CAN

pyb.can is a module in the Python language, used to control the CAN bus in the MicroPython system. It provides a series of function interfaces, which can realize CAN communication function conveniently.

The following are the main functions of the pyb.can module and what they do:

  • init(speed, mode=MODE_NORMAL, extframe=False, prescaler=0): Initialize CAN bus, set parameters such as baud rate, mode, frame type and prescaler.

  • filter(id, mask=0x7ff, *, extended=False, mode='mask'): Set the CAN filter to filter the received CAN frames.

  • send(data, id, *, timeout=-1, priority=0, rtr=False, extended=False): Send a CAN frame, including frame data, identifier, timeout time, priority, RTR and extended frame and other information .

  • recv(*, timeout=-1, fifo=0): Receive a CAN frame and return information such as frame data, identifier, RTR and extended frame.

  • info(): Obtain CAN bus status information, including error count, receive buffer status, etc.

  • deinit(): Close the CAN bus.

These functions can be called through MicroPython REPL or script files to realize functions such as sending and receiving CAN communication.

can available sensors

CAN bus can connect many types of sensors and actuators, such as:

  1. Automotive electronic sensors: CAN bus is widely used in the automotive industry and can connect various electronic control units (ECU), including engine control module (ECM), brake control module (BCM), transmission control module (TCM), etc.

  2. Industrial measuring instruments: CAN bus can also be used to connect sensors and actuators in industrial automation, such as thermometers, pressure sensors, flow meters, servo drives, etc.

  3. IoT devices: CAN bus can also be used to connect sensors and devices in the Internet of Things, such as environmental monitoring sensors, smart home controllers, robotic actuators, etc.

It should be noted that the CAN communication protocol used by different sensors or actuators may be different, so it is necessary to confirm the communication protocol and parameters of the device before use, and perform corresponding configuration and debugging according to actual needs.

I looked through a certain treasure, and there are not many development boards with can, let's talk about it after encountering the actual combat tutorial QAQ

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/130220104