[Iot smart gateway-09] CAN bus communication demonstration (tinygui touch screen)

Source: Internet
Author: User
Document directory
  • 2. Master CAN communication program
  • 2. CAN communication program from the device
  • 2. tinygui Design

Can is short for Controller Area Network, ISO International Standard serial communication protocol. It was first applied to automobiles, and in Europe it was a standard protocol for Automotive networks, it has been widely used in industrial automation, Smart Home, ships, textiles, medical equipment, and other fields (for details, see the CAN bus protocol in Baidu encyclopedia).

Because the official library of CAN bus interface. Net micro framework is not supported, I designed a CAN bus communication library based on the characteristics of the CAN bus on the basis of the serial library.

The can class library declaration is as follows:

public class CAN    {        public CAN(string canName, CanBaudRate baudRate);        public CAN(string canName, CanBaudRate baudRate, CanConfig config);        public int DatasToRead { get; }        public int DatasToWrite { get; }        public event CanEventHandler DataReceived;        public event CanEventHandler ErrorReceived;        public int Close();        public int DiscardInBuffer();        public int DiscardOutBuffer();        public int Flush();        public int IOControl(CanIOControl code, int parameter);        public int Open();        public int Read(CanData data);        public int SetFilter(int index, CanFilter filter);        public int Write(CanData data);    }

The can2.0a and CAN2.0B protocols are supported at the same time, which are relatively distinctive. One is candata and the other is filter settings.

A candata entry contains the following information:

 

   public class CanData    {        public CanData();        public CanData(uint id, bool IsEXID);        public CanData(uint id, bool IsEXID, byte[] data);        public int Count { get; }        public byte[] Data { get; }        public int Filter { get; }        public uint ID { get; }        public bool IsEXID { get; }        public bool IsRemote { get; }    }

Different from the serial port and network port communication, you can send up to 8 bytes of data in a single frame. The ID can be an 11-bit standard ID or a 29-bit extended ID, frame data can also be defined as a remote frame (excluding data, including only ID and other information) or a standard frame. For stm32f207 (or stm32f103), the sending buffer can cache three data frame R, the receiving buffer is dual-FIFO, and one FIFO can receive three data packets. However, this is not enough. This class library is modeled after serial communication and has a built-in function that allows you to define the size of the sending and receiving buffer (in fact, for serial communication, the sending and receiving buffer is internally fixed and cannot be customized by the user ). In this way, when users send and receive a large amount of data, there will be no worries.

Filters should be the essence of can. It is not easy to understand and set filters. For ease of use, many definition functions are reloaded for the filter class. The filter class library declaration is as follows:

 

   public class CanFilter    {        public CanFilter(bool enable);        public CanFilter(uint id, uint mask);        public CanFilter(uint id0, uint id1, bool IsRemote);        public CanFilter(uint id, uint mask, bool IsRemote, bool IsMaskMode);        public CanFilter(ushort id0, ushort mask0, ushort id1, ushort mask1);        public CanFilter(bool IsMaskMode, bool IsFifo0, bool IsWidth32, uint data1, uint data2);        public CanFilter(ushort id0, ushort id1, ushort id2, ushort id3, bool IsRemote);        public CanFilter(ushort id0, ushort mask0, ushort id1, ushort mask1, bool IsRemote, bool IsMaskMode);    }

For the can function integrated by the stm32 chip, there are two filter modes: ID list mode and ID mask mode. These two types are divided into 32-bit and 16-bit based on the different width of the filtered data.

For more information, see the documentation in yfsoft.can.rar for details.

This article uses three smart Iot gateways for CAN bus network communication to explain the use of the CAN bus (by the way, we will introduce the use of tinygui touch screen events ).

The sample program can be divided into two types: one is the main control program. The three switch buttons are designed above, and the CAN devices to be controlled can be switched. Another method is to deploy the program from the device program to the two devices respectively. The only difference between the program and the two devices is that the ID to be received is different. The other one is 2 #, one is 3 # (as shown in the program interface ).

Before talking about CAN bus communication, let's first introduce the use of tinygui touch screen events.

The sample code is as follows, which is easy to use.

Graphics screen = new graphics (); screen. ontouch + = new toucheventhandler (screen_ontouch); static void screen_ontouch (int x, int y, int state) {// X-x coordinate Y-Y coordinate // state-1 press 0 to lift}

  

2. Master CAN communication program

Step 1: Create a CAN communication class and bind the receive and error events

Can = new can ("can1", canbaudrate. bps_100k );

Can. errorreceived + = new caneventhandler (can_errorreceived );

Step 2: Send different ID identifiers and different can data based on different options

Can. Write (New candata (canid, true, new byte [] {0, 0 }));

Canid is 2 or 3, the first number of data, 0 ~ 2 indicates the corresponding three lights.

2. CAN communication program from the device

Step 1: Create a CAN communication class and bind the receive and error events

Can = new can ("can1", canbaudrate. bps_100k );

Can. datareceived + = new caneventhandler (can_datareceived );

Can. errorreceived + = new caneventhandler (can_errorreceived );

Can. setfilter (0, new canfilter (2, 0, false ));

The ID Id set by the filter is 2 or 3.

Step 2: receive data

    static void can_DataReceived(int canPort, int parameter)    {        int count = can.DatasToRead;        CanData data = new CanData();        for (int i = 0; i < count; i++)        {            can.Read(data);            int index = data.Data[0];            LameState[index] = !LameState[index];            lamp[index].OnDraw(index, LameState[index]);        }              }

  

2. tinygui Design

For more information, see sample code.

After the program is deployed, the video of running time is as follows:

 

Http://v.youku.com/v_show/id_XNDM5NzI1MTg0.html

Note: To run this instance properly, the IOT smart gateway firmware version must be upgraded to v1.8.17 or later. (If your current tinyclr version is earlier than v1.7.15, tinybooter must also be upgraded ).

Firmware: http://www.sky-walker.com.cn/MFRelease/firmware/MFv42_YF_Wisteria207.rar

---------------------------------

Class Library download: http://www.sky-walker.com.cn/MFRelease/library/V42//YFSoft.CAN.rar

Example download: http://www.sky-walker.com.cn/MFRelease/Sample/CAN_Test.rar

Mf Introduction: http://blog.csdn.net/yefanqiu/article/details/5711770

Mf data: http://www.sky-walker.com.cn/News.asp? Id = 25

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.