Main Content

write

Write messages to CAN bus

Description

example

write(ch,identifier,isExtended,dataArray) writes CAN messages onto the bus via the CAN channel.

example

write(ch,CANMessage) writes the CAN message created using the canMessage of Vehicle Network Toolbox™ onto the bus.

Examples

Transmit a CAN Message

Transmit messages to the CAN bus.

arduinoObj = arduino('COM24', 'Uno', 'Libraries', 'CAN');
ch = canChannel(arduinoObj,"Sparkfun CAN-Bus Shield", "BusSpeed", 500e3);
write(ch,1,true,[1:8]);

Transmit a CAN Message using Vehicle Network Toolbox

Transmit messages to the CAN bus.

arduinoObj = arduino('COM24', 'Uno', 'Libraries', 'CAN');
ch = canChannel(arduinoObj,"Sparkfun CAN-Bus Shield", "BusSpeed", 500e3);
message = canMessage(500,false,5);
message.Data = [200:204];
write(ch,message);

Input Arguments

collapse all

Connection to the CAN channel specified as an object.

ID of the message, specified as a numeric value.

Indicates whether the message ID is of standard or extended type, specified as true or false. The logical value true indicates that the ID is of extended type, false indicates standard type.

Data to write to the CAN channel, specified as a non negative numeric value. The data is truncated to uint8.

Message to transmit, specified as a CAN message object. These messages are transmitted via a CAN channel to other CAN nodes.

Note

Error and Remote frames are not supported.

Dependencies

You can create the message using CANMessage only with Vehicle Network Toolbox.

See Also

| | (Vehicle Network Toolbox)

Introduced in R2020a