CANopen Error -2147136862: The buffer size is insufficient for the requested operation

Updated Nov 22, 2024

Reported In

Software

  • LabVIEW

Driver

  • NI-Industrial Communications for CANopen

Issue Details

I am observing the following error when calling the CANopen SDO Read vi from the NI-Industrial Communications for CANopen.

 

Error -2147136862 occurred at CANopen SDO Read [U16].vi:3790001

Possible reason(s):
The buffer size is insufficient for the requested operation. Ensure the buffer size is sufficient for the requested operation.

 

This error only happens when I am using a specific instance of SDO Read function, others like CANopen SDO Read [STR].vi don't output an error.

Solution

A single, non-segmented CANopen SDO message can carry up to 4 bytes of data. The CANopen SDO Read function in the toolkit lets you choose different data types like U8, I8, U16, I16, and so on. The buffer size for incoming data changes depending on the data type you select. For instance:

  • U8/I8 uses a 1-byte buffer
  • U16/I16 uses a 2-byte buffer
  • U32/I32 uses a 4-byte buffer

 

If a CANopen device sends more data than the buffer can handle, a buffer overflow will occur. To avoid this issue, you must use an SDO Read function that matches the maximum number of bytes the device might send.

 

An alternative is to use the SDO Read Block function shown below, which allows you to change the buffer size and outputs an unprocessed U8 array, which you can then manipulate as needed. 

Another option is to read the data as a string, which can handle any number of bytes and then typecast it to the required data type. In the example illustrated below, the string data is cast into a U16 array using the Type Cast function.