|
Matlab class for CAN bus controller |
Top |
|
CANCtrl
Connection to CAN bus Since R2019b
Description A CANCtrl object represents a CAN BUS node for communication with the other CAN Bus nodes in the same CAN bus network. After creating the object, use dot notation to call methods.
Creation
Syntax
can = CANCtrl(portName) can = CANCtrl(portName, Name, Value)
Description can = CANCtrl(portName) connects to the CAN bus network by USB serial port specified by portName. It will use default CAN bus parameters for connecting. can = CANCtrl(portName, Name, Value) connects to the CAN bus network by USB serial port specified by portName and sets additional private properties using optional name-value pair arguments.
Let us introduce CAN Bus Controller function. Our CAN Bus controller can transmit/Receive standard or/and extended CAN Bus data and RTR frames as general CAN bus node. And furthermore, transmitting CAN Bus Frame can be synchronized by special CAN BUS transmitting frame or receiving frame. If Sync is enabled, CAN Bus controller only can transmit CAN Bus frame when it receives or transmits this special CAN BUS frame. This special CAN BUS frame is called "Sync frame" Another feature is that our CAN Bus Controller has Watchdog function. Watchdog purpose is for getting communication fault information. Received Watchdog ( Call it RxWatchdog) is for itself to know whether CAN BUS communication OK or not. Transmitted Watchdog ( Call it TxWatchdog) is for other CAN bus nodes to know whether CAN BUS communication OK or not. TxWatchdog CAN Bus frame send out periodically automatically by hardware, You don't need to send by calling transmit method. Similarly, if "Sync frame" is "Transmit frame", "Sync frame" send out periodically automatically by hardware, You don't need to send by calling transmit method.
Notes: 1. "Sync frame " can use "RxWatchdog/TxWatchdog frame" to replace, and in this situation, the CAN ID setting of sync frame will be ignored. 2. All watchdogs are implemented in hardware. It is hardware real time. You can disable them and use Matlab to implement watchdog by yourself if you don't care hardware real time.
Input Arguments
portName -- USB serial port name which is used by "CAN Bus controller" hardware. character vector | string scalar
USB serial port name, specified as a character vector or string scalar. Use serialportlist to get a list of connected ports.
Example: "COM2"
Name-Value Arguments Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after portName, but the order of the pairs does not matter. Please, use commas to separate each name and value, and enclose Name in quotes. For example, can=CANCtrl("COM1","Sync", true,"SyncCANID", 38) will set Sync enabled and Sync CAN ID = 38 (standard 11 bits of CAN ID) You can use Name-Value pairs to set the following arguments:
Value is logical true or false. true means all trasnmittings will occur after special CAN ID frame (SyncCANID). It may be Transmitting CAN frame, it may be receiver CAN frame. It depends on "SyncByTransmit". Default=false
Value is scalar number which denotes Sync CAN ID. if it is Extended frame, you must make Bit30 =1 (bit0 is MSb). Default=123
Value is scalar number which range from 1 to 65535. It denotes Sync periods in ms. Default=500
Value is logical true or false. true means Receiver Watchdog CAN Frame is enabled. Receiver Watchdog (We call it RxWatchdog) is for my controller to know whether CAN BUS communication fault. Default=false
Value is logical true or false. true means Transmitting Watchdog CAN Frame is enabled. Transmitting Watchdog (We call it TxWatchdog) is for other CAN Bus nodes to know whether CAN BUS communication fault. Default=false
Value is logical true or false. true means we don't use SyncCANID for Sync. Instead of that, we use watchdog (maybe TxWatchdog, Maybe RxWatchdog. It depends on "SyncByTransmit" ) as Sync. Default=false
Value is logical true or false. true means we use Transmitting CAN Frame as Sync. Default=false
Value is scalar number which can be 0, 1, 2. Default=0. It is RxWatchdog working mode. RxWatchdog has 3 different modes. Mode 0: Watchdog initial value is 0. Watchdog is free running up-counter each ms. Watchdog Counter will return 0 if we receive value from Watchdog CAN Bus frame, and value is different from previous received one. When Watchdog Counter is over RxWatchdog Period, it will keep the Rxwatchdog value and communication fault will occur. We don't need our Matlab to detect RxWatchdog. Hardware will do it automatically, and you just use public Property "Status" to get communication fault. Mode 1: Watchdog initial value is 0. Watchdog is free running up-counter each ms. When it arrive at periods value, it will keep it, and one communication fault will occur. RxWatchdog Data packet (receiving watchdog data packet) can change counter value to avoid communication fault. We don't need our Matlab to detect RxWatchdog. Hardware will do it automatically, and you just use public Property "Status" to get communication fault. Mode 2: Watchdog initial value is equal to periods. Watchdog is free running down-counter each ms. When it arrive at 0, it will keep 0 and communication fault will occur. RxWatchdog Data packet (receiving watchdog data packet) can change counter value to avoid communication fault. We don't need our Matlab to detect RxWatchdog. Hardware will do it automatically, and you just use public Property "Status" to get communication fault.
Value is scalar number which can be 1 to 8. Default=8. It is RxWatchdog Data frame's total length (Byte Qty). it is not RxWachdog data length.
Value is scalar number which can be 0, 1, 2, 3. Default=0. It is TxWatchdog working mode. TxWatchdog has 4 different modes. Mode 0 : TxWatchdog value is decided by Method "sendTxWatchdogValue". Value 's position and Length in CAN BUS data packet is decided by "TxWatchdogSartPos" and "TxWatchdogLen". You don't need to send CAN Bus TxWatchdog CAN Bus frame because Hardware sent out automatically, Mode 1: TxWatchdog value increases 1 every TxWatchdog's period by hardware automatically. You don't need to use Method "sendTxWatchdogValue" to give TxWatchdog value. Value 's position and Length in CAN BUS data packet is decided by "TxWatchdogSartPos" and "TxWatchdogLen". Value is unsigned. When value > maximum unsigned number, it will return 0. You don't need to send CAN Bus TxWatchdog CAN Bus frame because Hardware sent out automatically. Mode 2: TxWatchdog value decreases 1 every TxWatchdog's period by hardware automatically. You don't need to use Method "sendTxWatchdogValue" to give TxWatchdog value. Value 's position and Length in CAN BUS data packet is decided by "TxWatchdogSartPos" and "TxWatchdogLen". Value is unsigned. When value =0, it will return maximum unsigned number. You don't need to send CAN Bus TxWatchdog CAN Bus frame because Hardware sent out automatically. Mode 3: TxWatchdog will have no any value, its data packet length is zero. It is used for CANOpen Sync frame.
Value is scalar number which can be 1 to 8. Default=1. It is RxWatchdog value's position (1 based) in frame's data field.
Value is scalar number 1 or 2. Default=2. It is RxWatchdog value' byte Qty.
Value is scalar number which can be 1 to 8. Default=8. It is TxWatchdog Data frame's total length (Byte Qty). it is not TxWachdog data length.
Value is scalar number which can be 1 to 8. Default=1. It is TxWatchdog value's position (1 based) in frame's data field.
Value is scalar number 1 or 2. Default=1. It is TxWatchdog value' byte Qty.
Value is scalar number which denotes RxWatchdog CAN ID. if it is Extended frame, you must make Bit30 =1 (bit0 is MSb). Default=234
Value is scalar number which range from 1 to 65535. It denotes RxWatchdog periods in ms. Default=500
Value is scalar number which denotes TxWatchdog CAN ID. if it is Extended frame, you must make Bit30 =1 (bit0 is MSb). Default=345
Value is scalar number which range from 1 to 65535. It denotes TxWatchdog periods in ms. Default=500
Value is logical true or false. true means you can receive what you sent in CAN Bus. false means you cannot receive what you sent in CAN Bus. Default=false. If your CAN bus controller is "CAN Bus Analyzer only", your setting is no use, it will keep Echo= true even though you set it to false. Similarly, if your CAN bus controller is "CAN Bus Simulator only", your setting is no use, it will keep Echo= false even though you set it to true.
Value is logical true or false. true means you will set hardware with 120 ohms CAN Bus terminator. Default=false
Value is scalar number which range is from 25000 (25K) to 1000000 (1M). Default=250000. It is CAN Bus baud rate. When its BaudRate<50000, 1500000/BaudRate must be integer. When BaudRate>=50000, 3000000/BaudRate must be integer.
Value is 16 numbers of vector which denotes 16 CAN Bus filters. 16 Filters are called F1, F2 ,..., F16. Our CAN bus controller has 3 Masks called M1, M2, M3. F1 to F8 are used M1, F9 to F12 are used M2, F13 to F16 are used M3. CAN bus Controller Hardware only receives CAN Frame with CANID meets the condition below: Fi "Logical bitwise And" M1 = CAN ID "Logical bitwise And" M1 (i=1 to 8) or Fi "Logical bitwise And" M2 = CAN ID "Logical bitwise And" M2 (i=9 to 12) or Fi "Logical bitwise And" M3 = CAN ID "Logical bitwise And" M3 (i=13 to 16) If you want to receive 29 bits of extended frame, you must keep filter and relative Mask both Bit30 =1 (bit0 is MSb), that means Fi=Fi + 2^30 and Mask=Mask+2^30. Of cause, if you want to receive 11 bits of standard frame, you must keep at least one filter and relative Mask both Bit30 =0 (bit0 is MSb).. Default=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2^30]. It means CAN bus controller receives all CAN bus frames if masks use default too. We propose customer to set up Filters and Masks when CAN BUS network has High traffic. Otherwise, don't touch it, just keep default.
Value is 3 numbers of vector which denotes 3 CAN Bus masks. Please read Filter above. Default=[0 0 2^30]. It means CAN bus controller receives all CAN bus frames if Filters use default too.
In general, you only need to set up USB serial port and CAN Bus baud rate, the default behaviour is disable all watchdogs and syncs. And receive all CAN bus frames (11 bits of standard CAN ID and 29 bits of extended CAN ID) except transmitted CAN Frames. The following statement is usually used in general project:
can=CANCtrl("COM2","baudrate", 250000);
In the following example, we will enable Sync, and it will only transmit CAN Frame only when CAN Bus controller receives standard frame with CAN ID=651:
can=CANCtrl("COM2","baudrate", 250000, "Sync", true, "SyncCANID", 651);
In the following example, we will enable Sync, and it will transmit extended frame with CAN ID=351 every 300ms automatically. Any transmit method only does truly transmitting after extended frame of CAN ID=351 transmitted. can=CANCtrl("COM2","baudrate", 250000, "Sync", true, "SyncCANID", 351+2^30, "SyncByTransmit", true, "SyncPeriod", 300);
In the following example, we will enable Sync. However, we use TxWatchdog to act as Sync. So you don't need to set Sync CAN ID and Sync period. TxWatchdog mode is 1, which means Watchdog counter increases 1 every 800ms. TxWatchdog CAN ID is extended 29 bits of 888. TxWatchdog Counter is 16 bits. and from CAN Bus frame data field's 1st byte start. So it will transmit extended frame with CAN ID=88 (DLC=4) every 800ms automatically. Any transmit method only does truly transmitting after extended frame of CAN ID=888 transmitted.
can=CANCtrl("COM2","baudrate", 250000, "Sync", true, "SyncUseWatchdog", true, "SyncByTransmit", true, ... "TxWatchdog" , true, "TxWatchdogDLC", 4, "TxWatchdogCANID", 888+2^30, "TxWatchdogMode", 1, ... "TxWatchdogSartPos", 1, "TxWatchdogLen", 2, "TxWatchdogPeriod", 800);
In the following example, firstly, we use "x" to denote 0 or 1 for an bit value (don't care). Our CAN BUS controller will receive standard CAN ID in binary below:
And Our CAN BUS controller will also receive extended CAN ID in binary below
The following statement will implement CAN bus object wuth above function:
can=CANCtrl("COM2","baudrate", 250000, "Filters", ... [15, 1, 2, 3, 3, 3, 3, 3, 1199570944, 1199570944, 1199570944, 1199570944, 1082130432, 1082130432, 1082130432, 1082130432], ... "Masks", [15, 1199570944, 1199570944]);
Properties
uint8 type row vector. Status(1) is CAN bus Rx Error count value, Status(2) is CAN bus Tx Error count value. Status(3) is BUS OFF (1: Bus off, 0: no bus off). Status(4) is CAN Bus communication fault flag based on RxWatchdog. (1 :Fault, 0: normal)
Object Functions (Or Methods)
Events
SpecialCANCaught. After you use setCatchCANID method to set up a special CAN ID and use enableCatchCANID method to enable catch this CAN ID, when receive this CAN ID Frame, this event willl be created. If you already use setRxEventCallback method to set up your call back function, Matlab will execute your call back function. In general, in call back function, you firstly call refresh method to update receiver buffers, and then use receive method to get data you want.
All methods details
transmit 11 bits of standard CAN bus data frames with the same data type Since R2019b
Syntax
transmitDataStandard(device,CANID, DLC, Data,DataType,Endian)
Description
Transmit 11 bits of standard CAN bus data frames It is used for transmitting data frames with the same data type. If different data type, please use method transmitDataSingleFrame
Input Arguments
CANCtrl object
number vector for CAN ID of each standard data frame. This argument is mandatory.
number vector for Data bytes Qty of each standard data frame. This argument is mandatory.
number matrix for CAN bus Data. Row is frame No, column is data. This argument is mandatory.
String scalar. It can be "uint8", "int8", "uint16","int16","uint32","int32","single","uint64","int64","double" Default="uint8"
String scalar. It can be "Little-Endian", "Big-Endian". Default="Little-Endian"
Examples
We send standard CAN Bus data, Data type is uint16, and little-endian. Every frame has 4 uint16 data (8 bytes). The first frame : CAN ID= 12, DLC=8, and 4 uint16 data are "367, 789, 8901, 9992" The second frame: CAN ID=67, DLC=8, and 4 uint16 data are "1367, 2000, 3000, 8001"
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k:
can=CANCtrl("COM2","baudrate", 250000);
Then we prepare input arguments:
CANID=[12 67]; DLC=[8 8]; Data=[367 789 8901 9992; 1367 2000 3000 8001];
At last, we call CANCtrl object's method to transmit CAN frames:
transmitDataStandard(can,CANID,DLC.Data,"uint16","Little-Endian");
transmit 29 bits of extended CAN bus data frames with the same data type Since R2019b
Syntax
transmitDataExtend(device,CANID, DLC, Data,DataType,Endian)
Description
Transmit 29 bits of extended CAN bus data frames It is used for transmitting data frames with the same data type. If different data type, please use method transmitDataSingleFrame
Input Arguments
CANCtrl object
number vector for CAN ID of each standard data frame. This argument is mandatory.
number vector for Data bytes Qty of each standard data frame. This argument is mandatory.
number matrix for CAN bus Data. Row is frame No, column is data. This argument is mandatory.
String scalar. It can be "uint8", "int8", "uint16","int16","uint32","int32","single","uint64","int64","double" Default="uint8"
String scalar. It can be "Little-Endian", "Big-Endian". Default="Little-Endian"
Examples
We send extended CAN Bus data, Data type is int32, and big-endian. The first frame : CAN ID= 8412, DLC=8, and 2 int32 data are "-32189, 98765" The second frame: CAN ID=9867, DLC=4, and only one int32 data are "-999998"
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 125k:
can=CANCtrl("COM2","baudrate", 125000);
Then we prepare input arguments:
CANID=[8412 9867]; DLC=[8 4]; Data=[-32189 98765; -999998 0];
At last, we call CANCtrl object's method to transmit CAN frames:
transmitDataExtend(can,CANID,DLC.Data,"int32","Big-Endian");
transmit 11/29 bits of standard/extended CAN bus data single frame with the different data type Since R2019b
Syntax
transmitDataSingleFrame(device, CANID, Extended, Data, DataType, Endian)
Description
Transmit 11/29 bits of standard/extended CAN bus data single frame. It is used for transmitting data frames with the different data type. If the same data type, please use method transmitDataStandard or transmitDataExtend
Input Arguments
CANCtrl object
number scalar for CAN ID. This argument is mandatory..
logical scalar. true means extended data frame. This argument is mandatory.
number vector for CAN bus Data in single frame. This argument is mandatory.
String vector. It denotes each data type in CAN Bus Data field.. It can be "uint8", "int8", "uint16","int16","uint32","int32","single","uint64","int64","double" Default="uint8"
String scalar. It denotes each data type in CAN Bus Data field. It can be "Little-Endian", "Big-Endian". Default="Little-Endian"
Examples
We send extended CAN Bus data, 3 data, the 1st Data is uint8, .value is 125, the 2nd data is int16, value is -12560, the 3rd data is single, value is 412.38. We use "Big-Endian" for int16 and single CAN ID is 59904. We have to make sure total byte qty<=8. In this example, total byte qty=1+2+4=7. It is OK. And method will use DLC=7 automatically.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 125k:
can=CANCtrl("COM2","baudrate", 125000);
Then we prepare input arguments:
CANID=59904; Data=[125 -12560 412.38]; DataType=["uint8" "int16" "single"];
At last, we call CANCtrl object's method to transmit CAN frames:
transmitDataSingleFrame(can,CANID, true. Data, DataType ,"Big-Endian");
transmit 11/29 bits of standard/extended CAN bus data long frames with the same data type Since R2019b
Syntax
transmitDataLongFrame(device, CANID, Extended, Data, DataType, Endian, FrameNoStart, isLastFrameDLC8)
Description
Transmit 11/29 bits of standard/extended CAN bus data long frames. Long frames means that multiple frames with the same CAN ID and first data byte is frame number starting from 0 or 1 in general. The other 7 bytes in long frames data field will be truly data. Every frame number from long frame is increased 1 consecutively. The last frame's DLC can be 8 or less. This method is used for transmitting data frames with the same data type. but data bytes qty is over 8. The first byte in each frame is frame number.
Input Arguments
CANCtrl object
number scalar for CAN ID. This argument is mandatory..
logical scalar. true means extended data frame. This argument is mandatory.
number vector for CAN bus Data in single frame. This argument is mandatory.
String scalar. It denotes each data type in CAN Bus Data field.. It can be "uint8", "int8", "uint16","int16","uint32","int32","single","uint64","int64","double" Default="uint8"
String scalar. It denotes each data type in CAN Bus Data field. It can be "Little-Endian", "Big-Endian". Default="Little-Endian"
number scalar. It denotes the first frame's frme number. Default=0.
logical scalar. true means that the DLC of the last frame is 8. the remainder data of last frame will be zero. false means that system will use actual DLC for the last frame. Default= false
Examples
We send standard CAN Bus data, 12 data, Data type is uint16, .value is 125, 89, 678, 134, 789, 444, 555, 666,777,111,222, and 888. We use "Big-Endian" for uint16 CAN ID is 599. FrameNoStart=3. The last frame DLC uses actual length we need.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 125k:
can=CANCtrl("COM2","baudrate", 125000);
Then we prepare input arguments:
CANID=599; Data=[125, 89, 678, 134, 789, 444, 555, 666,777,111,222, 888]; DataType="uint16";
At last, we call CANCtrl object's method to transmit CAN frames:
transmitDataLongFrame(can,CANID, false. Data, DataType ,"Big-Endian", 3, false);
transmit 11 bits CAN bus remote request frames. Since R2019b
Syntax
transmitRTRStandard(device,CANID)
Description
Transmit 11 bits of standard CAN bus remote request frames It is used for transmitting multiple RTR frames with different CAN ID.
Input Arguments
CANCtrl object
number vector for CAN ID of each standard RTR frame. This argument is mandatory.
Examples
We send 3 standard CAN Bus remote request frames. The first frame : CAN ID= 12. The second frame: CAN ID=67. The third frame: CAN ID=84.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k:
can=CANCtrl("COM2","baudrate", 250000);
Then we prepare input arguments:
CANID=[12 67 84];
At last, we call CANCtrl object's method to transmit CAN frames:
transmitRTRStandard(can,CANID);
transmit 29 bits CAN bus remote request frames. Since R2019b
Syntax
transmitRTRExtend(device,CANID)
Description
Transmit 29 bits of extended CAN bus remote request frames It is used for transmitting multiple RTR frames with different CAN ID.
Input Arguments
CANCtrl object
number vector for CAN ID of each extended RTR frame. This argument is mandatory.
Examples
We send 3 extended CAN Bus remote request frames. The first frame : CAN ID= 12345. The second frame: CAN ID=67000. The third frame: CAN ID=84989.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k:
can=CANCtrl("COM2","baudrate", 250000);
Then we prepare input arguments:
CANID=[12345 67000 84989];
At last, we call CANCtrl object's method to transmit CAN frames:
transmitRTRExtend(can,CANID);
change Tx Watchdog counter value.. Since R2019b
Syntax
sendTxWatchdogValue(device, TxWDValue)
Description
Send out TxWatchdog value. It is used when TxWatchdog Enable and mode is 0..
Input Arguments
CANCtrl object
number scalar for TxWatchdog counter value. This argument is mandatory.
Examples
We enabled TxWatchdog. And its work mode is 0. TxWatchdog Periods=300ms, TxWatchdog frame has 8 bytes. However, TxWatchdog counter has 2 bytes and start from the first byte of TxWatchdog CAN Bus frame. We will send TxWatchdog counter value every 600ms. and Counter value increase 5 every 600ms. If it over maximum value of 2 bytes (65535), it will be back to zero.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k , and set up TxWatchdog as we expected.
can=CANCtrl("COM2","baudrate", 250000, "TxWatchdog", true, "TxWatchdogCANID", 56, ... "TxWatchdogDLC", 8, "TxWatchdogSartPos", 1, "TxWatchdogLen" , 2, "TxWatchdogMode", 0 , "TxWatchdogPeriod", 300);
And we set up counter initial value=0
counter=0;
then, we call CANCtrl object's method to send out counter value every 600ms
while true sendTxWatchdogValue(can, counter); counter=counter+5; % modify counter value if counter>65535 counter=0; end pause(0.6) % pause 600 ms for sending every 600ms end
update CAN Bus receive buffers. If you want the latest data, you must call it. Since R2019b
Syntax
refresh(device,)
Description
let CAN bus receiver buffers update to dictate the latest receiver frames. After executing this method, all old buffers will disappear, and buffers will be filled with all receiver frames from previous refresh to this time's refresh. If you want the latest frame, you should call it before you call any receive method, otherwise, you still get old data.
Input Arguments
CANCtrl object
receive CAN Bus frames's raw data. Since R2019b
Syntax
[CANID,Transmit, Extended, RTR, DLC, Data, TimeStamp,Status] = receiveRaw( device )
Description
get CAN Bus received frames's raw data. You must call refresh method before calling it in order to get new frame data. You can know whether received frame is self-transmitted or RTR, and you can know time stamp and Communication status in additional to received data byte. This method is non-block function. If no any frame received, it will get empty vector for CAN ID.
Input Arguments
CANCtrl object
Output Arguments
uint32 row vector for received CAN ID.
logical row vector. true means received frame is what it transmitted frame.
logical row vector. true means received frame is 29 bits of extended frame.
logical row vector. true means received frame is remote request frame.
uint8 row vector which tells you DLC of received frame.
uint8 Matrix, which denotes received frame's data. row is frame number, column is data number. Matrix has 8 columns which means maximum 8 bytes.
double type row vector. it denotes each frame's received time stamp in second. Time start point (0 value point) is from time you create CAN BUS object.
uint8 type row vector. Status(1) is CAN bus Rx Error count value, Status(2) is CAN bus Tx Error count value. Status(3) is BUS OFF (1: Bus off, 0: no bus off). Status(4) is CAN Bus communication fault flag based on RxWatchdog. (1 :Fault, 0: normal)
Examples
The following example will receive CAN bus frames every 200ms.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k.
can=CANCtrl("COM2","baudrate", 250000 );
then, we call CANCtrl object's method refresh and receiveRaw to get received frame information every 200ms
while true pause(0.2) % pause 200 ms for receiving every 200ms refresh( can ); [CANID,Transmit, Extended, RTR, DLC, Data, TimeStamp,Status] = receiveRaw( can ) end
receive specified CAN ID single frame Data with different data type. Since R2019b
Syntax
[DataOut, Exist]=receiveSingleFrame( device, CANIDTarget, ExtendedTarget, RTRTarget, DataType, Endian)
Description
This is non-block method. It will receive specified CAN ID single frame Data. (One frame with different data type in CAN Bus data field). If not received, Exist will be false. You must call refresh method before call it in order to get the new data.
Input Arguments
CANCtrl object
number type scalar for received CAN ID. This is mandatory argument.
logical scalar. true means we only need extended frame. false means we only need standard frame. This is mandatory argument.
logical scalar. true means we only need remote request frame. false means we only need data frame. This is mandatory argument.
String vector. It denotes each data type in CAN Bus Data field. It can be "uint8", "int8", "uint16","int16","uint32","int32","single","uint64","int64","double" Default="uint8".
String scalar. It denotes CAN Bus Data frame's Data Endian. It can be "Little-Endian", "Big-Endian". Default="Little-Endian"
Output Arguments
double row vector. It denotes each data we received from specified CAN ID. For RTR frame or no any frame received, DataOut is empty.
logical scalar. true means we got what we expected CAN bus frame.
Examples
The following example will receive CAN bus frames every 200ms. We expect CAN ID=12 standard frame with 1 data and int32 data type, and 1 data with single data type. We expect CAN ID=12 extended frame with 3 data and uint16 data type. All data are "Little-Endian"
Note: Although these 2 CAN Bus frames' CAN ID is 12, they are different CAN ID because one is 11 bits' standard CAN ID, the other is 29 bits' extended CAN ID
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k.
can=CANCtrl("COM2","baudrate", 250000 );
then, we call CANCtrl object's method refresh and receiveSingleFrame to get data we expected every 200ms
while true pause(0.2) % pause 200 ms for receiving every 200ms refresh( can ); % refresh receiver buffers [DataOut1, Exist1]=receiveSingleFrame( can, 12, false, false, ["int32" "single"]); if Exist1 disp("standard frame Data:") disp(DataOut1) end [DataOut2, Exist2]=receiveSingleFrame( can, 12, true, false, ["int16" "int16" "int16" ]); if Exist2 disp("extended frame Data:") disp(DataOut2) end end
receive specified CAN ID multiple frames Data with the same data type. Since R2019b
Syntax
[DataOut, Exist]=receiveMultipleFrames (device, CANIDTarget, ExtendedTarget, DataQty, DataType, Endian, FrameNoStart)
Description
This is non-block method. It will receive specified CAN ID multiple frames Data (Multiple frames with the same CAN ID , and more than 8 bytes' data, and all data types are the same , The first byte of every frame is frame number.). If not received, Exist will be false. You must call refresh method before call it in order to get the new data. And you must make sure all frames are ready before you call refresh method. You can use Call back function of event "SpecialCANCaught" to guarantee.
Input Arguments
CANCtrl object
number type scalar for received CAN ID. This is mandatory argument.
logical scalar. true means we only need extended frame. false means we only need standard frame. This is mandatory argument.
number type scalar. It denotes how many data items we will receive (data unit is DatatType parameter below) . This is mandatory argument..
String scalar. It denotes data type in CAN Bus Data field. It can be "uint8", "int8", "uint16","int16","uint32","int32","single","uint64","int64","double" Default="uint8".
String scalar. It denotes CAN Bus Data frame's Data Endian. It can be "Little-Endian", "Big-Endian". Default="Little-Endian"
number scalar. It denote the first frame's frame number. The value is 0 to 255. Default=0.
Output Arguments
double row vector. It denotes all data we received from multiple frames. If long data frames are not fully ready (received entire long frames in the frame Number order), DataOut will be empty and Exist=false.
logical scalar. true means we got what we expected CAN bus frame.
Examples
The following example will receive CAN bus frames every 200ms. We expect CAN ID=12 standard frame with 43 data with int16 data type and all data are "Little-Endian". The first frame number is 0
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k.
can=CANCtrl("COM2","baudrate", 250000 );
then, we call CANCtrl object's method refresh and receiveMultipleFrames to get data we expected every 200ms
while true pause(0.2) % pause 200 ms for receiving every 200ms refresh( can ); % refresh receiver buffers [DataOut, Exist]=receiveMultipleFrames (can, 12, false, 43, "uint16"); if Exist disp("standard frame Data:") disp(DataOut) end end
The above example is not perfect. If the time of refresh is before receiving the last frame, it will have problem. You can set one Flag variable to true in the Call back function of event "SpecialCANCaught" (catch the last frame of long frame). For example, Flag variable is "Got_CANID". The example code is modified as below:
while true if Got_CANID pause(0.2) % pause 200 ms for receiving every 200ms Got_CANID=false; refresh( can ); % refresh receiver buffers [DataOut, Exist]=receiveMultipleFrames (can, 12, false, 43, "uint16"); if Exist disp("standard frame Data:") disp(DataOut) end end end
set up one special CAN ID we can catch Since R2019b
Syntax
setCatchCANID(device, CANID, Extended, CatchSpecialframe, SpecialFrameNo)
Description
It will set up a special CAN ID. When this special CAN ID received, CAN BUS controller object will create event "SpecialCANCaught" if caught CAN ID enabled and CatchSpecialframe is false. Or when this special CAN ID received and the first byte of data field=SpecialFrameNo, we will create event "SpecialCANcaught" if caught CAN ID is enabled and CatchSpecialframe is true. Event "SpecialCANcaught" will call "callback function". Callback function is customized function, You can use method "setRxEventCallback" to set it.
Input Arguments
CANCtrl object
number type scalar for caught CAN ID. This is mandatory argument.
logical type scalar for CAN ID type of caught CAN frame. Default=false, means 11 bits of standard frame.
logical type scalar. True means we will compare 1st byte of caught CAN frame besides CAN ID and Extended, we only create event "SpecialCANcaught" when both CAN ID / Extended and first bye match. False means we only care CAN ID and Extended value , we don't care RTR and the 1st byte. Default=false.
number type scalar for the 1st byte value of CAN bus data field. Data range is 0 to 255. Default=2.
Examples
The following example will set up caught CAN ID=standard 11 bits' 456. Don't care data field.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k.
can=CANCtrl("COM2","baudrate", 250000 );
then we use the following statement to set up caught CAN ID:
setCatchCANID( can, 456, false );
Enable/disable CAN ID catch function Since R2019b
Syntax
enableCatchCANID( device, enable)
Description
It will enable/disable "Caught CAN ID". And if it is enabled and "Caught CAN ID" received and also other conditions are met, it will create event "SpecialCANCaught". It leads to callback function executing.
Input Arguments
CANCtrl object
logical scalar. true means we enable "Catch CAN ID". Default=true..
Examples
The following example will set up caught CAN ID=standard 11 bits' 456. and Enable it.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k.
can=CANCtrl("COM2","baudrate", 250000 );
then we use the following statement to set up caught CAN ID:
setCatchCANID( can, 456, false );
At last, we enable it:
enableCatchCANID( can );
set up callback function for caught CAN ID Since R2019b
Syntax
setRxEventCallback( device, myCallback)
Description
It will set up callback function for event "SpecialCANCaught".
Input Arguments
CANCtrl object
Customer's function. Function prototype is " FunctionName(src,event) " Inside call back function, you will call src.fresh and src's receiveRaw method or src's receiveSingleFrame or receiveMultipleFrames methods to get received data.
Examples
The following example will set up caught CAN ID=standard 11 bits' 456. and Enable it. And when CAN ID 456 Coming, call back function will be called.
Firstly, we set up a CANCtrl object with COM2 and CAN Bus baud rate: 250k.
can=CANCtrl("COM2","baudrate", 250000 );
then we use the following statement to set up caught CAN ID:
setCatchCANID( can, 456, false );
We write a call back function:
myCallback=@(src,evt) (disp("ok"));
And we use setRxEventCallback to set up my Call back function:
setRxEventCallback( can, myCallback)
At last, we enable "Caught CAN ID":
enableCatchCANID( can );
When CAN Bus controller receives standard CANID=456, it will call myCallback, so "ok" will be displayed. |